-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathORFCoverage.py
196 lines (156 loc) · 6.23 KB
/
ORFCoverage.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
########################################################################################
## This file is a part of YAP package of scripts. https://github.com/shpakoo/YAP
## Distributed under the MIT license: http://www.opensource.org/licenses/mit-license.php
## Copyright (c) 2011-2013 Sebastian Szpakowski
########################################################################################
#################################################
## Coverage of ORF
#################################################
import sys
from optparse import OptionParser
from collections import defaultdict
_author="Sebastian Szpakowski"
_date="Jan 29, 2013"
_version="V1.00"
#################################################
## Classes
##
#################################################
### Iterator over input fasta file.
### Only reading when requested
### Useful for very large FASTA files
### with many sequences
class FastaParser:
def __init__ (self, x, quals=False):
self.filename = x
self.fp = open(x, "r")
self.currline = ""
self.currentFastaName = ""
self.currentFastaSequence = ""
self.lastitem=False
if quals:
self.linesep=" "
else:
self.linesep=""
def __iter__(self):
return(self)
#####
def next(self):
for self.currline in self.fp:
if self.currline.startswith(">"):
self.currline = self.currline[1:]
if self.currentFastaName == "":
self.currentFastaName = self.currline
else:
otpt = (self.currentFastaName.strip(), self.currentFastaSequence.strip())
self.currentFastaName = self.currline
self.currentFastaSequence = ""
self.previoustell = self.fp.tell()
return (otpt)
else:
self.addSequence(self.currline)
if not self.lastitem:
self.lastitem=True
return (self.currentFastaName.strip(), self.currentFastaSequence.strip())
else:
raise StopIteration
def addSequence(self, x):
self.currentFastaSequence = "%s%s%s" % (self.currentFastaSequence,self.linesep, x.strip())
def __str__():
return ("reading file: %s" %self.filename)
#################################################
### Iterator over input file.
### every line is converted into a dictionary with variables referred to by their
### header name
class GeneralPurposeParser:
def __init__(self, file, skip=0, sep="\t"):
self.filename = file
self.fp = open(self.filename, "rU")
self.sep = sep
self.linecounter = 0
self.currline=""
def __iter__(self):
return (self)
def next(self):
otpt = dict()
for currline in self.fp:
currline = currline.strip().split(self.sep)
self.currline = currline
self.linecounter = self.linecounter + 1
return(currline)
raise StopIteration
def __str__(self):
return "%s [%s]\n\t%s" % (self.filename, self.linecounter, self.currline)
#################################################
## Functions
##
def overlap(s,e,S,E):
return not (e<S or s>E)
#################################################
## Arguments
##
parser = OptionParser()
parser.add_option("-o", "--orf", dest="fn_orf",
help="ORFs", metavar="FILE")
parser.add_option("-a", "--anno", dest="fn_anno",
help="annotation", metavar="FILE")
parser.add_option("-e", "--outputfile", dest="fn_output",
help="output file", metavar="FILE")
#parser.add_option("-q", "--quiet",
# action="store_false", dest="verbose", default=True,
# help="don't print status messages to stdout")
(options, args) = parser.parse_args()
#################################################
## Begin
##
### check which ORFS don't need to have coverage recalculated.
needed = list()
lookup = defaultdict(list)
cache = defaultdict(float)
otpt = open(options.fn_output, "w")
for head, seq in FastaParser(options.fn_orf):
head = head.strip().split("_")
id, tid, length, reads, cov, start, end, strand = head
length = float(length)
start = float(start)
end = float(end)
if end - start > length-10 and end - start < length+10 :
otpt.write( "%s\t%s\n" % ( "_".join(head), cov.strip("]").strip("[").split("=")[-1]))
pass
else:
needed.append(int(tid))
lookup[int(tid)].append(("_".join(head), int(start), int(end)))
print "easy part done..."
#### cache reads for the "needed" contigs
# 0 SOLEXA4:32:C13WTACXX:1:1102:10260:39080/1 101 0 51 13360 6632 6683 0 1 0 51
# 1 SOLEXA4:32:C13WTACXX:1:1102:10260:39080/2 101 2 101 23163 254 353 1 1 0 99
for x in GeneralPurposeParser(options.fn_anno, sep=None):
tid = int(x[5])+1
if (float(x[0]) % 1000000 == 1):
print x
for curorf in lookup[tid]:
if overlap(int(x[6])+1,int(x[7])+1, curorf[1], curorf[2]):
cache[curorf[0]]+= (int(x[4]) - int(x[3]))
print curorf
print x
print cache[curorf[0]]
print
#
# if len(x)==12:
# qnum, qid, qlen, qstart, qend, tid, tstart, tend, qrev, qmatch, paired, score = x
# else:
# qnum, qid, qlen, qstart, qend, tid, tstart, tend, qrev, qmatch, score = x
# ### tid is a number, starting at 0!
# contigs[int(tid)+1] += 1
#
# process cache
for orfid in cache.keys():
orf = orfid.strip().split("_")
s = int(orf[-3])
e = int(orf[-2])
otpt.write( "%s\t%.2f\n" % (orfid, cache[orfid]/(e-s)) )
otpt.close()
print "\n"
#################################################
## Finish
#################################################