-
Notifications
You must be signed in to change notification settings - Fork 1
/
GTF2psix.py
357 lines (227 loc) · 13.2 KB
/
GTF2psix.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import numpy as np
import pandas as pd
import argparse
from tqdm import tqdm
import gzip
from gtfparse import read_gtf
'''
Process GTF annotation to create an index of cassette exons and constitutive introns compatible with Psix.
Author: Carlos F Buen Abad Najar
cfbuenabadn [at] berkeley [dot] edu
'''
description = 'Create cassette exon annotation compatible with Psix, from a GTF file.'
description += ' Usage: python GTF2psix.py --gtf <path/to/gtf> [optional arguments]'
parser = argparse.ArgumentParser(description=description)
parser.add_argument('--gtf', type=str, required=True, help='GTF file to convert.')
parser.add_argument('-o', '--output_name', type=str, required=False, default='psix_annotation', help='Name for output file. Psix will add a .tab.gz extension')
parser.add_argument('-g', '--gene_type', type=str, required=False, default='all', help='Type of genes to include in annotation. E.g.: protein_coding.')
parser.add_argument('-n', '--gene_name', type=str, required=False, default='gene_id', help='Which gene identifier should be used to create the annotation. Use "gene_id" (if available in your gtf file) to create annotation using ensembl IDs. Use "gene_name" to create annotation with gene symbols. If none of these tags are present in your GTF file, you should indicate the tag in the GTF that you want to use.')
parser.add_argument('-trim', '--no_trim_id', action='store_true', required=False, help='Indicate if the gene ID should be trimmed. Some annotations (such as Gencode) have the following format for their ensembl id: "ENS0000001234.1". Passing this argument will trim the id in the annotation to "ENS0000001234". The default is to trim the annotation. If the annotation does not need trimming, it will not have an effect.')
parser.add_argument('-gt', '--gene_type_tag', type=str, required=False, default='gene_type', help='Indicate the tag to be used for gene type. By default we use "gene_type". Some annotations use "gene_biotype".')
parser.add_argument('-tt', '--transcript_type_tag', type=str, required=False, default='transcript_type', help='Indicate the tag to be used for transcript type. By default we use "transcript_type". Some annotations use "transcript_biotype".')
parser.add_argument('-e', '--exclude_chromosome', type=str, required=False, default='', help='Chromosomes to exclude, separated by comma. E.g.: chrX,chrY,chrM')
def process_gtf(gtf_file, exclude, gene_name, no_trim_id, gene_type_tag, transcript_type_tag):
print('Processing GTF file...')
exclude_chromosomes = exclude.split(',')
gtf = read_gtf(gtf_file)
try:
try:
cols = ['seqname', 'start', 'end', 'strand', 'transcript_id', gene_type_tag, gene_name, transcript_type_tag]
gtf = gtf.loc[pd.Series([x not in exclude_chromosomes for x in gtf.seqname]) & (gtf.feature == 'exon'), cols]
gtf.columns = ['chrom', 'start', 'end', 'strand', 'transcript', 'gene_type', 'gene', 'transcript_type']
except:
cols = ['seqname', 'start', 'end', 'strand', 'transcript_id', gene_name]
gtf = gtf.loc[pd.Series([x not in exclude_chromosomes for x in gtf.seqname]) & (gtf.feature == 'exon'), cols]
gtf.columns = ['chrom', 'start', 'end', 'strand', 'transcript', 'gene']
except:
raise Exception('Isufficient information to create annotation. transcript_id is needed to find cassette exons.')
if not no_trim_id:
gtf['gene'] = gtf.gene.str.replace(r'\.\d+$', '', regex=True)
gtf.transcript = [x.split('.')[0] for x in gtf.transcript]
print('Finished processing GTF file')
gtf = gtf.loc[gtf.gene != '']
return gtf
def get_dirs(gtf_gene, gene):
donor_dir = {}
acceptor_dir = {}
old_transcript = ''
for idx, row in gtf_gene.sort_values(['transcript', 'start', 'end']).iterrows():
transcript = row.transcript
if old_transcript != transcript:
counter = 1
old_transcript = transcript
else:
transcript_e = transcript + '.' + str(counter)
acceptor = str(int(row.start)-1)
if not acceptor in acceptor_dir.keys():
acceptor_dir.update({acceptor:{donor:[transcript_e]}})
elif not donor in acceptor_dir[acceptor].keys():
acceptor_dir[acceptor].update({donor:[transcript_e]})
else:
acceptor_dir[acceptor][donor].append(transcript_e)
if acceptor in donor_dir[donor].keys():
donor_dir[donor][acceptor].append(transcript_e)
else:
donor_dir[donor].update({acceptor:[transcript_e]})
counter += 1
donor = str(int(row.end)+1)
if not donor in donor_dir.keys():
#print('repetido')
donor_dir.update({donor:{}})
return donor_dir, acceptor_dir
def get_const_introns(fh, gtf):
try:
pc_gtf = gtf.loc[(gtf.transcript_type == 'protein_coding') & (gtf.gene_type == 'protein_coding')]
except:
pc_gtf = gtf
for i in tqdm(pc_gtf.groupby('gene'), leave=True, position=0):
t_gtf = i[1]
transcripts = t_gtf.transcript.unique()
donor_dir, acceptor_dir = get_dirs(t_gtf, transcripts[0])
introns_list = []
for donor in donor_dir:
if len(donor_dir[donor].keys()) > 0:
acceptor = list(donor_dir[donor].keys())[0]
intron = str(t_gtf.chrom.unique()[0])
intron += ':' + str(donor)
intron += ':' + str(acceptor)
intron += ':' + str(t_gtf.strand.unique()[0])
introns_list.append(intron)
for t in transcripts:
t_gtf = i[1].loc[i[1].transcript == t]
introns = []
donor_dir, acceptor_dir = get_dirs(t_gtf, transcripts[0])
for donor in donor_dir:
if len(donor_dir[donor].keys()) > 0:
acceptor = list(donor_dir[donor].keys())[0]
intron = str(t_gtf.chrom.unique()[0])
intron += ':' + str(donor)
intron += ':' + str(acceptor)
intron += ':' + str(t_gtf.strand.unique()[0])
introns.append(intron)
introns_list = [x for x in introns_list if x in introns]
intron_counts = 1
if len(introns_list) > 0:
for intron in introns_list:
chrom, start, end, strand = intron.split(':')
intron_number = i[0] + '_' + str(intron_counts)
intron_name = intron_number + '_CI'
intron_loc = chrom + ':' + start + '-' + end + ':' + strand
linea = '\t'.join([intron_name, intron_loc, intron_number, i[0]]) + '\n'
fh.write(linea)
intron_counts += 1
fh.close()
def get_cassette_exons(gtf_gene, donor_dir, acceptor_dir):
ase_dir = {}
for donor in donor_dir.keys(): # Parse all donors
if len(donor_dir[donor].keys()) > 1: # Check for donors with more than one acceptor
acceptors = sorted(donor_dir[donor].keys()) # Sort them, so that we go in order for acceptors
for n in range(1, len(acceptors)): # start with the 2nd acceptor; we assume no exon inside 1st intron
i = acceptors[n] # parse through acceptors
intron_list = donor_dir[donor][i] # introns that end in the nth acceptor
for intron in intron_list: # parsing through all the introns
for j in acceptor_dir[i].keys(): # for each donor of the nth acceptor
if j == donor:
skipped_isoforms = [x for x in donor_dir[j][i] if x in acceptor_dir[i][j]]
if j != donor: # check one that is different
cassette_isoforms = []
for acceptor_isoform in acceptor_dir[i][j]:
split_intron = acceptor_isoform.split('.')
upstream_intron = split_intron[0] + '.' + str(int(split_intron[1]) - 1)
for acceptor_final in donor_dir[donor].keys():
if upstream_intron in donor_dir[donor][acceptor_final]:
I1 = donor + ':' + acceptor_final
I2 = j + ':' + i
SE = donor + ':' + i
evento = (I1, I2, SE)
cassette_isoforms.append(upstream_intron + ':' + acceptor_isoform)
nmd = upstream_intron.split('.')[0]
try:
nmd_type = gtf_gene.loc[gtf_gene.transcript == nmd].transcript_type.unique()[0]
except:
nmd_type = 'notype'
event_key = '|'.join(evento)
if event_key not in ase_dir.keys():
ase_dir.update({event_key:[nmd_type]})
else:
ase_dir[event_key].append(nmd_type)
return ase_dir
def process_gene_annotation(gtf, gene):
gtf_gene = gtf.loc[gtf.gene == gene]
donor_dir, acceptor_dir = get_dirs(gtf_gene, gene)
ase_dir = get_cassette_exons(gtf_gene, donor_dir, acceptor_dir)
chrom = list(gtf_gene.chrom)[0]
strand = list(gtf_gene.strand)[0]
event_sorted = sorted(ase_dir.keys())
gene_events = ''
notype_counts = 1
pc_counts = 1
nmd_counts = 1
other_counts = 1
for event in event_sorted:
if 'protein_coding' in ase_dir[event]:
event_name = gene + '_ProteinCoding_' + str(pc_counts)
pc_counts += 1
elif 'nonsense_mediated_decay' in ase_dir[event]:
event_name = gene + '_NMD_' + str(nmd_counts)
nmd_counts += 1
### modify the script here if you're interested in other tags
### elif 'your_tag' in ase_dir[event]:
### event_name = gene + '_YOURTAG_' + str(yourtag_counts)
elif 'notype' in ase_dir[event]:
event_name = gene + '_UNKNOWNTYPE_' + str(notype_counts)
notype_counts += 1
else:
event_name = gene + '_other_' + str(other_counts)
other_counts += 1
i1_start, i1_end = event.split('|')[0].split(':')
intron_loc = chrom + ':' + i1_start + '-' + i1_end + ':' + strand
i1_line = '\t'.join([event_name + '_I1', intron_loc, event_name, gene]) + '\n'
gene_events += i1_line
i2_start, i2_end = event.split('|')[1].split(':')
intron_loc = chrom + ':' + i2_start + '-' + i2_end + ':' + strand
i2_line = '\t'.join([event_name + '_I2', intron_loc, event_name, gene]) + '\n'
gene_events += i2_line
se_start, se_end = event.split('|')[2].split(':')
intron_loc = chrom + ':' + se_start + '-' + se_end + ':' + strand
se_line = '\t'.join([event_name + '_SE', intron_loc, event_name, gene]) + '\n'
gene_events += se_line
return gene_events
def write_annotation(gtf, out_file, gene_type = 'protein_coding'):
if gene_type != 'all':
print('Working on ' + gene_type + ' genes')
try:
gene_list = gtf.loc[gtf.gene_type == gene_type].gene.unique()
except:
raise Exception(
'Gene type not found in GTF file. Please make sure that gene_type annotation exists, or use "--gene_type all" instead.'
)
else:
print('Working on all genes')
gene_list = gtf.gene.unique()
fh = gzip.open(out_file + '.tab.gz', 'wt')
fh.write('\t'.join(['', 'intron', 'event', 'gene']) + '\n')
gene_counts = 1
total_genes = len(gene_list)
print('Working in a total of ' + str(total_genes))
for gene in tqdm(gene_list, leave=True, position=0):
rows = process_gene_annotation(gtf, gene)
fh.write(rows)
gene_counts += 1
print('Processed ' + str(gene_counts-1)+'/'+str(total_genes))
print('Getting constitutive introns')
get_const_introns(fh, gtf)
fh.close()
print('Finished writing annotation')
if __name__ == '__main__':
args = parser.parse_args()
gtf_file = args.gtf
output_name = args.output_name
gene_type = args.gene_type
gene_name = args.gene_name
no_trim_id = args.no_trim_id
gene_type_tag = args.gene_type_tag
transcript_type_tag = args.transcript_type_tag
exclude = args.exclude_chromosome
gtf = process_gtf(gtf_file, exclude, gene_name, no_trim_id, gene_type_tag, transcript_type_tag)
write_annotation(gtf, output_name, gene_type)