1 # Time-stamp: <2020-12-01 12:57:23 Tao Liu>
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
as pyarray
17 from MACS3
.Utilities
.Constants
import MACS_VERSION
19 # ------------------------------------
21 # ------------------------------------
23 # ------------------------------------
25 # ------------------------------------
26 def zwig_write (trackI
, subdir
, fileprefix
, d
, log
=None,space
=10, single
=False):
27 """Write shifted tags information in wiggle file in a given
28 step. Then compress it using 'gzip' program.
30 trackI: shifted tags from PeakDetect object
31 subdir: directory where to put the wiggle file
32 fileprefix: wiggle file prefix
34 log : logging function, default is sys.stderr.write
35 space : space to write tag number on spots, default 10
38 log
= lambda x
: sys
.stderr
.write(x
+"\n")
39 chrs
= trackI
.get_chr_names()
44 log("write to a wiggle file")
45 f
= os
.path
.join(subdir
,fileprefix
+"_all"+".wig")
47 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
51 f
= os
.path
.join(subdir
,fileprefix
+"_"+chrom
+".wig")
52 log("write to "+f
+" for chromosome "+chrom
)
55 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
57 log("write data for chromosome "+chrom
)
59 wigfhd
.write("variableStep chrom=%s span=%d\n" % (chrom
,space
))
60 tags
= trackI
.get_locations_by_chr(chrom
)[0]
62 window_counts
= pyarray('L',[0]*step
)
68 s
= tags
[index_tag
]-d
/2 # start of tag
72 # project tag to window_counts line
73 ps
= s
-startp
# projection start
74 pe
= ps
+d
# projection end
75 for i
in range(ps
,pe
):
79 # write it to zwig file then reset parameters
80 # keep this tag for next window
81 for i
in range(d
,step
-d
,space
):
82 if window_counts
[i
] == 0:
85 wigfhd
.write("%d\t%d\n" % (i
+startp
+1,window_counts
[i
]))
87 window_counts_next
= pyarray('L',[0]*step
)
88 # copy d values from the tail of previous window to next window
89 for n
,i
in enumerate(range(step
-2*d
,step
)): # debug
90 window_counts_next
[n
] = window_counts
[i
]
91 window_counts
= window_counts_next
95 for i
in range(d
,step
-d
,space
):
96 if window_counts
[i
] == 0:
99 wigfhd
.write("%d\t%d\n" % (i
+startp
+1,window_counts
[i
]))
102 log("compress the wiggle file using gzip...")
106 log("compress the wiggle file using gzip...")
110 def zbdg_write (trackI
, subdir
, fileprefix
, d
, log
=None, single
=False):
111 """Write shifted tags information in wiggle file in a given
112 step. Then compress it using 'gzip' program.
114 trackI: shifted tags from PeakDetect object
115 subdir: directory where to put the wiggle file
116 fileprefix: wiggle file prefix
118 log : logging function, default is sys.stderr.write
119 space : space to write tag number on spots, default 10
122 log
= lambda x
: sys
.stderr
.write(x
+"\n")
123 chrs
= trackI
.get_chr_names()
125 step
= 10000000 + 2*d
128 log("write to a bedGraph file")
129 f
= os
.path
.join(subdir
,fileprefix
+"_all"+".bdg")
131 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
135 f
= os
.path
.join(subdir
,fileprefix
+"_"+chrom
+".bdg")
136 log("write to "+f
+" for chromosome "+chrom
)
138 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
140 log("write data for chromosome "+chrom
)
142 tags
= trackI
.get_locations_by_chr(chrom
)[0]
144 window_counts
= pyarray('L',[0]*step
)
150 s
= tags
[index_tag
]-d
/2 # start of tag
154 # project tag to window_counts line
155 ps
= s
-startp
# projection start
156 pe
= ps
+d
# projection end
157 for i
in range(ps
,pe
):
158 window_counts
[i
] += 1
161 # write it to zbdg file then reset parameters
162 # keep this tag for next window
163 prev
= window_counts
[d
]
166 for i
in range(d
+1,step
-d
):
167 if window_counts
[i
] == prev
:
173 bdgfhd
.write("%s\t%d\t%d\t%d\n" % (chrom
,left
,right
,prev
))
174 prev
= window_counts
[i
]
179 bdgfhd
.write("%s\t%d\t%d\t%d\n" % (chrom
,left
,right
,prev
))
182 window_counts_next
= pyarray('L',[0]*step
)
183 # copy d values from the tail of previous window to next window
184 for n
,i
in enumerate(range(step
-2*d
,step
)): # debug
185 window_counts_next
[n
] = window_counts
[i
]
186 window_counts
= window_counts_next
190 prev
= window_counts
[d
]
193 for i
in range(d
+1,step
-d
):
194 if window_counts
[i
] == prev
:
200 bdgfhd
.write("%s\t%d\t%d\t%d\n" % (chrom
,left
,right
,prev
))
201 prev
= window_counts
[i
]
206 bdgfhd
.write("%s\t%d\t%d\t%d\n" % (chrom
,left
,right
,prev
))
210 log("compress the bedGraph file using gzip...")
214 log("compress the bedGraph file using gzip...")
218 def model2r_script(model
,filename
,name
):
219 rfhd
= open(filename
,"w")
224 alt_d
= model
.alternative_d
225 #s = model.shifted_line
235 norm_p
[i
] = float(p
[i
])*100/sum_p
236 norm_m
[i
] = float(m
[i
])*100/sum_m
237 #norm_s[i] = float(s[i])*100/sum_s
238 rfhd
.write("# R script for Peak Model\n")
239 rfhd
.write("# -- generated by MACS\n")
241 rfhd
.write("""p <- c(%s)
246 x <- seq.int((length(p)-1)/2*-1,(length(p)-1)/2)
247 pdf('%s_model.pdf',height=6,width=6)
248 plot(x,p,type='l',col=c('red'),main='Peak Model',xlab='Distance to the middle',ylab='Percentage')
249 lines(x,m,col=c('blue'))
250 legend('topleft',c('forward tags','reverse tags'),lty=c(1,1,1),col=c('red','blue'))
251 plot(xcorr,ycorr,type='l',col=c('black'),main='Cross-Correlation',xlab='Lag between + and - tags',ylab='Correlation')
252 abline(v=altd,lty=2,col=c('red'))
253 legend('topleft','alternative lag(s)',lty=2,col='red')
254 legend('right','alt lag(s) : %s',bty='n')
256 """ % (','.join(map(str,norm_p
)),
257 ','.join(map(str,norm_m
)),
258 ','.join(map(str,ycorr
)),
259 ','.join(map(str,xcorr
)),
260 ', '.join(map(str,alt_d
)),
262 ','.join(map(str,alt_d
))
266 def diag_write (filename
, diag_result
):
267 ofhd_diag
= open(filename
,"w")
270 s
= [90-x
*10 for x
in range(l
)]
271 ofhd_diag
.write("FC range\t# of Peaks\tcover by sampling %s\n" % ("%\t".join (map(str,s
))+"%"))
272 format
= "%s\t%d"+"\t%.2f"*l
+"\n"
273 ofhd_diag
.write( "".join( [format
% tuple(x
) for x
in diag_result
]) )
276 # ------------------------------------
278 # ------------------------------------