-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcircosvis.py
354 lines (270 loc) · 9.5 KB
/
circosvis.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env python
#encoding:utf-8
"""
CircosVis.py
created by Liang Sun on 2016-10-13
Copyright (c) 2016 Liang Sun. All rights reserved.
Updated and maintained by Liang Sun since Oct 2016
CircosVis visualize small variance and large deletions
usage:
CircosVis [options] -c <control1[,control2,...]> -m <mutant1[,mutant2,...]> -o <output file>
example:
python CircosVis.py -l fnb/fnb.mt4_chr1_alldeletion_20x_annot.bed -o fnb_circos.png
"""
import sys
import os
import argparse
import time
def pickDeletionSize(count,lfile,minLen,maxLen):
#minLen = 100
while(count>25000):
minLen = minLen + 10
FileIN2 = open(lfile,"rU")
FileIN2.readline()
count = 0
for line in FileIN2:
data = line.strip().split("\t")
if int(data[4])>=minLen and int(data[4])<=maxLen:
count = count + 1
FileIN2.close()
return minLen
def writeDelTrackFile(trackfile,lfile,minLen,maxLen):
print "*****The minimum Length of big deletions to be visualized in track" + trackfile + ": "+str(minLen) + "bp"
FileOUT = open(trackfile,"w")
FileIN = open(lfile,"rU")
FileIN.readline()
for line in FileIN:
data = line.strip().split("\t")
if 'scaffold' in data[1]:
continue
if int(data[4]) < minLen:
continue
if int(data[4]) > minLen and int(data[4]) < maxLen:
FileOUT.write(data[1]+"\t"+data[2]+"\t"+data[3]+"\t"+data[4]+"\n")
FileIN.close()
FileOUT.close()
def writeDelTextFile(trackfile,lfile,minLen,maxLen):
print "*****Write text track to circos"
FileOUT = open(trackfile,"w")
FileIN = open(lfile,"rU")
FileIN.readline()
for line in FileIN:
data = line.strip().split("\t")
if 'scaffold' in data[1]:
continue
if len(data) < 12:
sys.exit("Input file error: Please provide deletion file with annotation for each deletion!")
if minLen < int(data[4]) <= maxLen and data[11] != "[]":
info = data[11].translate(None,"'[]").split(",")
for id in info:
FileOUT.write(data[1]+"\t"+data[2]+"\t"+data[3]+"\t"+id+"\n")
FileIN.close()
FileOUT.close()
def processDel(lfile):
FileIN1 = open(lfile,"rU")
#count = sum(1 for line in FileIN1)
FileIN1.readline()
count1 = 0
count100 = 0
count1000 = 0
for line in FileIN1:
data = line.strip().split("\t")
if int(data[3]) < 100:
count1 = count1 + 1
elif int(data[3]) < 1000:
count100 = count100 + 1
else:
count1000 = count1000 + 1
FileIN1.close()
# find the minimum length of deletion and wirte the deletion track files
# minLen1 = pickDeletionSize(count100,lfile,1,maxLen)
# writeDelTrackFile("vis/DelTrack1.txt",lfile,minLen100,maxLen)
minLen1 = pickDeletionSize(count100,lfile,1,100)
writeDelTrackFile("vis/DelTrack1.txt",lfile,minLen1,100)
#write text file
writeDelTextFile("vis/DelTrack1_text.txt",lfile,minLen1,100)
minLen100 = pickDeletionSize(count100,lfile,100,1000)
writeDelTrackFile("vis/DelTrack100.txt",lfile,minLen100,1000)
#write text file
writeDelTextFile("vis/DelTrack100_text.txt",lfile,minLen100,1000)
minLen1000 = pickDeletionSize(count1000,lfile,1000,1500000)
writeDelTrackFile("vis/DelTrack1000.txt",lfile,minLen1000,1500000)
#write text file
writeDelTextFile("vis/DelTrack1000_text.txt",lfile,minLen1000,1500000)
def createConf(ofile): #create in vis folder
tempt_start = """
# 1.2 IDEOGRAM LABELS, TICKS, AND MODULARIZING CONFIGURATION
karyotype = vis/karyotype.mt.txt
chromosomes_units = 1000000
<plots>
#text for dels from 1 to 100bp
<plot>
type = text
color = 160, 0, 0
label_font = condensed
file = vis/DelTrack1_text.txt
orientation = in
r1 = 0.99r
r0 = 0.80r
label_size = 12p
show_links = yes
link_dims = 2p,2p,4p,2p,2p
link_thickness = 2p
link_color = red
label_snuggle = yes
max_snuggle_distance = 1r
snuggle_tolerance = 0.25r
snuggle_sampling = 2
</plot>
#########histgram for dels from 1 to 100bp
<plot>
# The type sets the format of the track.
type = histogram
thickness = 2
file = vis/DelTrack1.txt
extend_bin = yes
fill_color = 0, 0, 254
color = grey
orientation = in
r1 = 0.80r
r0 = 0.65r
# Histograms can have both a fill and outline. The default outline is 1px thick black.
<axes>
<axis>
spacing = 0.4r
color = lgrey
thickness = 1
</axis>
</axes>
</plot>
######## text for dels from 100 to 1000bp
<plot>
type = text
color = 0,150,0
label_font = condensed
file = vis/DelTrack100_text.txt
orientation = in
r1 = 0.65r
r0 = 0.55r
label_size = 12p
show_links = yes
link_dims = 2p,2p,4p,2p,2p
link_thickness = 2p
link_color = red
label_snuggle = yes
max_snuggle_distance = 1r
snuggle_tolerance = 0.25r
snuggle_sampling = 2
</plot>
#########histgram for dels from 100 to 1000bp
<plot>
# The type sets the format of the track.
type = histogram
thickness = 2
file = vis/DelTrack100.txt
extend_bin = yes
fill_color = 0, 200, 0
color = grey
orientation = in
r1 = 0.55r
r0 = 0.40r
# Histograms can have both a fill and outline. The default outline is 1px thick black.
<axes>
<axis>
spacing = 0.4r
color = lgrey
thickness = 1
</axis>
</axes>
</plot>
######### text for dels from 1000 to 1,500,000bp
<plot>
type = text
color = 0,0,190
label_font = condensed
file = vis/DelTrack1000_text.txt
orientation = in
r1 = 0.40r
r0 = 0.30r
label_size = 12p
show_links = yes
link_dims = 2p,2p,4p,2p,2p
link_thickness = 2p
link_color = red
label_snuggle = yes
max_snuggle_distance = 1r
snuggle_tolerance = 0.25r
snuggle_sampling = 2
</plot>
#########histgram for dels from 1000 to 1,500,000bp
<plot>
# The type sets the format of the track.
type = histogram
thickness = 2
file = vis/DelTrack1000.txt
extend_bin = yes
fill_color = 230,0,0
color = grey
orientation = in
r1 = 0.30r
r0 = 0.15r
# Histograms can have both a fill and outline. The default outline is 1px thick black.
<axes>
<axis>
spacing = 0.4r
color = lgrey
thickness = 1
</axis>
</axes>
</plot>
</plots>
# don't include links
<<include etc/ideogram.conf>>
<<include etc/ticks.conf>>
<image>
<<include etc/image.conf>>
"""
fileName = "file* = vis/"+ofile
tempt_end = """
</image>
<<include etc/colors_fonts_patterns.conf>>
<<include etc/housekeeping.conf>>
data_out_of_range* = trim
"""
FileOUT = open("circos.conf","w")
FileOUT.write(tempt_start+"\n")
FileOUT.write(fileName+"\n")
FileOUT.write(tempt_end+"\n")
FileOUT.close()
#got the right format of input file from Yinbing
#output the right format which circos and accept
############################# declare variables #############################
t0 = time.time()
sfile = ""
wfile = int
lfile = ""
ofile = ""
############################## process command line arguments #############################
parser = argparse.ArgumentParser(description="VarAnnot annotate small variants (snp&small indel) and big deletions")
parser.add_argument('--version', action='version', version='%(prog)s 1.0')
#parser.add_argument('-s', action='store', dest='sfile', help="small variance file")
#parser.add_argument('-w', action='store', default= 50000, dest='wsize', help="windowsize for small variance file")
parser.add_argument('-l', action='store', dest='lfile', help="annoated large deletion file")
parser.add_argument('-o', action='store', dest='ofile', help="the output file name for circos image file")
args = parser.parse_args()
# sfile = args.sfile
# wsize = args.wsize
lfile = args.lfile
ofile = args.ofile
if len(sys.argv) == 1:
print parser.print_help()
sys.exit("error: give me your input and output files!")
############################# read AF files #############################
else :
#processSmallVar(sfile,wsize)
processDel(lfile)
createConf(ofile)
os.system("circos")
t1 = time.time()
totalTime = t1-t0
print "Total running time (sec): " + str(totalTime)