# Time-stamp: <2011-02-16 22:16:16 Tao Liu> """Module Description Copyright (c) 2008,2009,2010,2011 Tao Liu 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 MACS14.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() if not os.path.exists(subdir): 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