forked from albertwcheng/albert-bioinformatics-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindConstituteRegions.py
executable file
·196 lines (158 loc) · 5.65 KB
/
findConstituteRegions.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
#!/usr/bin/env python
'''
Copyright 2010 Wu Albert Cheng <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
'''
from sys import *
from getopt import getopt
def zeros(n):
L=[]
for i in range(0,n):
L.append(0)
return L
#intervals=[ (chr,start0,end1),....]
#returns chr, blockBounds, blockCounts
#where blockBounds = [ b1,b2,b3,b4,b5 ]. blocks (start0,end1) are (b1,b2),(b2,b3),...,(b4,b5)
#where blockCounts = [c1,c2,c3,c4] where c1 is for (b1,b2), c2 is for (b2,b3), etc
def getNonOverlappingBlocks(intervals):
blockBounds=set()
if len(intervals)==0:
print >> stderr,"no intervals provided"
raise ValueError
chrom=intervals[0][0]
#first pass:add all coords into blockBounds
for interval in intervals:
tchrom,start0,end1=interval[:3]
if tchrom!=chrom:
print >> stderr,"inconsistent chromosomes. was",chrom,"now",tchrom
raise ValueError
blockBounds.add(start0)
blockBounds.add(end1)
#now convert blockBounds into list and sort
blockBounds=list(blockBounds)
blockBounds.sort()
nBlocks=len(blockBounds)-1
blockCounts=zeros(nBlocks)
#second pass: go to each interval and count
for interval in intervals:
chrom,start0,end1=interval[:3]
try:
sI=blockBounds.index(start0)
except:
print >> stderr,"error start0=",start0,"not indexed"
print >> stderr,"blockBounds=",blockBounds
raise ValueError
try:
eI=blockBounds.index(end1)
except:
print >> stderr,"error end1=",end1,"not indexed"
print >> stderr,"blockBounds=",blockBounds
raise ValueError
for i in range(sI,eI):
blockCounts[i]+=1
return chrom,blockBounds,blockCounts
def toStrArray(L):
sL=[]
for x in L:
sL.append(str(x))
return sL
def printUsageAndExit(programName):
print >> stderr,"Usage",programName,"[options] inbed"
print >> stderr,"from a bed file of chr start0 end1, find regions that are represented at least a number of times"
print >> stderr,"Options:"
print >> stderr,"--block-count-min [default:1]. set block count min threshold to be retained in output"
print >> stderr,"--append-count. append a column of block count"
print >> stderr,"--out-one-line. output as one line transcript bed entry"
print >> stderr,"--ool-strand [default:.]. set strand for one line output"
print >> stderr,"--ool-name [default:.]. set name for one line output"
print >> stderr,"--ool-score [default: 0]. set score for one line output"
print >> stderr,"--ool-itemRgb [defualt: 0,0,0]. set itemRgb for one line output"
exit()
if __name__=='__main__':
#intervals=[ ('chr1',0,21), ('chr1',10,30), ('chr1',20,25) , ('chr1',50,100)]
#print getNonOverlappingBlocks(intervals)
programName=argv[0]
blockCountMin=1
appendCount=False
onelineBedOut=False
oolscore='0'
oolname='.'
oolstrand='.'
oolitemrgb="0,0,0"
opts,args=getopt(argv[1:],'',['block-count-min=','append-count','out-one-line','ool-score=','ool-name=','ool-strand=','ool-itemRgb='])
for o,v in opts:
if o=='--block-count-min':
blockCountMin=int(v)
elif o=='--append-count':
appendCount=True
elif o=='--out-one-line':
onelineBedOut=True
elif o=='--ool-score':
oolscore=v
elif o=='--ool-name':
oolname=v
elif o=='--ool-strand':
oolstrand=v
elif o=='--ool-itemRgb':
oolitemrgb=v
try:
inbed,=args
except:
printUsageAndExit(programName)
intervals=[]
fil=open(inbed)
for lin in fil:
lin=lin.rstrip("\r\n")
if len(lin)<1 or lin[0]=='#':
continue
fields=lin.split("\t")
try:
chrom,start0,end1=fields[:3]
interval=(chrom,int(start0),int(end1))
intervals.append(interval)
except:
print >> stderr,"invalid line",lino,"ignored:",fields
continue
fil.close()
#now getNonOverlappingBlocks
chrom,blockBounds,blockCounts=getNonOverlappingBlocks(intervals)
oneLineStarts0=[]
oneLineLengths=[]
lastBlockEnd1=-1
firstBlockStart0=-1
for i in range(0,len(blockCounts)):
blockStart0=blockBounds[i]
blockEnd1=blockBounds[i+1]
blockCount=blockCounts[i]
if blockCount>=blockCountMin:
#output!
if onelineBedOut:
if firstBlockStart0==-1:
firstBlockStart0=blockStart0
lastBlockEnd1=blockEnd1
oneLineStarts0.append(blockStart0-firstBlockStart0)
oneLineLengths.append(blockEnd1-blockStart0)
else:
outputV=[chrom,str(blockStart0),str(blockEnd1)]
if appendCount:
outputV.append(str(blockCount))
print >> stdout,"\t".join(outputV)
if onelineBedOut and len(oneLineStarts0)>0:
oneLineStarts0=toStrArray(oneLineStarts0)
oneLineLengths=toStrArray(oneLineLengths)
outputV=[chrom,str(firstBlockStart0),str(lastBlockEnd1),oolname,oolscore,oolstrand,str(firstBlockStart0),str(lastBlockEnd1),str(oolitemrgb),str(len(oneLineStarts0)),",".join(oneLineLengths),",".join(oneLineStarts0)]
print >> stdout,"\t".join(outputV)