1 # Time-stamp: <2019-09-20 11:59:32 taoliu>
3 """Module Description: Functions to write file
5 This code is free software; you can redistribute it and/or modify it
6 under the terms of the BSD License (see the file LICENSE included with
10 # ------------------------------------
12 # ------------------------------------
15 from array
import array
16 from MACS2
.Constants
import *
18 # ------------------------------------
20 # ------------------------------------
21 # to determine the byte size
22 if array('h',[1]).itemsize
== 2:
25 raise Exception("BYTE2 type cannot be determined!")
26 if array('i',[1]).itemsize
== 4:
28 elif array('l',[1]).itemsize
== 4:
31 raise Exception("BYTE4 type cannot be determined!")
33 if array('f',[1]).itemsize
== 4:
35 elif array('d',[1]).itemsize
== 4:
38 raise Exception("FBYTE4 type cannot be determined!")
40 # ------------------------------------
42 # ------------------------------------
43 def zwig_write (trackI
, subdir
, fileprefix
, d
, log
=None,space
=10, single
=False):
44 """Write shifted tags information in wiggle file in a given
45 step. Then compress it using 'gzip' program.
47 trackI: shifted tags from PeakDetect object
48 subdir: directory where to put the wiggle file
49 fileprefix: wiggle file prefix
51 log : logging function, default is sys.stderr.write
52 space : space to write tag number on spots, default 10
55 log
= lambda x
: sys
.stderr
.write(x
+"\n")
56 chrs
= trackI
.get_chr_names()
61 log("write to a wiggle file")
62 f
= os
.path
.join(subdir
,fileprefix
+"_all"+".wig")
64 wigfhd
.write("track type=wiggle_0 name=\"%s_all\" description=\"Extended tag pileup from MACS version %s for every %d bp\"\n" % (fileprefix
.replace('_afterfiting',''), MACS_VERSION
, space
)) # data type line
68 f
= os
.path
.join(subdir
,fileprefix
+"_"+chrom
+".wig")
69 log("write to "+f
+" for chromosome "+chrom
)
72 wigfhd
.write("track type=wiggle_0 name=\"%s_%s\" description=\"Extended tag pileup from MACS version %s for every %d bp\"\n" % ( fileprefix
.replace('_afterfiting',''), chrom
, MACS_VERSION
, space
)) # data type line
74 log("write data for chromosome "+chrom
)
76 wigfhd
.write("variableStep chrom=%s span=%d\n" % (chrom
,space
))
77 tags
= trackI
.get_locations_by_chr(chrom
)[0]
79 window_counts
= array(BYTE4
,[0]*step
)
85 s
= tags
[index_tag
]-d
/2 # start of tag
89 # project tag to window_counts line
90 ps
= s
-startp
# projection start
91 pe
= ps
+d
# projection end
92 for i
in range(ps
,pe
):
96 # write it to zwig file then reset parameters
97 # keep this tag for next window
98 for i
in range(d
,step
-d
,space
):
99 if window_counts
[i
] == 0:
102 wigfhd
.write("%d\t%d\n" % (i
+startp
+1,window_counts
[i
]))
104 window_counts_next
= array(BYTE4
,[0]*step
)
105 # copy d values from the tail of previous window to next window
106 for n
,i
in enumerate(range(step
-2*d
,step
)): # debug
107 window_counts_next
[n
] = window_counts
[i
]
108 window_counts
= window_counts_next
112 for i
in range(d
,step
-d
,space
):
113 if window_counts
[i
] == 0:
116 wigfhd
.write("%d\t%d\n" % (i
+startp
+1,window_counts
[i
]))
119 log("compress the wiggle file using gzip...")
123 log("compress the wiggle file using gzip...")
127 def zbdg_write (trackI
, subdir
, fileprefix
, d
, log
=None, single
=False):
128 """Write shifted tags information in wiggle file in a given
129 step. Then compress it using 'gzip' program.
131 trackI: shifted tags from PeakDetect object
132 subdir: directory where to put the wiggle file
133 fileprefix: wiggle file prefix
135 log : logging function, default is sys.stderr.write
136 space : space to write tag number on spots, default 10
139 log
= lambda x
: sys
.stderr
.write(x
+"\n")
140 chrs
= trackI
.get_chr_names()
142 step
= 10000000 + 2*d
145 log("write to a bedGraph file")
146 f
= os
.path
.join(subdir
,fileprefix
+"_all"+".bdg")
148 bdgfhd
.write("track type=bedGraph name=\"%s_all\" description=\"Extended tag pileup from MACS version %s\"\n" % (fileprefix
.replace('_afterfiting',''), MACS_VERSION
)) # data type line
152 f
= os
.path
.join(subdir
,fileprefix
+"_"+chrom
+".bdg")
153 log("write to "+f
+" for chromosome "+chrom
)
155 bdgfhd
.write("track type=bedGraph name=\"%s_%s\" description=\"Extended tag pileup from MACS version %s\"\n" % (fileprefix
.replace('_afterfiting',''), chrom
, MACS_VERSION
)) # data type line
157 log("write data for chromosome "+chrom
)
159 tags
= trackI
.get_locations_by_chr(chrom
)[0]
161 window_counts
= array(BYTE4
,[0]*step
)
167 s
= tags
[index_tag
]-d
/2 # start of tag
171 # project tag to window_counts line
172 ps
= s
-startp
# projection start
173 pe
= ps
+d
# projection end
174 for i
in range(ps
,pe
):
175 window_counts
[i
] += 1
178 # write it to zbdg file then reset parameters
179 # keep this tag for next window
180 prev
= window_counts
[d
]
183 for i
in range(d
+1,step
-d
):
184 if window_counts
[i
] == prev
:
190 bdgfhd
.write("%s\t%d\t%d\t%d\n" % (chrom
,left
,right
,prev
))
191 prev
= window_counts
[i
]
196 bdgfhd
.write("%s\t%d\t%d\t%d\n" % (chrom
,left
,right
,prev
))
199 window_counts_next
= array(BYTE4
,[0]*step
)
200 # copy d values from the tail of previous window to next window
201 for n
,i
in enumerate(range(step
-2*d
,step
)): # debug
202 window_counts_next
[n
] = window_counts
[i
]
203 window_counts
= window_counts_next
207 prev
= window_counts
[d
]
210 for i
in range(d
+1,step
-d
):
211 if window_counts
[i
] == prev
:
217 bdgfhd
.write("%s\t%d\t%d\t%d\n" % (chrom
,left
,right
,prev
))
218 prev
= window_counts
[i
]
223 bdgfhd
.write("%s\t%d\t%d\t%d\n" % (chrom
,left
,right
,prev
))
227 log("compress the bedGraph file using gzip...")
231 log("compress the bedGraph file using gzip...")
235 def model2r_script(model
,filename
,name
):
236 rfhd
= open(filename
,"w")
241 alt_d
= model
.alternative_d
242 #s = model.shifted_line
252 norm_p
[i
] = float(p
[i
])*100/sum_p
253 norm_m
[i
] = float(m
[i
])*100/sum_m
254 #norm_s[i] = float(s[i])*100/sum_s
255 rfhd
.write("# R script for Peak Model\n")
256 rfhd
.write("# -- generated by MACS\n")
258 rfhd
.write("""p <- c(%s)
263 x <- seq.int((length(p)-1)/2*-1,(length(p)-1)/2)
264 pdf('%s_model.pdf',height=6,width=6)
265 plot(x,p,type='l',col=c('red'),main='Peak Model',xlab='Distance to the middle',ylab='Percentage')
266 lines(x,m,col=c('blue'))
267 legend('topleft',c('forward tags','reverse tags'),lty=c(1,1,1),col=c('red','blue'))
268 plot(xcorr,ycorr,type='l',col=c('black'),main='Cross-Correlation',xlab='Lag between + and - tags',ylab='Correlation')
269 abline(v=altd,lty=2,col=c('red'))
270 legend('topleft','alternative lag(s)',lty=2,col='red')
271 legend('right','alt lag(s) : %s',bty='n')
273 """ % (','.join(map(str,norm_p
)),
274 ','.join(map(str,norm_m
)),
275 ','.join(map(str,ycorr
)),
276 ','.join(map(str,xcorr
)),
277 ', '.join(map(str,alt_d
)),
279 ','.join(map(str,alt_d
))
283 def diag_write (filename
, diag_result
):
284 ofhd_diag
= open(filename
,"w")
287 s
= [90-x
*10 for x
in range(l
)]
288 ofhd_diag
.write("FC range\t# of Peaks\tcover by sampling %s\n" % ("%\t".join (map(str,s
))+"%"))
289 format
= "%s\t%d"+"\t%.2f"*l
+"\n"
290 ofhd_diag
.write( "".join( [format
% tuple(x
) for x
in diag_result
]) )
293 # ------------------------------------
295 # ------------------------------------