/usr/lib/python2.7/dist-packages/MACS2/OutputWriter.py is in macs 2.1.0.20151222-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # Time-stamp: <2012-10-02 17:14:25 Tao Liu>
"""Module Description
Copyright (c) 2008,2009,2010,2011 Tao Liu <taoliu@jimmy.harvard.edu>
This code is free software; you can redistribute it and/or modify it
under the terms of the BSD License (see the file COPYING included with
the distribution).
@status: experimental
@version: $Revision$
@author: Tao Liu
@contact: taoliu@jimmy.harvard.edu
"""
# ------------------------------------
# python modules
# ------------------------------------
import os
import sys
from array import array
from MACS2.Constants import *
# ------------------------------------
# constants
# ------------------------------------
# to determine the byte size
if array('h',[1]).itemsize == 2:
BYTE2 = 'h'
else:
raise Exception("BYTE2 type cannot be determined!")
if array('i',[1]).itemsize == 4:
BYTE4 = 'i'
elif array('l',[1]).itemsize == 4:
BYTE4 = 'l'
else:
raise Exception("BYTE4 type cannot be determined!")
if array('f',[1]).itemsize == 4:
FBYTE4 = 'f'
elif array('d',[1]).itemsize == 4:
FBYTE4 = 'd'
else:
raise Exception("FBYTE4 type cannot be determined!")
# ------------------------------------
# Misc functions
# ------------------------------------
def zwig_write (trackI, subdir, fileprefix, d, log=None,space=10, single=False):
"""Write shifted tags information in wiggle file in a given
step. Then compress it using 'gzip' program.
trackI: shifted tags from PeakDetect object
subdir: directory where to put the wiggle file
fileprefix: wiggle file prefix
d : d length
log : logging function, default is sys.stderr.write
space : space to write tag number on spots, default 10
"""
if not log:
log = lambda x: sys.stderr.write(x+"\n")
chrs = trackI.get_chr_names()
os.makedirs (subdir)
step = 10000000 + 2*d
if single:
log("write to a wiggle file")
f = os.path.join(subdir,fileprefix+"_all"+".wig")
wigfhd = open(f,"w")
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
for chrom in chrs:
if not single:
f = os.path.join(subdir,fileprefix+"_"+chrom+".wig")
log("write to "+f+" for chromosome "+chrom)
wigfhd = open(f,"w")
# suggested by dawe
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
else:
log("write data for chromosome "+chrom)
wigfhd.write("variableStep chrom=%s span=%d\n" % (chrom,space))
tags = trackI.get_locations_by_chr(chrom)[0]
l = len(tags)
window_counts = array(BYTE4,[0]*step)
startp = -1*d
endp = startp+step
index_tag = 0
while index_tag<l:
s = tags[index_tag]-d/2 # start of tag
e = s+d # end of tag
if e < endp:
# project tag to window_counts line
ps = s-startp # projection start
pe = ps+d # projection end
for i in xrange(ps,pe):
window_counts[i] += 1
index_tag += 1
else:
# write it to zwig file then reset parameters
# keep this tag for next window
for i in xrange(d,step-d,space):
if window_counts[i] == 0:
pass
else:
wigfhd.write("%d\t%d\n" % (i+startp+1,window_counts[i]))
# reset
window_counts_next = array(BYTE4,[0]*step)
# copy d values from the tail of previous window to next window
for n,i in enumerate(xrange(step-2*d,step)): # debug
window_counts_next[n] = window_counts[i]
window_counts = window_counts_next
startp = endp - 2*d
endp = startp+step
# last window
for i in xrange(d,step-d,space):
if window_counts[i] == 0:
pass
else:
wigfhd.write("%d\t%d\n" % (i+startp+1,window_counts[i]))
if not single:
wigfhd.close()
log("compress the wiggle file using gzip...")
os.system("gzip "+f)
if single:
wigfhd.close()
log("compress the wiggle file using gzip...")
os.system("gzip "+f)
def zbdg_write (trackI, subdir, fileprefix, d, log=None, single=False):
"""Write shifted tags information in wiggle file in a given
step. Then compress it using 'gzip' program.
trackI: shifted tags from PeakDetect object
subdir: directory where to put the wiggle file
fileprefix: wiggle file prefix
d : d length
log : logging function, default is sys.stderr.write
space : space to write tag number on spots, default 10
"""
if not log:
log = lambda x: sys.stderr.write(x+"\n")
chrs = trackI.get_chr_names()
os.makedirs (subdir)
step = 10000000 + 2*d
if single:
log("write to a bedGraph file")
f = os.path.join(subdir,fileprefix+"_all"+".bdg")
bdgfhd = open(f,"w")
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
for chrom in chrs:
if not single:
f = os.path.join(subdir,fileprefix+"_"+chrom+".bdg")
log("write to "+f+" for chromosome "+chrom)
bdgfhd = open(f,"w")
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
else:
log("write data for chromosome "+chrom)
tags = trackI.get_locations_by_chr(chrom)[0]
l = len(tags)
window_counts = array(BYTE4,[0]*step)
startp = -1*d
endp = startp+step
index_tag = 0
while index_tag<l:
s = tags[index_tag]-d/2 # start of tag
e = s+d # end of tag
if e < endp:
# project tag to window_counts line
ps = s-startp # projection start
pe = ps+d # projection end
for i in xrange(ps,pe):
window_counts[i] += 1
index_tag += 1
else:
# write it to zbdg file then reset parameters
# keep this tag for next window
prev = window_counts[d]
left = startp+d
right = left+1
for i in xrange(d+1,step-d):
if window_counts[i] == prev:
# same value, extend
right += 1
else:
# diff value, close
if prev != 0:
bdgfhd.write("%s\t%d\t%d\t%d\n" % (chrom,left,right,prev))
prev = window_counts[i]
left = right
right = left + 1
# last bin
if prev != 0:
bdgfhd.write("%s\t%d\t%d\t%d\n" % (chrom,left,right,prev))
# reset
window_counts_next = array(BYTE4,[0]*step)
# copy d values from the tail of previous window to next window
for n,i in enumerate(xrange(step-2*d,step)): # debug
window_counts_next[n] = window_counts[i]
window_counts = window_counts_next
startp = endp - 2*d
endp = startp+step
# last window
prev = window_counts[d]
left = startp+d
right = left+1
for i in xrange(d+1,step-d):
if window_counts[i] == prev:
# same value, exrend
right += 1
else:
# diff value, close
if prev != 0:
bdgfhd.write("%s\t%d\t%d\t%d\n" % (chrom,left,right,prev))
prev = window_counts[i]
left = right
right = left + 1
# last bin
if prev != 0:
bdgfhd.write("%s\t%d\t%d\t%d\n" % (chrom,left,right,prev))
if not single:
bdgfhd.close()
log("compress the bedGraph file using gzip...")
os.system("gzip "+f)
if single:
bdgfhd.close()
log("compress the bedGraph file using gzip...")
os.system("gzip "+f)
def model2r_script(model,filename,name):
rfhd = open(filename,"w")
p = model.plus_line
m = model.minus_line
ycorr = model.ycorr
xcorr = model.xcorr
alt_d = model.alternative_d
#s = model.shifted_line
d = model.d
w = len(p)
norm_p = [0]*w
norm_m = [0]*w
#norm_s = [0]*w
sum_p = sum(p)
sum_m = sum(m)
#sum_s = sum(s)
for i in range(w):
norm_p[i] = float(p[i])*100/sum_p
norm_m[i] = float(m[i])*100/sum_m
#norm_s[i] = float(s[i])*100/sum_s
rfhd.write("# R script for Peak Model\n")
rfhd.write("# -- generated by MACS\n")
rfhd.write("""p <- c(%s)
m <- c(%s)
ycorr <- c(%s)
xcorr <- c(%s)
altd <- c(%s)
x <- seq.int((length(p)-1)/2*-1,(length(p)-1)/2)
pdf('%s_model.pdf',height=6,width=6)
plot(x,p,type='l',col=c('red'),main='Peak Model',xlab='Distance to the middle',ylab='Percentage')
lines(x,m,col=c('blue'))
legend('topleft',c('forward tags','reverse tags'),lty=c(1,1,1),col=c('red','blue'))
plot(xcorr,ycorr,type='l',col=c('black'),main='Cross-Correlation',xlab='Lag between + and - tags',ylab='Correlation')
abline(v=altd,lty=2,col=c('red'))
legend('topleft','alternative lag(s)',lty=2,col='red')
legend('right','alt lag(s) : %s',bty='n')
dev.off()
""" % (','.join(map(str,norm_p)),
','.join(map(str,norm_m)),
','.join(map(str,ycorr)),
','.join(map(str,xcorr)),
', '.join(map(str,alt_d)),
name,
','.join(map(str,alt_d))
))
rfhd.close()
def diag_write (filename, diag_result):
ofhd_diag = open(filename,"w")
a = diag_result[0]
l = len(a)-2
s = [90-x*10 for x in range(l)]
ofhd_diag.write("FC range\t# of Peaks\tcover by sampling %s\n" % ("%\t".join (map(str,s))+"%"))
format = "%s\t%d"+"\t%.2f"*l+"\n"
ofhd_diag.write( "".join( [format % tuple(x) for x in diag_result]) )
ofhd_diag.close()
# ------------------------------------
# Classes
# ------------------------------------
|