forked from bmajoros/python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFastbTrack.py
executable file
·232 lines (210 loc) · 7.3 KB
/
FastbTrack.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
#=========================================================================
# This is OPEN SOURCE SOFTWARE governed by the Gnu General Public
# License (GPL) version 3, as described at www.opensource.org.
# Copyright (C)2016 William H. Majoros ([email protected]).
#=========================================================================
from __future__ import (absolute_import, division, print_function,
unicode_literals, generators, nested_scopes, with_statement)
from builtins import (bytes, dict, int, list, object, range, str, ascii,
chr, hex, input, next, oct, open, pow, round, super, filter, map, zip)
from FastaWriter import FastaWriter
from Interval import Interval
######################################################################
#
# FastbTrack.py [email protected] 10/14/2016
#
# A track in a Fastb file.
#
# Attributes:
# type : "discrete" or "continuous"
# id : name of track
# data : string (for discrete) or array of float (for continuous)
# deflineExtra : extra info for defline
# Methods:
# track=FastbTrack(type,id,data,deflineExtra="") # type="discrete" or "continuous"
# type=track.getType()
# data=track.getData()
# track.setSequence(string) # discrete
# track.setData(values) # continuous
# id=track.getID()
# L=track.getLength()
# track.rename(newID)
# bool=track.isDiscrete()
# bool=track.isContinuous()
# track.save(FILEHANDLE)
#
# Methods for discrete tracks:
# getDiscreteContiguousRegions(self) # returns array of Interval with
# "value" attribute added
#
# Methods for continuous tracks:
# array=track.getNonzeroRegions() # returns array of Interval
# array=track.getZeroRegions() # returns array of Interval
# array=track.getRegionsAbove(cutoff) # returns array of Interval
# bool=track.anyZeroValues() # only for continuous tracks
# array=track.getContiguousRegions() # returns an array of Interval with
# "value" attribute added
# newTrack=track.slice(begin,end) # [begin,end) => end not inclusive
# meanValue=track.getMean(interval=None) # only for continuous data
# (maxValue,maxPos)=track.getMax(interval=None) # only for continuous data
######################################################################
class FastbTrack:
def __init__(self,type,id,data,deflineExtra=""):
self.type=type
self.id=id
self.data=data
self.deflineExtra=deflineExtra
def getType(self):
return self.type
def getData(self):
return self.data
def isDiscrete(self):
return self.type=="discrete"
def isContinuous(self):
return self.type=="continuous"
def getID(self):
return self.id
def save(self,fh):
writer=FastaWriter()
id=self.id
data=self.data
deflineExtra=self.deflineExtra
if(self.isDiscrete()):
writer.addToFasta(">"+id+" "+deflineExtra,data,fh)
else:
fh.write("%"+id+" "+deflineExtra+"\n")
n=len(data)
for i in range(0,n): fh.write(str(data[i])+"\n")
def getContiguousRegions(self):
"""getContiguousRegions() returns an array of Interval objects
with a "value" attribute added
"""
data=self.data
if(self.isDiscrete()): raise Exception("track is not continuous")
L=len(data)
intervals=[]
if(L==0): return intervals
begin=0
for i in range(1,L):
x=data[i]
prev=data[i-1]
if(x==prev): continue
interval=Interval(begin,i)
interval.value=prev
intervals.append(interval)
begin=i
interval=Interval(begin,L)
interval.value=data[L-1]
intervals.append(interval)
return intervals
def getDiscreteContiguousRegions(self):
"""getDiscreteContiguousRegions() returns an array of Interval objects
with a "value" attribute added
"""
data=self.data
if(not self.isDiscrete()): raise Exception("track is not discrete")
L=len(data)
intervals=[]
if(L==0): return intervals
begin=0
for i in range(1,L):
x=data[i]
prev=data[i-1]
if(x==prev): continue
interval=Interval(begin,i)
interval.value=prev
intervals.append(interval)
begin=i
interval=Interval(begin,L)
interval.value=data[L-1]
intervals.append(interval)
return intervals
def getZeroRegions(self):
"""getNonzeroRegions() returns array of Intervals"""
data=self.data
if(self.isDiscrete()): raise Exception("track is not continuous")
L=len(data)
intervals=[]
begin=None
for i in range(L):
x=data[i]
if(x==0 and (i==0 or data[i-1]!=0)): begin=i
elif(x!=0 and i>0 and data[i-1]==0):
intervals.append(Interval(begin,i))
if(L>0 and data[L-1]==0):
intervals.append(Interval(begin,L))
return intervals
def getNonzeroRegions(self):
"""getNoneroRegions() returns array of Intervals"""
data=self.data
if(self.isDiscrete()): raise Exception("track is not continuous")
L=len(data)
intervals=[]
begin=None
for i in range(0,L):
x=data[i]
if(x!=0 and (i==0 or data[i-1]==0)): begin=i
elif(x==0 and i>0 and data[i-1]!=0):
intervals.append(Interval(begin,i))
if(L>0 and data[L-1]!=0):
intervals.append(Interval(begin,L))
return intervals
def getRegionsAbove(self,cutoff):
"""getRegionsAbove() returns array of Intervals"""
data=self.data
if(self.isDiscrete()): raise Exception("track is not continuous")
L=len(data)
intervals=[]
begin=None
for i in range(0,L):
x=data[i]
if(x>cutoff and (i==0 or data[i-1]<=cutoff)): begin=i
elif(x<=cutoff and i>0 and data[i-1]>cutoff):
intervals.append(Interval(begin,i))
if(L>0 and data[L-1]>cutoff):
intervals.append(Interval(begin,L))
return intervals
def rename(self,newID):
self.id=newID
def getLength(self):
return len(self.data)
def slice(self,begin,end):
return FastbTrack(self.type,self.id,self.data[begin:end],
self.deflineExtra)
def setSequence(self,string):
self.data=string
def setData(self,values):
self.data=values
def getMean(self,interval=None):
data=self.data
L=len(data)
begin=0
end=L
if(interval is not None):
begin=interval.begin
end=interval.end
L=end-begin
sum=0.0
for i in range(begin,end): sum+=data[i]
mean=sum/L
return mean
def getMax(self,interval=None):
data=self.data
L=len(data)
begin=0
end=L
if(interval is not None):
begin=interval.begin
end=interval.end
L=end-begin
max=data[begin]; maxPos=begin
for i in range(begin+1,end):
if(data[i]>max):
max=data[i]
maxPos=i
return (max,maxPos)
def anyZeroValues(self):
data=self.data
for x in data:
if(x==0.0): return True
return False