/
GenePredictionReader.py
executable file
·181 lines (171 loc) · 5.21 KB
/
GenePredictionReader.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
import sys, CommonDNA
#IN: reads in genemark input
#########gmm called with ./gmhmmp -m hmm/Escherichia_coli_K12.mod -o test_out -k -r ../blastulator/12568252 -a
#OUT: returns List of GenePrediction objects
class GenePrediction:
#attributes are seq, start, stop, len, similarORFs (list), alignment (obj or matrix?), syntaxscoring (list)
def __init__( self, name, sequence, start, stop, strand):
self.name = name
self.sequence = sequence
self.start = int(start)
self.stop = int(stop)
#self.len = stop-start
self.strand = strand
self.ncbiId = ""
def __str__(self):
return "\n".join([self.name, self.sequence, str(self.start), str(self.stop), self.strand])
class NCBIProteinEntry:
def __init__(self, header, sequence):
self.sequence = sequence
self.header = header
def __str__(self):
return "\n".join([self.sequence, self.header])
def FastaToGenePrediction(genemarkFasta):
contents = genemarkFasta.split("\n")
seq = ""
start = 0
stop = 0
strand = ""
for line in contents:
if line.startswith(">"):
fields = line.split("|")
name = fields[0]
strand = fields[3]
start = fields[4]
tmp = fields[5].split(" ")[0]
tmp2 = tmp.split("\t")[0]
stop = tmp2
else:
seq = seq+line
seq = seq.strip()
g = GenePrediction(name, seq, start, stop, strand)
g.description = "predicted"
return g
def ExtendNonMetORFs(predictions, DNAseq):
for p in predictions:
if (p.sequence[0] != 'M' and p.sequence[0] != 'V'): #starting amino acid is not M
#extend upstream in DNAseq until Met found
nextAA = ""
i=0
while(nextAA != 'M'):
nextcodon = ''
if p.strand == '+':
if p.start-i-3 < 0:
break
nextcodon = DNAseq[p.start-i-3 : p.start - i]
else:
if p.stop + i + 3 > len(DNAseq):
break
nextcodon = DNAseq[p.stop + i : p.stop + i + 3]
nextcodon= CommonDNA.reverseComplement(nextcodon)
nextAA = CommonDNA.translate(nextcodon)
if(nextAA == "_"):
break
i+=3
p.sequence = nextAA + p.sequence
if p.strand == "+":
p.start = p.start-i-3
else:
p.stop = p.stop + i + 3
return predictions
def ExtendTruncatedORFs(predictions, DNAseq):
#takes in a gene prediction, along with its homologs, and sees if there is an upstream sequence in the DNA that matches
for p in predictions:
if len(p.homologs) < 1:
#skip if no homologs
continue
else:
firstTenAA = ""
try:
firstTenAA = p.sequence[0:10]
except IndexError:
continue
else:
#find first ten AA in other homologs
for h in p.homologs:
offset = h.sequence.find(firstTenAA)
if h.sequence.find(firstTenAA) > 1:
#find extra sequence in our DNA input
inputMatch = ""
if p.strand == "+":
inputMatch = DNAseq[p.start: p.start - offset*3]
else:
inputMatch = DNAseq[p.stop: p.stop + offset*3]
inputMatch = CommonDNA.reverseComplement(inputMatch)
return predictions
def MergeOverlapping(geneA, geneB):
#assumed that geneA starts earlier than geneB
# add on beginning of A to B, and return that object
loc = geneA.sequence.find(geneB.sequence[:20])
geneB.sequence = geneA.sequence[:loc]+geneB.sequence
geneB.start = min(geneA.start, geneB.start)
geneB.stop = max(geneA.stop, geneB.stop)
return geneB
#want to take the union of list A and list B.
#rules for merging two together are
def MergeGenePredObjects(listA, listB):
results = []
allmembers = listA+listB
allmembers.sort(key= lambda x: len(x.sequence), reverse= True)
ctr = 0
ignore = []
for a in allmembers:
flag = False
y = ctr + 1
z = y
if ctr in ignore:
ctr = ctr+1
continue
for b in allmembers[y:]:
merge_flag = False
attempt_merge = False
if b.sequence in a.sequence:
merge_flag = True
#if they overlap, have the same frame, and on the same strand
elif ((a.start < b.start and b.start < a.stop)): #or (a.start > b.start and a.start < b.stop)):
attempt_merge = True
elif(a.start > b.start and a.start < b.stop):
attempt_merge = True
if attempt_merge:
if(b.sequence[:20] in a.sequence):
a = MergeOverlapping(a, b)
merge_flag = True
elif(a.sequence[:20] in b.sequence): #oppostie strand
a = MergeOverlapping(b, a)
merge_flag = True
if merge_flag:
ignore.append(z)
#flag = True #TODO: copy over the annotations for b onto a
if(a.description == "predicted"):
a.description = b.description
else:
if(b.description not in a.description):
a.description += " "+b.description
z = z +1
if not flag:
results.append(a)
ctr = ctr + 1
return results
def GenemarkReader(genemarkFile):
listOfGenePredictions = []
contents=open(genemarkFile)
buffer = ""
flag = False
for line in contents:
if flag == True and not(line.startswith(">")):
buffer = buffer+line.strip()
if line.startswith(">"):
if flag == True:
#this is the 2nd or 3rd or.. gene
#process the buffer and clear it
listOfGenePredictions.append(FastaToGenePrediction(buffer))
buffer = ""
buffer = buffer+line
flag = True
listOfGenePredictions.append(FastaToGenePrediction(buffer))
#for gp in listOfGenePredictions:
#print gp
#print "====================\n"
return listOfGenePredictions
#example usage, import and then call with
#GenemarkReader(sys.argv[1])