annotate gff_to_gtf.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 Program to convert data from GFF to GTF
6e589f267c14 Uploaded
devteam
parents:
diff changeset
4
6e589f267c14 Uploaded
devteam
parents:
diff changeset
5 Usage: python gff_to_gtf.py in.gff > out.gtf
6e589f267c14 Uploaded
devteam
parents:
diff changeset
6
6e589f267c14 Uploaded
devteam
parents:
diff changeset
7 Requirement:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
8 GFFParser.py: https://github.com/vipints/GFFtools-GX/blob/master/GFFParser.py
6e589f267c14 Uploaded
devteam
parents:
diff changeset
9
6e589f267c14 Uploaded
devteam
parents:
diff changeset
10 Copyright (C)
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 sys
6e589f267c14 Uploaded
devteam
parents:
diff changeset
17 import GFFParser
6e589f267c14 Uploaded
devteam
parents:
diff changeset
18
6e589f267c14 Uploaded
devteam
parents:
diff changeset
19 def printGTF(tinfo):
6e589f267c14 Uploaded
devteam
parents:
diff changeset
20 """
6e589f267c14 Uploaded
devteam
parents:
diff changeset
21 writing result file in GTF format
6e589f267c14 Uploaded
devteam
parents:
diff changeset
22
6e589f267c14 Uploaded
devteam
parents:
diff changeset
23 @args tinfo: parsed object from gff file
6e589f267c14 Uploaded
devteam
parents:
diff changeset
24 @type tinfo: numpy array
6e589f267c14 Uploaded
devteam
parents:
diff changeset
25 """
6e589f267c14 Uploaded
devteam
parents:
diff changeset
26
6e589f267c14 Uploaded
devteam
parents:
diff changeset
27 for ent1 in tinfo:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
28 for idx, tid in enumerate(ent1['transcripts']):
6e589f267c14 Uploaded
devteam
parents:
diff changeset
29
6e589f267c14 Uploaded
devteam
parents:
diff changeset
30 exons = ent1['exons'][idx]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
31 cds_exons = ent1['cds_exons'][idx]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
32
6e589f267c14 Uploaded
devteam
parents:
diff changeset
33 stop_codon = start_codon = ()
6e589f267c14 Uploaded
devteam
parents:
diff changeset
34
6e589f267c14 Uploaded
devteam
parents:
diff changeset
35 if ent1['strand'] == '+':
6e589f267c14 Uploaded
devteam
parents:
diff changeset
36 if cds_exons.any():
6e589f267c14 Uploaded
devteam
parents:
diff changeset
37 start_codon = (cds_exons[0][0], cds_exons[0][0]+2)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
38 stop_codon = (cds_exons[-1][1]-2, cds_exons[-1][1])
6e589f267c14 Uploaded
devteam
parents:
diff changeset
39 elif ent1['strand'] == '-':
6e589f267c14 Uploaded
devteam
parents:
diff changeset
40 if cds_exons.any():
6e589f267c14 Uploaded
devteam
parents:
diff changeset
41 start_codon = (cds_exons[-1][1]-2, cds_exons[-1][1])
6e589f267c14 Uploaded
devteam
parents:
diff changeset
42 stop_codon = (cds_exons[0][0], cds_exons[0][0]+2)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
43 else:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
44 print 'STRAND information missing - %s, skip the transcript - %s' % (ent1['strand'], tid[0])
6e589f267c14 Uploaded
devteam
parents:
diff changeset
45 pass
6e589f267c14 Uploaded
devteam
parents:
diff changeset
46
6e589f267c14 Uploaded
devteam
parents:
diff changeset
47 last_cds_cod = 0
6e589f267c14 Uploaded
devteam
parents:
diff changeset
48 for idz, ex_cod in enumerate(exons):
6e589f267c14 Uploaded
devteam
parents:
diff changeset
49
6e589f267c14 Uploaded
devteam
parents:
diff changeset
50 print '%s\t%s\texon\t%d\t%d\t.\t%s\t.\tgene_id "%s"; transcript_id "%s"; exon_number "%d"; gene_name "%s"; ' % (ent1['chr'], ent1['source'], ex_cod[0], ex_cod[1], ent1['strand'], ent1['name'], tid[0], idz+1, ent1['gene_info']['Name'])
6e589f267c14 Uploaded
devteam
parents:
diff changeset
51
6e589f267c14 Uploaded
devteam
parents:
diff changeset
52 if cds_exons.any():
6e589f267c14 Uploaded
devteam
parents:
diff changeset
53 try:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
54 print '%s\t%s\tCDS\t%d\t%d\t.\t%s\t%d\tgene_id "%s"; transcript_id "%s"; exon_number "%d"; gene_name "%s"; ' % (ent1['chr'], ent1['source'], cds_exons[idz][0], cds_exons[idz][1], ent1['strand'], cds_exons[idz][2], ent1['name'], tid[0], idz+1, ent1['gene_info']['Name'])
6e589f267c14 Uploaded
devteam
parents:
diff changeset
55 last_cds_cod = idz
6e589f267c14 Uploaded
devteam
parents:
diff changeset
56 except:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
57 pass
6e589f267c14 Uploaded
devteam
parents:
diff changeset
58
6e589f267c14 Uploaded
devteam
parents:
diff changeset
59 if idz == 0:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
60 print '%s\t%s\tstart_codon\t%d\t%d\t.\t%s\t%d\tgene_id "%s"; transcript_id "%s"; exon_number "%d"; gene_name "%s"; ' % (ent1['chr'], ent1['source'], start_codon[0], start_codon[1], ent1['strand'], cds_exons[idz][2], ent1['name'], tid[0], idz+1, ent1['gene_info']['Name'])
6e589f267c14 Uploaded
devteam
parents:
diff changeset
61
6e589f267c14 Uploaded
devteam
parents:
diff changeset
62 if stop_codon:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
63 print '%s\t%s\tstop_codon\t%d\t%d\t.\t%s\t%d\tgene_id "%s"; transcript_id "%s"; exon_number "%d"; gene_name "%s"; ' % (ent1['chr'], ent1['source'], stop_codon[0], stop_codon[1], ent1['strand'], cds_exons[last_cds_cod][2], ent1['name'], tid[0], idz+1, ent1['gene_info']['Name'])
6e589f267c14 Uploaded
devteam
parents:
diff changeset
64
6e589f267c14 Uploaded
devteam
parents:
diff changeset
65
6e589f267c14 Uploaded
devteam
parents:
diff changeset
66 if __name__ == "__main__":
6e589f267c14 Uploaded
devteam
parents:
diff changeset
67
6e589f267c14 Uploaded
devteam
parents:
diff changeset
68 try:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
69 gff_fname = sys.argv[1]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
70 except:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
71 print __doc__
6e589f267c14 Uploaded
devteam
parents:
diff changeset
72 sys.exit(-1)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
73
6e589f267c14 Uploaded
devteam
parents:
diff changeset
74 Transcriptdb = GFFParser.Parse(gff_fname)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
75
6e589f267c14 Uploaded
devteam
parents:
diff changeset
76 printGTF(Transcriptdb)