-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgff_2_gtf.py
More file actions
126 lines (92 loc) · 3.99 KB
/
gff_2_gtf.py
File metadata and controls
126 lines (92 loc) · 3.99 KB
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
""" This python script will take a GFF as 1st cmd line arg and spit out a GTF with the same name as input GFF. """
import sys
import re
def read_gff(filename):
outfile_name = str(filename).replace("gff", "gtf")
outfile = open(outfile_name, "w")
file = open(filename, "r")
gtf = ""
for line in file:
column = line.rstrip().split("\t")
#two booleans to determine if line contains actual features for gtf
#or details specific to GFF3
if bool(re.search("#.*", line)):
continue
feature_line = ""
attribute = str(column[2])
if attribute == "region" :
reg = re.search("ID=(.*?);", column[8])
if reg:
feature_line += 'ID "' + str(reg.group(1)) + '"; '
nam = re.search(".*Name=(.*?);", column[8])
if nam:
feature_line += 'Name "' + str(nam.group(1)) + '"; '
chrom = re.search(".*chromosome=(.*?);", column[8])
if chrom:
feature_line += 'Chromosome "' + str(chrom.group(1)) + '"; '
elif attribute == "mRNA" :
mrna = re.search("ID=(.*?);", column[8])
if mrna:
feature_line += 'ID "' + str(mrna.group(1)) + '"; '
gbank = re.search(".*GeneID:(.*?),", column[8])
if gbank:
feature_line += 'gene_id "' + str(gbank.group(1)) + '"; '
nam = re.search(".*gene=(.*?);", column[8])
if nam:
feature_line += 'gene_name "' + str(nam.group(1)) + '"; '
trans = re.search(".*transcript_id=(.*?);", column[8])
if trans:
feature_line += 'transcript_id "' + str(trans.group(1)) + '"; '
else:
trans2 = re.search(".*transcript_id=(.*)", column[8])
if trans2:
feature_line += 'transcript_id "' + str(trans2.group(1)) + '"; '
else:
continue
elif attribute == "exon" :
gbank = re.search(".*GeneID:(.*?),", column[8])
if gbank:
feature_line += 'gene_id "' + str(gbank.group(1)) + '"; '
else:
gbank2 = re.search(".*GeneID:(.*)", column[8])
if gbank2:
feature_line += 'gene_id "' + str(gbank2.group(1)) + '"; '
else:
continue
trans = re.search(".*transcript_id=(.*?);", column[8])
if trans:
feature_line += 'transcript_id "' + str(trans.group(1)) + '"; '
else:
trans2 = re.search(".*transcript_id=(.*)", column[8])
if trans2:
feature_line += 'transcript_id "' + str(trans2.group(1)) + '"; '
else:
continue
elif attribute == "CDS" :
seq = re.search("ID=(.*?);", column[8])
if seq:
feature_line += 'ID "' + str(seq.group(1)) + '"; '
parent = re.search(".*Parent=(.*?);", column[8])
if parent:
feature_line += 'ID "' + str(parent.group(1)) + '"; '
gbank = re.search(".*GeneID:(.*?),", column[8])
if gbank:
feature_line += 'gene_id "' + str(gbank.group(1)) + '"; '
prot = re.search(".*protein_id=(.*?);", column[8])
if prot:
feature_line += 'protein_id "' + str(prot.group(1)) + '"; '
else:
prot2 = re.search(".*protein_id=(.*)", column[8])
if prot2:
feature_line += 'protein_id "' + str(prot2.group(1)) + '"; '
else:
continue
else:
continue
#formatting gene_id and transcript_id
attribute = column[0] + "\t" + column[1] + "\t" + column[2] + "\t" + column[3] + "\t" + column[4] + "\t" + column[5] + "\t" + column[6] + "\t" + column[7] + "\t" + feature_line + "\n"
gtf += attribute
outfile.write(gtf)
if __name__ == '__main__':
file = sys.argv[1]
read_gff(file)