annotate GFFParser.py @ 5:6e589f267c14

Uploaded
author devteam
date Tue, 04 Nov 2014 12:15:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
6e589f267c14 Uploaded
devteam
parents:
diff changeset
1 #!/usr/bin/env python
6e589f267c14 Uploaded
devteam
parents:
diff changeset
2 """
6e589f267c14 Uploaded
devteam
parents:
diff changeset
3 Extract genome annotation from a GFF (a tab delimited format for storing sequence features and annotations) file.
6e589f267c14 Uploaded
devteam
parents:
diff changeset
4
6e589f267c14 Uploaded
devteam
parents:
diff changeset
5 Requirements:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
6 Numpy :- http://numpy.org/
6e589f267c14 Uploaded
devteam
parents:
diff changeset
7 Scipy :- http://scipy.org/
6e589f267c14 Uploaded
devteam
parents:
diff changeset
8
6e589f267c14 Uploaded
devteam
parents:
diff changeset
9 Copyright (C)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
10
6e589f267c14 Uploaded
devteam
parents:
diff changeset
11 2009-2012 Friedrich Miescher Laboratory of the Max Planck Society, Tubingen, Germany.
6e589f267c14 Uploaded
devteam
parents:
diff changeset
12 2012-2014 Memorial Sloan Kettering Cancer Center, New York City, USA.
6e589f267c14 Uploaded
devteam
parents:
diff changeset
13 """
6e589f267c14 Uploaded
devteam
parents:
diff changeset
14
6e589f267c14 Uploaded
devteam
parents:
diff changeset
15 import re
6e589f267c14 Uploaded
devteam
parents:
diff changeset
16 import os
6e589f267c14 Uploaded
devteam
parents:
diff changeset
17 import sys
6e589f267c14 Uploaded
devteam
parents:
diff changeset
18 import urllib
6e589f267c14 Uploaded
devteam
parents:
diff changeset
19 import numpy as np
6e589f267c14 Uploaded
devteam
parents:
diff changeset
20 import scipy.io as sio
6e589f267c14 Uploaded
devteam
parents:
diff changeset
21 from collections import defaultdict
6e589f267c14 Uploaded
devteam
parents:
diff changeset
22 import helper as utils
6e589f267c14 Uploaded
devteam
parents:
diff changeset
23
6e589f267c14 Uploaded
devteam
parents:
diff changeset
24 def attribute_tags(col9):
6e589f267c14 Uploaded
devteam
parents:
diff changeset
25 """
6e589f267c14 Uploaded
devteam
parents:
diff changeset
26 Split the key-value tags from the attribute column, it takes column number 9 from GTF/GFF file
6e589f267c14 Uploaded
devteam
parents:
diff changeset
27
6e589f267c14 Uploaded
devteam
parents:
diff changeset
28 @args col9: attribute column from GFF file
6e589f267c14 Uploaded
devteam
parents:
diff changeset
29 @type col9: str
6e589f267c14 Uploaded
devteam
parents:
diff changeset
30 """
6e589f267c14 Uploaded
devteam
parents:
diff changeset
31 info = defaultdict(list)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
32 is_gff = False
6e589f267c14 Uploaded
devteam
parents:
diff changeset
33
6e589f267c14 Uploaded
devteam
parents:
diff changeset
34 if not col9:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
35 return is_gff, info
6e589f267c14 Uploaded
devteam
parents:
diff changeset
36
6e589f267c14 Uploaded
devteam
parents:
diff changeset
37 # trim the line ending semi-colon ucsc may have some white-space
6e589f267c14 Uploaded
devteam
parents:
diff changeset
38 col9 = col9.rstrip(';| ')
6e589f267c14 Uploaded
devteam
parents:
diff changeset
39 # attributes from 9th column
6e589f267c14 Uploaded
devteam
parents:
diff changeset
40 atbs = col9.split(" ; ")
6e589f267c14 Uploaded
devteam
parents:
diff changeset
41 if len(atbs) == 1:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
42 atbs = col9.split("; ")
6e589f267c14 Uploaded
devteam
parents:
diff changeset
43 if len(atbs) == 1:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
44 atbs = col9.split(";")
6e589f267c14 Uploaded
devteam
parents:
diff changeset
45 # check the GFF3 pattern which has key value pairs like:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
46 gff3_pat = re.compile("\w+=")
6e589f267c14 Uploaded
devteam
parents:
diff changeset
47 # sometime GTF have: gene_id uc002zkg.1;
6e589f267c14 Uploaded
devteam
parents:
diff changeset
48 gtf_pat = re.compile("\s?\w+\s")
6e589f267c14 Uploaded
devteam
parents:
diff changeset
49
6e589f267c14 Uploaded
devteam
parents:
diff changeset
50 key_vals = []
6e589f267c14 Uploaded
devteam
parents:
diff changeset
51
6e589f267c14 Uploaded
devteam
parents:
diff changeset
52 if gff3_pat.match(atbs[0]): # gff3 pattern
6e589f267c14 Uploaded
devteam
parents:
diff changeset
53 is_gff = True
6e589f267c14 Uploaded
devteam
parents:
diff changeset
54 key_vals = [at.split('=') for at in atbs]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
55 elif gtf_pat.match(atbs[0]): # gtf pattern
6e589f267c14 Uploaded
devteam
parents:
diff changeset
56 for at in atbs:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
57 key_vals.append(at.strip().split(" ",1))
6e589f267c14 Uploaded
devteam
parents:
diff changeset
58 else:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
59 # to handle attribute column has only single value
6e589f267c14 Uploaded
devteam
parents:
diff changeset
60 key_vals.append(['ID', atbs[0]])
6e589f267c14 Uploaded
devteam
parents:
diff changeset
61 # get key, val items
6e589f267c14 Uploaded
devteam
parents:
diff changeset
62 for item in key_vals:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
63 key, val = item
6e589f267c14 Uploaded
devteam
parents:
diff changeset
64 # replace the double qoutes from feature identifier
6e589f267c14 Uploaded
devteam
parents:
diff changeset
65 val = re.sub('"', '', val)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
66 # replace the web formating place holders to plain text format
6e589f267c14 Uploaded
devteam
parents:
diff changeset
67 info[key].extend([urllib.unquote(v) for v in val.split(',') if v])
6e589f267c14 Uploaded
devteam
parents:
diff changeset
68
6e589f267c14 Uploaded
devteam
parents:
diff changeset
69 return is_gff, info
6e589f267c14 Uploaded
devteam
parents:
diff changeset
70
6e589f267c14 Uploaded
devteam
parents:
diff changeset
71 def spec_features_keywd(gff_parts):
6e589f267c14 Uploaded
devteam
parents:
diff changeset
72 """
6e589f267c14 Uploaded
devteam
parents:
diff changeset
73 Specify the feature key word according to the GFF specifications
6e589f267c14 Uploaded
devteam
parents:
diff changeset
74
6e589f267c14 Uploaded
devteam
parents:
diff changeset
75 @args gff_parts: attribute field key
6e589f267c14 Uploaded
devteam
parents:
diff changeset
76 @type gff_parts: str
6e589f267c14 Uploaded
devteam
parents:
diff changeset
77 """
6e589f267c14 Uploaded
devteam
parents:
diff changeset
78 for t_id in ["transcript_id", "transcriptId", "proteinId"]:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
79 try:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
80 gff_parts["info"]["Parent"] = gff_parts["info"][t_id]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
81 break
6e589f267c14 Uploaded
devteam
parents:
diff changeset
82 except KeyError:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
83 pass
6e589f267c14 Uploaded
devteam
parents:
diff changeset
84 for g_id in ["gene_id", "geneid", "geneId", "name", "gene_name", "genename"]:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
85 try:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
86 gff_parts["info"]["GParent"] = gff_parts["info"][g_id]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
87 break
6e589f267c14 Uploaded
devteam
parents:
diff changeset
88 except KeyError:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
89 pass
6e589f267c14 Uploaded
devteam
parents:
diff changeset
90 ## TODO key words
6e589f267c14 Uploaded
devteam
parents:
diff changeset
91 for flat_name in ["Transcript", "CDS"]:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
92 if gff_parts["info"].has_key(flat_name):
6e589f267c14 Uploaded
devteam
parents:
diff changeset
93 # parents
6e589f267c14 Uploaded
devteam
parents:
diff changeset
94 if gff_parts['type'] in [flat_name] or re.search(r'transcript', gff_parts['type'], re.IGNORECASE):
6e589f267c14 Uploaded
devteam
parents:
diff changeset
95 if not gff_parts['id']:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
96 gff_parts['id'] = gff_parts['info'][flat_name][0]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
97 #gff_parts["info"]["ID"] = [gff_parts["id"]]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
98 # children
6e589f267c14 Uploaded
devteam
parents:
diff changeset
99 elif gff_parts["type"] in ["intron", "exon", "three_prime_UTR",
6e589f267c14 Uploaded
devteam
parents:
diff changeset
100 "coding_exon", "five_prime_UTR", "CDS", "stop_codon",
6e589f267c14 Uploaded
devteam
parents:
diff changeset
101 "start_codon"]:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
102 gff_parts["info"]["Parent"] = gff_parts["info"][flat_name]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
103 break
6e589f267c14 Uploaded
devteam
parents:
diff changeset
104 return gff_parts
6e589f267c14 Uploaded
devteam
parents:
diff changeset
105
6e589f267c14 Uploaded
devteam
parents:
diff changeset
106 def Parse(ga_file):
6e589f267c14 Uploaded
devteam
parents:
diff changeset
107 """
6e589f267c14 Uploaded
devteam
parents:
diff changeset
108 Parsing GFF/GTF file based on feature relationship, it takes the input file.
6e589f267c14 Uploaded
devteam
parents:
diff changeset
109
6e589f267c14 Uploaded
devteam
parents:
diff changeset
110 @args ga_file: input file name
6e589f267c14 Uploaded
devteam
parents:
diff changeset
111 @type ga_file: str
6e589f267c14 Uploaded
devteam
parents:
diff changeset
112 """
6e589f267c14 Uploaded
devteam
parents:
diff changeset
113 child_map = defaultdict(list)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
114 parent_map = dict()
6e589f267c14 Uploaded
devteam
parents:
diff changeset
115
6e589f267c14 Uploaded
devteam
parents:
diff changeset
116 ga_handle = utils.open_file(ga_file)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
117
6e589f267c14 Uploaded
devteam
parents:
diff changeset
118 for rec in ga_handle:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
119 rec = rec.strip('\n\r')
6e589f267c14 Uploaded
devteam
parents:
diff changeset
120
6e589f267c14 Uploaded
devteam
parents:
diff changeset
121 # skip empty line fasta identifier and commented line
6e589f267c14 Uploaded
devteam
parents:
diff changeset
122 if not rec or rec[0] in ['#', '>']:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
123 continue
6e589f267c14 Uploaded
devteam
parents:
diff changeset
124 # skip the genome sequence
6e589f267c14 Uploaded
devteam
parents:
diff changeset
125 if not re.search('\t', rec):
6e589f267c14 Uploaded
devteam
parents:
diff changeset
126 continue
6e589f267c14 Uploaded
devteam
parents:
diff changeset
127
6e589f267c14 Uploaded
devteam
parents:
diff changeset
128 parts = rec.split('\t')
6e589f267c14 Uploaded
devteam
parents:
diff changeset
129 assert len(parts) >= 8, rec
6e589f267c14 Uploaded
devteam
parents:
diff changeset
130
6e589f267c14 Uploaded
devteam
parents:
diff changeset
131 # process the attribute column (9th column)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
132 ftype, tags = attribute_tags(parts[-1])
6e589f267c14 Uploaded
devteam
parents:
diff changeset
133 if not tags: # skip the line if no attribute column.
6e589f267c14 Uploaded
devteam
parents:
diff changeset
134 continue
6e589f267c14 Uploaded
devteam
parents:
diff changeset
135
6e589f267c14 Uploaded
devteam
parents:
diff changeset
136 # extract fields
6e589f267c14 Uploaded
devteam
parents:
diff changeset
137 if parts[1]:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
138 tags["source"] = parts[1]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
139 if parts[7]:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
140 tags["phase"] = parts[7]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
141
6e589f267c14 Uploaded
devteam
parents:
diff changeset
142 gff_info = dict()
6e589f267c14 Uploaded
devteam
parents:
diff changeset
143 gff_info['info'] = dict(tags)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
144 gff_info["is_gff3"] = ftype
6e589f267c14 Uploaded
devteam
parents:
diff changeset
145 gff_info['chr'] = parts[0]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
146 gff_info['score'] = parts[5]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
147
6e589f267c14 Uploaded
devteam
parents:
diff changeset
148 if parts[3] and parts[4]:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
149 gff_info['location'] = [int(parts[3]) ,
6e589f267c14 Uploaded
devteam
parents:
diff changeset
150 int(parts[4])]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
151 gff_info['type'] = parts[2]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
152 gff_info['id'] = tags.get('ID', [''])[0]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
153 if parts[6] in ['?', '.']:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
154 parts[6] = None
6e589f267c14 Uploaded
devteam
parents:
diff changeset
155 gff_info['strand'] = parts[6]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
156
6e589f267c14 Uploaded
devteam
parents:
diff changeset
157 # key word according to the GFF spec.
6e589f267c14 Uploaded
devteam
parents:
diff changeset
158 # is_gff3 flag is false check this condition and get the attribute fields
6e589f267c14 Uploaded
devteam
parents:
diff changeset
159 if not ftype:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
160 gff_info = spec_features_keywd(gff_info)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
161
6e589f267c14 Uploaded
devteam
parents:
diff changeset
162 # link the feature relationships
6e589f267c14 Uploaded
devteam
parents:
diff changeset
163 if gff_info['info'].has_key('Parent'):
6e589f267c14 Uploaded
devteam
parents:
diff changeset
164 for p in gff_info['info']['Parent']:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
165 if p == gff_info['id']:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
166 gff_info['id'] = ''
6e589f267c14 Uploaded
devteam
parents:
diff changeset
167 break
6e589f267c14 Uploaded
devteam
parents:
diff changeset
168 rec_category = 'child'
6e589f267c14 Uploaded
devteam
parents:
diff changeset
169 elif gff_info['id']:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
170 rec_category = 'parent'
6e589f267c14 Uploaded
devteam
parents:
diff changeset
171 else:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
172 rec_category = 'record'
6e589f267c14 Uploaded
devteam
parents:
diff changeset
173
6e589f267c14 Uploaded
devteam
parents:
diff changeset
174 # depends on the record category organize the features
6e589f267c14 Uploaded
devteam
parents:
diff changeset
175 if rec_category == 'child':
6e589f267c14 Uploaded
devteam
parents:
diff changeset
176 for p in gff_info['info']['Parent']:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
177 # create the data structure based on source and feature id
6e589f267c14 Uploaded
devteam
parents:
diff changeset
178 child_map[(gff_info['chr'], gff_info['info']['source'], p)].append(
6e589f267c14 Uploaded
devteam
parents:
diff changeset
179 dict( type = gff_info['type'],
6e589f267c14 Uploaded
devteam
parents:
diff changeset
180 location = gff_info['location'],
6e589f267c14 Uploaded
devteam
parents:
diff changeset
181 strand = gff_info['strand'],
6e589f267c14 Uploaded
devteam
parents:
diff changeset
182 score = gff_info['score'],
6e589f267c14 Uploaded
devteam
parents:
diff changeset
183 ID = gff_info['id'],
6e589f267c14 Uploaded
devteam
parents:
diff changeset
184 gene_id = gff_info['info'].get('GParent', '')
6e589f267c14 Uploaded
devteam
parents:
diff changeset
185 ))
6e589f267c14 Uploaded
devteam
parents:
diff changeset
186 elif rec_category == 'parent':
6e589f267c14 Uploaded
devteam
parents:
diff changeset
187 parent_map[(gff_info['chr'], gff_info['info']['source'], gff_info['id'])] = dict(
6e589f267c14 Uploaded
devteam
parents:
diff changeset
188 type = gff_info['type'],
6e589f267c14 Uploaded
devteam
parents:
diff changeset
189 location = gff_info['location'],
6e589f267c14 Uploaded
devteam
parents:
diff changeset
190 strand = gff_info['strand'],
6e589f267c14 Uploaded
devteam
parents:
diff changeset
191 score = gff_info['score'],
6e589f267c14 Uploaded
devteam
parents:
diff changeset
192 name = tags.get('Name', [''])[0])
6e589f267c14 Uploaded
devteam
parents:
diff changeset
193 elif rec_category == 'record':
6e589f267c14 Uploaded
devteam
parents:
diff changeset
194 #TODO how to handle plain records?
6e589f267c14 Uploaded
devteam
parents:
diff changeset
195 c = 1
6e589f267c14 Uploaded
devteam
parents:
diff changeset
196 ga_handle.close()
6e589f267c14 Uploaded
devteam
parents:
diff changeset
197
6e589f267c14 Uploaded
devteam
parents:
diff changeset
198 # depends on file type create parent feature
6e589f267c14 Uploaded
devteam
parents:
diff changeset
199 if not ftype:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
200 parent_map, child_map = create_missing_feature_type(parent_map, child_map)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
201
6e589f267c14 Uploaded
devteam
parents:
diff changeset
202 # connecting parent child relations
6e589f267c14 Uploaded
devteam
parents:
diff changeset
203 # essentially the parent child features are here from any type of GTF/GFF2/GFF3 file
6e589f267c14 Uploaded
devteam
parents:
diff changeset
204 gene_mat = format_gene_models(parent_map, child_map)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
205
6e589f267c14 Uploaded
devteam
parents:
diff changeset
206 return gene_mat
6e589f267c14 Uploaded
devteam
parents:
diff changeset
207
6e589f267c14 Uploaded
devteam
parents:
diff changeset
208 def format_gene_models(parent_nf_map, child_nf_map):
6e589f267c14 Uploaded
devteam
parents:
diff changeset
209 """
6e589f267c14 Uploaded
devteam
parents:
diff changeset
210 Genarate GeneObject based on the parsed file contents
6e589f267c14 Uploaded
devteam
parents:
diff changeset
211
6e589f267c14 Uploaded
devteam
parents:
diff changeset
212 @args parent_nf_map: parent features with source and chromosome information
6e589f267c14 Uploaded
devteam
parents:
diff changeset
213 @type parent_nf_map: collections defaultdict
6e589f267c14 Uploaded
devteam
parents:
diff changeset
214 @args child_nf_map: transctipt and exon information are encoded
6e589f267c14 Uploaded
devteam
parents:
diff changeset
215 @type child_nf_map: collections defaultdict
6e589f267c14 Uploaded
devteam
parents:
diff changeset
216 """
6e589f267c14 Uploaded
devteam
parents:
diff changeset
217
6e589f267c14 Uploaded
devteam
parents:
diff changeset
218 g_cnt = 0
6e589f267c14 Uploaded
devteam
parents:
diff changeset
219 gene = np.zeros((len(parent_nf_map),), dtype = utils.init_gene())
6e589f267c14 Uploaded
devteam
parents:
diff changeset
220
6e589f267c14 Uploaded
devteam
parents:
diff changeset
221 for pkey, pdet in parent_nf_map.items():
6e589f267c14 Uploaded
devteam
parents:
diff changeset
222 # considering only gene features
6e589f267c14 Uploaded
devteam
parents:
diff changeset
223 #if not re.search(r'gene', pdet.get('type', '')):
6e589f267c14 Uploaded
devteam
parents:
diff changeset
224 # continue
6e589f267c14 Uploaded
devteam
parents:
diff changeset
225
6e589f267c14 Uploaded
devteam
parents:
diff changeset
226 # infer the gene start and stop if not there in the
6e589f267c14 Uploaded
devteam
parents:
diff changeset
227 if not pdet.get('location', []):
6e589f267c14 Uploaded
devteam
parents:
diff changeset
228 GNS, GNE = [], []
6e589f267c14 Uploaded
devteam
parents:
diff changeset
229 # multiple number of transcripts
6e589f267c14 Uploaded
devteam
parents:
diff changeset
230 for L1 in child_nf_map[pkey]:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
231 GNS.append(L1.get('location', [])[0])
6e589f267c14 Uploaded
devteam
parents:
diff changeset
232 GNE.append(L1.get('location', [])[1])
6e589f267c14 Uploaded
devteam
parents:
diff changeset
233 GNS.sort()
6e589f267c14 Uploaded
devteam
parents:
diff changeset
234 GNE.sort()
6e589f267c14 Uploaded
devteam
parents:
diff changeset
235 pdet['location'] = [GNS[0], GNE[-1]]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
236
6e589f267c14 Uploaded
devteam
parents:
diff changeset
237 orient = pdet.get('strand', '')
6e589f267c14 Uploaded
devteam
parents:
diff changeset
238 gene[g_cnt]['id'] = g_cnt +1
6e589f267c14 Uploaded
devteam
parents:
diff changeset
239 gene[g_cnt]['chr'] = pkey[0]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
240 gene[g_cnt]['source'] = pkey[1]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
241 gene[g_cnt]['name'] = pkey[-1]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
242 gene[g_cnt]['start'] = pdet.get('location', [])[0]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
243 gene[g_cnt]['stop'] = pdet.get('location', [])[1]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
244 gene[g_cnt]['strand'] = orient
6e589f267c14 Uploaded
devteam
parents:
diff changeset
245 gene[g_cnt]['score'] = pdet.get('score','')
6e589f267c14 Uploaded
devteam
parents:
diff changeset
246
6e589f267c14 Uploaded
devteam
parents:
diff changeset
247 # default value
6e589f267c14 Uploaded
devteam
parents:
diff changeset
248 gene[g_cnt]['is_alt_spliced'] = gene[g_cnt]['is_alt'] = 0
6e589f267c14 Uploaded
devteam
parents:
diff changeset
249 if len(child_nf_map[pkey]) > 1:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
250 gene[g_cnt]['is_alt_spliced'] = gene[g_cnt]['is_alt'] = 1
6e589f267c14 Uploaded
devteam
parents:
diff changeset
251
6e589f267c14 Uploaded
devteam
parents:
diff changeset
252 # complete sub-feature for all transcripts
6e589f267c14 Uploaded
devteam
parents:
diff changeset
253 dim = len(child_nf_map[pkey])
6e589f267c14 Uploaded
devteam
parents:
diff changeset
254 TRS = np.zeros((dim,), dtype=np.object)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
255 TR_TYP = np.zeros((dim,), dtype=np.object)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
256 EXON = np.zeros((dim,), dtype=np.object)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
257 UTR5 = np.zeros((dim,), dtype=np.object)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
258 UTR3 = np.zeros((dim,), dtype=np.object)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
259 CDS = np.zeros((dim,), dtype=np.object)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
260 TISc = np.zeros((dim,), dtype=np.object)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
261 TSSc = np.zeros((dim,), dtype=np.object)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
262 CLV = np.zeros((dim,), dtype=np.object)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
263 CSTOP = np.zeros((dim,), dtype=np.object)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
264 TSTAT = np.zeros((dim,), dtype=np.object)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
265
6e589f267c14 Uploaded
devteam
parents:
diff changeset
266 # fetching corresponding transcripts
6e589f267c14 Uploaded
devteam
parents:
diff changeset
267 for xq, Lv1 in enumerate(child_nf_map[pkey]):
6e589f267c14 Uploaded
devteam
parents:
diff changeset
268
6e589f267c14 Uploaded
devteam
parents:
diff changeset
269 TID = Lv1.get('ID', '')
6e589f267c14 Uploaded
devteam
parents:
diff changeset
270 TRS[xq]= np.array([TID])
6e589f267c14 Uploaded
devteam
parents:
diff changeset
271
6e589f267c14 Uploaded
devteam
parents:
diff changeset
272 TYPE = Lv1.get('type', '')
6e589f267c14 Uploaded
devteam
parents:
diff changeset
273 TR_TYP[xq] = np.array('')
6e589f267c14 Uploaded
devteam
parents:
diff changeset
274 TR_TYP[xq] = np.array(TYPE) if TYPE else TR_TYP[xq]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
275
6e589f267c14 Uploaded
devteam
parents:
diff changeset
276 orient = Lv1.get('strand', '')
6e589f267c14 Uploaded
devteam
parents:
diff changeset
277
6e589f267c14 Uploaded
devteam
parents:
diff changeset
278 # fetching different sub-features
6e589f267c14 Uploaded
devteam
parents:
diff changeset
279 child_feat = defaultdict(list)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
280 for Lv2 in child_nf_map[(pkey[0], pkey[1], TID)]:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
281 E_TYP = Lv2.get('type', '')
6e589f267c14 Uploaded
devteam
parents:
diff changeset
282 child_feat[E_TYP].append(Lv2.get('location'))
6e589f267c14 Uploaded
devteam
parents:
diff changeset
283
6e589f267c14 Uploaded
devteam
parents:
diff changeset
284 # make general ascending order of coordinates
6e589f267c14 Uploaded
devteam
parents:
diff changeset
285 if orient == '-':
6e589f267c14 Uploaded
devteam
parents:
diff changeset
286 for etype, excod in child_feat.items():
6e589f267c14 Uploaded
devteam
parents:
diff changeset
287 if len(excod) > 1:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
288 if excod[0][0] > excod[-1][0]:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
289 excod.reverse()
6e589f267c14 Uploaded
devteam
parents:
diff changeset
290 child_feat[etype] = excod
6e589f267c14 Uploaded
devteam
parents:
diff changeset
291
6e589f267c14 Uploaded
devteam
parents:
diff changeset
292 # make exon coordinate from cds and utr regions
6e589f267c14 Uploaded
devteam
parents:
diff changeset
293 if not child_feat.get('exon'):
6e589f267c14 Uploaded
devteam
parents:
diff changeset
294 if child_feat.get('CDS'):
6e589f267c14 Uploaded
devteam
parents:
diff changeset
295 exon_cod = utils.make_Exon_cod( orient,
6e589f267c14 Uploaded
devteam
parents:
diff changeset
296 NonetoemptyList(child_feat.get('five_prime_UTR')),
6e589f267c14 Uploaded
devteam
parents:
diff changeset
297 NonetoemptyList(child_feat.get('CDS')),
6e589f267c14 Uploaded
devteam
parents:
diff changeset
298 NonetoemptyList(child_feat.get('three_prime_UTR')))
6e589f267c14 Uploaded
devteam
parents:
diff changeset
299 child_feat['exon'] = exon_cod
6e589f267c14 Uploaded
devteam
parents:
diff changeset
300 else:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
301 # TODO only UTR's
6e589f267c14 Uploaded
devteam
parents:
diff changeset
302 # searching through keys to find a pattern describing exon feature
6e589f267c14 Uploaded
devteam
parents:
diff changeset
303 ex_key_pattern = [k for k in child_feat if k.endswith("exon")]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
304 if ex_key_pattern:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
305 child_feat['exon'] = child_feat[ex_key_pattern[0]]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
306
6e589f267c14 Uploaded
devteam
parents:
diff changeset
307 # stop_codon are seperated from CDS, add the coordinates based on strand
6e589f267c14 Uploaded
devteam
parents:
diff changeset
308 if child_feat.get('stop_codon'):
6e589f267c14 Uploaded
devteam
parents:
diff changeset
309 if orient == '+':
6e589f267c14 Uploaded
devteam
parents:
diff changeset
310 if child_feat.get('stop_codon')[0][0] - child_feat.get('CDS')[-1][1] == 1:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
311 child_feat['CDS'][-1] = [child_feat.get('CDS')[-1][0], child_feat.get('stop_codon')[0][1]]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
312 else:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
313 child_feat['CDS'].append(child_feat.get('stop_codon')[0])
6e589f267c14 Uploaded
devteam
parents:
diff changeset
314 elif orient == '-':
6e589f267c14 Uploaded
devteam
parents:
diff changeset
315 if child_feat.get('CDS')[0][0] - child_feat.get('stop_codon')[0][1] == 1:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
316 child_feat['CDS'][0] = [child_feat.get('stop_codon')[0][0], child_feat.get('CDS')[0][1]]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
317 else:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
318 child_feat['CDS'].insert(0, child_feat.get('stop_codon')[0])
6e589f267c14 Uploaded
devteam
parents:
diff changeset
319
6e589f267c14 Uploaded
devteam
parents:
diff changeset
320 # transcript signal sites
6e589f267c14 Uploaded
devteam
parents:
diff changeset
321 TIS, cdsStop, TSS, cleave = [], [], [], []
6e589f267c14 Uploaded
devteam
parents:
diff changeset
322 cds_status, exon_status, utr_status = 0, 0, 0
6e589f267c14 Uploaded
devteam
parents:
diff changeset
323
6e589f267c14 Uploaded
devteam
parents:
diff changeset
324 if child_feat.get('exon'):
6e589f267c14 Uploaded
devteam
parents:
diff changeset
325 TSS = [child_feat.get('exon')[-1][1]]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
326 TSS = [child_feat.get('exon')[0][0]] if orient == '+' else TSS
6e589f267c14 Uploaded
devteam
parents:
diff changeset
327 cleave = [child_feat.get('exon')[0][0]]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
328 cleave = [child_feat.get('exon')[-1][1]] if orient == '+' else cleave
6e589f267c14 Uploaded
devteam
parents:
diff changeset
329 exon_status = 1
6e589f267c14 Uploaded
devteam
parents:
diff changeset
330
6e589f267c14 Uploaded
devteam
parents:
diff changeset
331 if child_feat.get('CDS'):
6e589f267c14 Uploaded
devteam
parents:
diff changeset
332 if orient == '+':
6e589f267c14 Uploaded
devteam
parents:
diff changeset
333 TIS = [child_feat.get('CDS')[0][0]]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
334 cdsStop = [child_feat.get('CDS')[-1][1]-3]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
335 else:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
336 TIS = [child_feat.get('CDS')[-1][1]]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
337 cdsStop = [child_feat.get('CDS')[0][0]+3]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
338 cds_status = 1
6e589f267c14 Uploaded
devteam
parents:
diff changeset
339 # cds phase calculation
6e589f267c14 Uploaded
devteam
parents:
diff changeset
340 child_feat['CDS'] = utils.add_CDS_phase(orient, child_feat.get('CDS'))
6e589f267c14 Uploaded
devteam
parents:
diff changeset
341
6e589f267c14 Uploaded
devteam
parents:
diff changeset
342 # sub-feature status
6e589f267c14 Uploaded
devteam
parents:
diff changeset
343 if child_feat.get('three_prime_UTR') or child_feat.get('five_prime_UTR'):
6e589f267c14 Uploaded
devteam
parents:
diff changeset
344 utr_status =1
6e589f267c14 Uploaded
devteam
parents:
diff changeset
345
6e589f267c14 Uploaded
devteam
parents:
diff changeset
346 if utr_status == cds_status == exon_status == 1:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
347 t_status = 1
6e589f267c14 Uploaded
devteam
parents:
diff changeset
348 else:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
349 t_status = 0
6e589f267c14 Uploaded
devteam
parents:
diff changeset
350
6e589f267c14 Uploaded
devteam
parents:
diff changeset
351 # add sub-feature # make array for export to different out
6e589f267c14 Uploaded
devteam
parents:
diff changeset
352 TSTAT[xq] = t_status
6e589f267c14 Uploaded
devteam
parents:
diff changeset
353 EXON[xq] = np.array(child_feat.get('exon'), np.float64)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
354 UTR5[xq] = np.array(NonetoemptyList(child_feat.get('five_prime_UTR')))
6e589f267c14 Uploaded
devteam
parents:
diff changeset
355 UTR3[xq] = np.array(NonetoemptyList(child_feat.get('three_prime_UTR')))
6e589f267c14 Uploaded
devteam
parents:
diff changeset
356 CDS[xq] = np.array(NonetoemptyList(child_feat.get('CDS')))
6e589f267c14 Uploaded
devteam
parents:
diff changeset
357 TISc[xq] = np.array(TIS)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
358 CSTOP[xq] = np.array(cdsStop)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
359 TSSc[xq] = np.array(TSS)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
360 CLV[xq] = np.array(cleave)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
361
6e589f267c14 Uploaded
devteam
parents:
diff changeset
362 # add sub-features to the parent gene feature
6e589f267c14 Uploaded
devteam
parents:
diff changeset
363 gene[g_cnt]['transcript_status'] = TSTAT
6e589f267c14 Uploaded
devteam
parents:
diff changeset
364 gene[g_cnt]['transcripts'] = TRS
6e589f267c14 Uploaded
devteam
parents:
diff changeset
365 gene[g_cnt]['exons'] = EXON
6e589f267c14 Uploaded
devteam
parents:
diff changeset
366 gene[g_cnt]['utr5_exons'] = UTR5
6e589f267c14 Uploaded
devteam
parents:
diff changeset
367 gene[g_cnt]['cds_exons'] = CDS
6e589f267c14 Uploaded
devteam
parents:
diff changeset
368 gene[g_cnt]['utr3_exons'] = UTR3
6e589f267c14 Uploaded
devteam
parents:
diff changeset
369 gene[g_cnt]['transcript_type'] = TR_TYP
6e589f267c14 Uploaded
devteam
parents:
diff changeset
370 gene[g_cnt]['tis'] = TISc
6e589f267c14 Uploaded
devteam
parents:
diff changeset
371 gene[g_cnt]['cdsStop'] = CSTOP
6e589f267c14 Uploaded
devteam
parents:
diff changeset
372 gene[g_cnt]['tss'] = TSSc
6e589f267c14 Uploaded
devteam
parents:
diff changeset
373 gene[g_cnt]['cleave'] = CLV
6e589f267c14 Uploaded
devteam
parents:
diff changeset
374
6e589f267c14 Uploaded
devteam
parents:
diff changeset
375 gene[g_cnt]['gene_info'] = dict( ID = pkey[-1],
6e589f267c14 Uploaded
devteam
parents:
diff changeset
376 Name = pdet.get('name'),
6e589f267c14 Uploaded
devteam
parents:
diff changeset
377 Source = pkey[1])
6e589f267c14 Uploaded
devteam
parents:
diff changeset
378 # few empty fields // TODO fill this:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
379 gene[g_cnt]['anno_id'] = []
6e589f267c14 Uploaded
devteam
parents:
diff changeset
380 gene[g_cnt]['confgenes_id'] = []
6e589f267c14 Uploaded
devteam
parents:
diff changeset
381 gene[g_cnt]['alias'] = ''
6e589f267c14 Uploaded
devteam
parents:
diff changeset
382 gene[g_cnt]['name2'] = []
6e589f267c14 Uploaded
devteam
parents:
diff changeset
383 gene[g_cnt]['chr_num'] = []
6e589f267c14 Uploaded
devteam
parents:
diff changeset
384 gene[g_cnt]['paralogs'] = []
6e589f267c14 Uploaded
devteam
parents:
diff changeset
385 gene[g_cnt]['transcript_info'] = []
6e589f267c14 Uploaded
devteam
parents:
diff changeset
386 gene[g_cnt]['transcript_valid'] = []
6e589f267c14 Uploaded
devteam
parents:
diff changeset
387 gene[g_cnt]['exons_confirmed'] = []
6e589f267c14 Uploaded
devteam
parents:
diff changeset
388 gene[g_cnt]['tis_conf'] = []
6e589f267c14 Uploaded
devteam
parents:
diff changeset
389 gene[g_cnt]['tis_info'] = []
6e589f267c14 Uploaded
devteam
parents:
diff changeset
390 gene[g_cnt]['cdsStop_conf'] = []
6e589f267c14 Uploaded
devteam
parents:
diff changeset
391 gene[g_cnt]['cdsStop_info'] = []
6e589f267c14 Uploaded
devteam
parents:
diff changeset
392 gene[g_cnt]['tss_info'] = []
6e589f267c14 Uploaded
devteam
parents:
diff changeset
393 gene[g_cnt]['tss_conf'] = []
6e589f267c14 Uploaded
devteam
parents:
diff changeset
394 gene[g_cnt]['cleave_info'] = []
6e589f267c14 Uploaded
devteam
parents:
diff changeset
395 gene[g_cnt]['cleave_conf'] = []
6e589f267c14 Uploaded
devteam
parents:
diff changeset
396 gene[g_cnt]['polya_info'] = []
6e589f267c14 Uploaded
devteam
parents:
diff changeset
397 gene[g_cnt]['polya_conf'] = []
6e589f267c14 Uploaded
devteam
parents:
diff changeset
398 gene[g_cnt]['is_valid'] = []
6e589f267c14 Uploaded
devteam
parents:
diff changeset
399 gene[g_cnt]['transcript_complete'] = []
6e589f267c14 Uploaded
devteam
parents:
diff changeset
400 gene[g_cnt]['is_complete'] = []
6e589f267c14 Uploaded
devteam
parents:
diff changeset
401 gene[g_cnt]['is_correctly_gff3_referenced'] = ''
6e589f267c14 Uploaded
devteam
parents:
diff changeset
402 gene[g_cnt]['splicegraph'] = []
6e589f267c14 Uploaded
devteam
parents:
diff changeset
403 g_cnt += 1
6e589f267c14 Uploaded
devteam
parents:
diff changeset
404
6e589f267c14 Uploaded
devteam
parents:
diff changeset
405 ## deleting empty gene records from the main array
6e589f267c14 Uploaded
devteam
parents:
diff changeset
406 XPFLG=0
6e589f267c14 Uploaded
devteam
parents:
diff changeset
407 for XP, ens in enumerate(gene):
6e589f267c14 Uploaded
devteam
parents:
diff changeset
408 if ens[0]==0:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
409 XPFLG=1
6e589f267c14 Uploaded
devteam
parents:
diff changeset
410 break
6e589f267c14 Uploaded
devteam
parents:
diff changeset
411
6e589f267c14 Uploaded
devteam
parents:
diff changeset
412 if XPFLG==1:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
413 XQC = range(XP, len(gene)+1)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
414 gene = np.delete(gene, XQC)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
415
6e589f267c14 Uploaded
devteam
parents:
diff changeset
416 return gene
6e589f267c14 Uploaded
devteam
parents:
diff changeset
417
6e589f267c14 Uploaded
devteam
parents:
diff changeset
418 def NonetoemptyList(XS):
6e589f267c14 Uploaded
devteam
parents:
diff changeset
419 """
6e589f267c14 Uploaded
devteam
parents:
diff changeset
420 Convert a None type to empty list
6e589f267c14 Uploaded
devteam
parents:
diff changeset
421
6e589f267c14 Uploaded
devteam
parents:
diff changeset
422 @args XS: None type
6e589f267c14 Uploaded
devteam
parents:
diff changeset
423 @type XS: str
6e589f267c14 Uploaded
devteam
parents:
diff changeset
424 """
6e589f267c14 Uploaded
devteam
parents:
diff changeset
425 return [] if XS is None else XS
6e589f267c14 Uploaded
devteam
parents:
diff changeset
426
6e589f267c14 Uploaded
devteam
parents:
diff changeset
427 def create_missing_feature_type(p_feat, c_feat):
6e589f267c14 Uploaded
devteam
parents:
diff changeset
428 """
6e589f267c14 Uploaded
devteam
parents:
diff changeset
429 GFF/GTF file defines only child features. This function tries to create
6e589f267c14 Uploaded
devteam
parents:
diff changeset
430 the parent feature from the information provided in the attribute column.
6e589f267c14 Uploaded
devteam
parents:
diff changeset
431
6e589f267c14 Uploaded
devteam
parents:
diff changeset
432 example:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
433 chr21 hg19_knownGene exon 9690071 9690100 0.000000 + . gene_id "uc002zkg.1"; transcript_id "uc002zkg.1";
6e589f267c14 Uploaded
devteam
parents:
diff changeset
434 chr21 hg19_knownGene exon 9692178 9692207 0.000000 + . gene_id "uc021wgt.1"; transcript_id "uc021wgt.1";
6e589f267c14 Uploaded
devteam
parents:
diff changeset
435 chr21 hg19_knownGene exon 9711935 9712038 0.000000 + . gene_id "uc011abu.2"; transcript_id "uc011abu.2";
6e589f267c14 Uploaded
devteam
parents:
diff changeset
436
6e589f267c14 Uploaded
devteam
parents:
diff changeset
437 This function gets the parsed feature annotations.
6e589f267c14 Uploaded
devteam
parents:
diff changeset
438
6e589f267c14 Uploaded
devteam
parents:
diff changeset
439 @args p_feat: Parent feature map
6e589f267c14 Uploaded
devteam
parents:
diff changeset
440 @type p_feat: collections defaultdict
6e589f267c14 Uploaded
devteam
parents:
diff changeset
441 @args c_feat: Child feature map
6e589f267c14 Uploaded
devteam
parents:
diff changeset
442 @type c_feat: collections defaultdict
6e589f267c14 Uploaded
devteam
parents:
diff changeset
443 """
6e589f267c14 Uploaded
devteam
parents:
diff changeset
444
6e589f267c14 Uploaded
devteam
parents:
diff changeset
445 child_n_map = defaultdict(list)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
446 for fid, det in c_feat.items():
6e589f267c14 Uploaded
devteam
parents:
diff changeset
447 # get the details from grand child
6e589f267c14 Uploaded
devteam
parents:
diff changeset
448 GID = STRD = SCR = None
6e589f267c14 Uploaded
devteam
parents:
diff changeset
449 SPOS, EPOS = [], []
6e589f267c14 Uploaded
devteam
parents:
diff changeset
450 TYP = dict()
6e589f267c14 Uploaded
devteam
parents:
diff changeset
451 for gchild in det:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
452 GID = gchild.get('gene_id', [''])[0]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
453 SPOS.append(gchild.get('location', [])[0])
6e589f267c14 Uploaded
devteam
parents:
diff changeset
454 EPOS.append(gchild.get('location', [])[1])
6e589f267c14 Uploaded
devteam
parents:
diff changeset
455 STRD = gchild.get('strand', '')
6e589f267c14 Uploaded
devteam
parents:
diff changeset
456 SCR = gchild.get('score', '')
6e589f267c14 Uploaded
devteam
parents:
diff changeset
457 if gchild.get('type', '') == "gene": ## gencode GTF file has this problem
6e589f267c14 Uploaded
devteam
parents:
diff changeset
458 continue
6e589f267c14 Uploaded
devteam
parents:
diff changeset
459 TYP[gchild.get('type', '')] = 1
6e589f267c14 Uploaded
devteam
parents:
diff changeset
460 SPOS.sort()
6e589f267c14 Uploaded
devteam
parents:
diff changeset
461 EPOS.sort()
6e589f267c14 Uploaded
devteam
parents:
diff changeset
462
6e589f267c14 Uploaded
devteam
parents:
diff changeset
463 # infer transcript type
6e589f267c14 Uploaded
devteam
parents:
diff changeset
464 transcript_type = 'transcript'
6e589f267c14 Uploaded
devteam
parents:
diff changeset
465 transcript_type = 'mRNA' if TYP.get('CDS', '') or TYP.get('cds', '') else transcript_type
6e589f267c14 Uploaded
devteam
parents:
diff changeset
466
6e589f267c14 Uploaded
devteam
parents:
diff changeset
467 # gene id and transcript id are same
6e589f267c14 Uploaded
devteam
parents:
diff changeset
468 transcript_id = fid[-1]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
469 if GID == transcript_id:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
470 transcript_id = 'Transcript:' + str(GID)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
471
6e589f267c14 Uploaded
devteam
parents:
diff changeset
472 # level -1 feature type
6e589f267c14 Uploaded
devteam
parents:
diff changeset
473 p_feat[(fid[0], fid[1], GID)] = dict( type = 'gene',
6e589f267c14 Uploaded
devteam
parents:
diff changeset
474 location = [], ## infer location based on multiple transcripts
6e589f267c14 Uploaded
devteam
parents:
diff changeset
475 strand = STRD,
6e589f267c14 Uploaded
devteam
parents:
diff changeset
476 name = GID )
6e589f267c14 Uploaded
devteam
parents:
diff changeset
477 # level -2 feature type
6e589f267c14 Uploaded
devteam
parents:
diff changeset
478 child_n_map[(fid[0], fid[1], GID)].append(
6e589f267c14 Uploaded
devteam
parents:
diff changeset
479 dict( type = transcript_type,
6e589f267c14 Uploaded
devteam
parents:
diff changeset
480 location = [SPOS[0], EPOS[-1]],
6e589f267c14 Uploaded
devteam
parents:
diff changeset
481 strand = STRD,
6e589f267c14 Uploaded
devteam
parents:
diff changeset
482 score = SCR,
6e589f267c14 Uploaded
devteam
parents:
diff changeset
483 ID = transcript_id,
6e589f267c14 Uploaded
devteam
parents:
diff changeset
484 gene_id = '' ))
6e589f267c14 Uploaded
devteam
parents:
diff changeset
485 # reorganizing the grand child
6e589f267c14 Uploaded
devteam
parents:
diff changeset
486 for gchild in det:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
487 child_n_map[(fid[0], fid[1], transcript_id)].append(
6e589f267c14 Uploaded
devteam
parents:
diff changeset
488 dict( type = gchild.get('type', ''),
6e589f267c14 Uploaded
devteam
parents:
diff changeset
489 location = gchild.get('location'),
6e589f267c14 Uploaded
devteam
parents:
diff changeset
490 strand = gchild.get('strand'),
6e589f267c14 Uploaded
devteam
parents:
diff changeset
491 ID = gchild.get('ID'),
6e589f267c14 Uploaded
devteam
parents:
diff changeset
492 score = gchild.get('score'),
6e589f267c14 Uploaded
devteam
parents:
diff changeset
493 gene_id = '' ))
6e589f267c14 Uploaded
devteam
parents:
diff changeset
494 return p_feat, child_n_map
6e589f267c14 Uploaded
devteam
parents:
diff changeset
495