forked from Jverma/Visualizing-genome-annotations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gff_parser.py
199 lines (157 loc) · 5.93 KB
/
gff_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# -*- coding: utf-8 -*-
# Parsing annotations file to extract data to be used for other purposes.
# Author - Janu Verma
# @januverma
import sys
import json
class gffParser:
""" GFF Parser Class.
Extracts the relevant information and stores it in a way
which facilitates quick access and processing.
Parameters
----------
input_file : path to the gff file.
Example
-------
>>> from gff import gffParser
>>> import sys
>>> input_file = sys.argv[1]
>>> out = gffParser(input_file)
>>> out.getGenes("1")
"""
def __init__(self, input_file):
self.data = {}
for line in input_file:
record = line.strip().split("\t")
sequence_name = record[0]
source = record[1]
feature = record[2]
start = int(record[3])
end = int(record[4])
if (record[5] != '.'):
score = record[5]
else:
score = None
if (record[6] == '+'):
strand = 1
else:
strand = -1
if (record[7] != '.'):
frame = record[7]
else:
frame = None
attributes = record[8].split(';')
attributes = {x.split('=')[0] : x.split('=')[1] for x in attributes if '=' in x}
if not(sequence_name in self.data):self.data[sequence_name] = []
alpha = {'source':source, 'feature':feature, 'start':start, 'end':end, 'score':score, 'strand':strand, 'frame':frame}
for k,v in attributes.iteritems(): alpha[k] = v
self.data[sequence_name].append(alpha)
def getChromosomes(self):
"""Gets all the chromosomes in the gff file.
Returns
-------
A list of chromosomes in the input GFF file.
"""
return self.data.keys()
def getGenes(self, Id):
""" Gets all the genes for a chromosomes with all the relevant information.
Parameters
----------
Id : The identifier for the sequence. e.g. 9, 1, 2 in our file.
Returns
-------
A list of dictionaries where each dictionary corresponds to a gene in the sequence.
"""
genes_list = []
chromosome = self.data[Id]
for x in chromosome:
if (x['feature'] == 'gene'):
gene_info = x
genes_list.append(gene_info)
return genes_list
def getmRNA(self, seq_name, Id):
""" Gets all the mRNAs (transcripts) for a given gene.
Arguments
---------
seq_name : The name/identifier of the sequence.
Id : The identifier/name of the gene we are interested in.
Returns
-------
A list of dictionaries where each dictionary contains
information about an mRNA for the gene.
"""
mRNA_list = []
for x in self.data[seq_name]:
if (x['feature'] == 'mRNA') and (x['Parent'] == Id):
mRNA_info = x
mRNA_list.append(mRNA_info)
return mRNA_list
def getCDS(self, seq_name, Id):
""" Gets all the CDS for a given transcript (mRNA).
Parameters
----------
seq_name : Name/identifier of the sequence.
Id : Identifier of the mRNA.
Returns
-------
A list of dictionaries where each dictionary contains the
informations about an CDS for the transcript.
"""
cds_list = []
for x in self.data[seq_name]:
if (x['feature'] == 'CDS') and (x['Parent'] == Id):
cds_info = x
cds_list.append(cds_info)
return cds_list
def getFivePrimeUTR(self, seq_name, Id):
""" Gets all the five prime UTRs for a given transcript.
Parameters
----------
seq_name : Name/identifier of the sequence.
Id : Identifier of the mRNA.
Returns
-------
A list of dictionaries where each dictionary contains the
informations about an 5'-UTR for the transcript.
"""
fivePrimeUTR_list =[]
for x in self.data[seq_name]:
if (x['feature'] == 'five_prime_UTR') and (x['Parent'] == Id):
fivePrimeUTR_info = x
fivePrimeUTR_list.append(fivePrimeUTR_info)
return fivePrimeUTR_list
def getThreePrimeUTR(self, seq_name,Id):
""" Gets all the three prime UTRs for a given trancript.
Parameters
----------
seq_name : Name/identifier of the sequence.
Id : Identifier of the mRNA.
Returns
-------
A list of dictionaries where each dictionary contains the
informations about an 3'-UTR for the transcript.
"""
threePrimeUTR_list = []
for x in self.data[seq_name]:
if (x['feature'] == 'three_prime_UTR') and (x['Parent'] == Id):
threePrimeUTR_info = x
threePrimeUTR_list.append(fivePrimeUTR_info)
return threePrimeUTR_list
def child_parent_dict(self):
""" Gets parents of a child transcript.
Returns
-------
A dictionary containing transcripts as keys and lists containing
the parent gene, chromosome and strand as values.
"""
child_parent_dict = {}
for x in self.data.keys():
y = self.data[x]
for z in y:
if (z['feature'] == 'mRNA'):
mRNA_id = z['ID']
parent_gene = z['Parent']
strand = z['strand']
child_parent_dict[mRNA_id] = [parent_gene, x, strand]
return child_parent_dict