I have a GTF file (in 'test.gtf' file):
chr1 HAVANA gene 10069 18412 . + . gene_id "ENSG00000223972.4"; transcript_id "ENSG00000223972.4"; gene_type "pseudogene"; gene_status "KNOWN"; gene_name "DDX11L1"; transcript_type "pseudogene"; transcript_status "KNOWN"; transcript_name "DDX11L1"; level 2; havana_gene "OTTHUMG00000000961.2";
chr2 HAVANA gene 11869 14412 . + . gene_id "ENSG0000023172.1"; transcript_id "ENSG000023172.1"; gene_type "pseudogene"; gene_status "KNOWN"; gene_name "PCNA"; transcript_type "pseudogene"; transcript_status "KNOWN"; transcript_name "PCNA"; level 2; havana_gene "OTTHUMG00000001931.2";
chr3 HAVANA gene 15769 19412 . + . gene_id "ENSG00000433972.1"; transcript_id "ENSG000023172.1"; gene_type "pseudogene"; gene_status "KNOWN"; gene_name "USP21"; transcript_type "pseudogene"; transcript_status "KNOWN"; transcript_name "USP21"; level 2; havana_gene "OTTHUMG00000002361.2";
I have loaded it as a pyranges object and I want to subset it so that it only contains gene from test_list
:
import pyranges as pr
import pandas as pd
gtf_file = "test.gtf"
test_list = ["PCNA","USP21", "USP1"]
gtf = pr.read_gtf(gtf_file)
sub_gtf = gtf
sub_gtf.df = gtf.df[gtf.df.gene_name.isin(test_list)]
But the last line throws this error:
File "<ipython-input-24-af0b4b5c7576>", line 1, in <module>
irongtf.df = irongtf.df[irongtf.df.gene_name.isin(test_list)]
File "/home/niek/.local/lib/python3.8/site-packages/pyranges/pyranges.py", line 314, in __setattr__
_setattr(self, column_name, column)
File "/home/niek/.local/lib/python3.8/site-packages/pyranges/methods/attr.py", line 48, in _setattr
df.insert(pos, column_name, column)
File "/home/niek/.local/lib/python3.8/site-packages/pandas/core/frame.py", line 4413, in insert
value = self._sanitize_column(value)
File "/home/niek/.local/lib/python3.8/site-packages/pandas/core/frame.py", line 4504, in _sanitize_column
com.require_length_match(value, self.index)
File "/home/niek/.local/lib/python3.8/site-packages/pandas/core/common.py", line 531, in require_length_match
raise ValueError(
ValueError: Length of values (2) does not match length of index (1)
What would be the right way to subset the gtf file so that it only contains the rows where gene_name
contains the gene symbols from test_list
?
Or is there another tool that I could use to do this?