6 from commoncode import readDataset
8 print "%prog: version 6.7"
14 print 'psyco not running'
21 usage = "usage: python %s name rdsfile outfilename [options]"
23 parser = optparse.OptionParser(usage=usage)
24 parser.add_option("--raw", action="store_false", dest="doNormalize")
25 parser.add_option("--color", dest="color")
26 parser.add_option("--altcolor", dest="altColor")
27 parser.add_option("--chrom", dest="limitChrom")
28 parser.add_option("--shift", type="int", dest="shift")
29 parser.add_option("--split", action="store_true", dest="doSplit")
30 parser.add_option("--listfile", dest="listfilename")
31 parser.add_option("--listprefix", dest="listPrefix")
32 parser.add_option("--group", dest="group")
33 parser.add_option("--startPriority", type="float", dest="startPriority")
34 parser.add_option("--skiprandom", action="store_true", dest="skipRandom")
35 parser.add_option("--nomulti", action="store_false", dest="withMulti")
36 parser.add_option("--splices", action="store_true", dest="withSplices")
37 parser.add_option("--singlebase", action="store_true", dest="doSingle")
38 parser.add_option("--cache", type="int", dest="cachePages")
39 parser.add_option("--enforceChr", action="store_true", dest="enforceChr")
40 parser.add_option("--stranded", dest="strand")
41 parser.add_option("--maxchunk", type="int", dest="chunk")
42 parser.set_defaults(doNormalize=True, color=None, altColor="", limitChrom=None,
43 shift=0, doSplit=False, listfilename=None, listPrefix="",
44 group="", startPriority=0.01, skipRandom=False, withMulti=True,
45 withSplices=False, doSingle=False, cachePages=-1, enforceChr=False,
46 strand=None, chunk=20)
48 (options, args) = parser.parse_args(argv[1:])
58 makewiggle(name, hitfilename, outfilename, options.doNormalize, options.color, options.altColor,
59 options.limitChrom, options.shift, options.doSplit, options.listfilename, options.listPrefix,
60 options.group, options.startPriority, options.skipRandom, options.withMulti,
61 options.withSplices, options.doSingle, options.cachePages, options.enforceChr, options.strand,
65 def makewiggle(name, hitfilename, outfilename, doNormalize=True, color=None, altColor="",
66 limitChrom=None, shift=0, doSplit=False, listfilename=None, listPrefix="",
67 group="", startPriority=0.01, skipRandom=False, withMulti=True, withSplices=False,
68 doSingle=False, cachePages=-1, enforceChr=False, strand=None, chunk=20):
70 priorityIncrement = 0.01
74 colorString = " color=%s" % color
79 colorString += " altcolor=%s" % altColor
82 if listfilename is not None:
86 if limitChrom is not None:
90 groupName = "group=%s" % group
96 maxSpan = chunk * 1000000
99 strandedDirection = "both"
100 if strand is not None:
103 strandedDirection = "plusOnly"
104 elif strand == "minus":
105 strandedDirection = "minusOnly"
107 print "will keep track of %s strand(s)" % strandedDirection
110 print "Will shift reads by +/- %d bp according to their sense" % shift
111 name += "shift=%d" % shift
113 hitRDS = readDataset(hitfilename, verbose=True, cache=doCache)
115 if cachePages > hitRDS.getDefaultCacheSize():
116 hitRDS.setDBcache(cachePages)
118 readlen = hitRDS.getReadSize()
121 normalizeBy = len(hitRDS) / 1000000.
126 listfile = open(listfilename, "w")
128 priority = startPriority
130 outfile = open(outfilename, "w")
132 listfile.write("%s%s\n" % (listPrefix, outfilename))
134 outfile.write('track type=%s name="%s" %s priority=%.3f visibility=full%s\n' % (wigType, name, groupName, priority, colorString))
136 chromList = hitRDS.getChromosomes()
138 for achrom in chromList:
139 if enforceChr and ("chr" not in achrom):
142 if chromLimit and achrom != limitChrom:
145 if skipRandom and "random" in achrom:
149 outfile = open("%s.%s" % (outfilename, achrom), "w")
151 listfile.write("%s%s.%s\n" % (listPrefix, outfilename, achrom))
153 outfile.write('track type=%s name="%s %s" %s priority=%.3f visibility=full%s\n' % (wigType, name, achrom, groupName, priority, colorString))
154 priority += priorityIncrement
156 lastNT = hitRDS.getMaxCoordinate(achrom, doMulti=withMulti, doSplices=withSplices) + readlen
162 for spanStop in xrange(maxSpan, lastNT+maxSpan, maxSpan):
163 if spanStop > lastNT:
166 print achrom, spanStart, spanStop
167 chromModel = hitRDS.getChromProfile(achrom, spanStart, spanStop, withMulti, withSplices, normalizeBy, isStranded, strandedDirection, shiftValue=shift)
169 for index in xrange(len(chromModel)):
170 currentVal = chromModel[index]
172 outline = "%s %d %.4f\n" % (achrom, spanStart + index, currentVal)
173 outfile.write(outline)
176 if currentVal == previousVal:
179 if currentVal != previousVal:
181 lastpos = index + spanStart
182 outline = "%s %d %d %.4f\n" % (achrom, previousStart, lastpos, previousVal)
183 outfile.write(outline)
186 previousVal = currentVal
187 previousStart = index + spanStart
191 spanStart = spanStop + 1
208 if __name__ == "__main__":