5 print "psyco not running"
7 print "altSpliceCounts: version 3.7"
12 from commoncode import getConfigParser, getConfigOption
18 usage = "usage: python %s rdsfile outfilename [--cache pages]"
20 parser = makeParser(usage)
21 (options, args) = parser.parse_args(argv[1:])
30 if options.numCachePages is not None:
32 cachePages = options.numCachePages
37 altSpliceCounts(hitfile, outfilename, doCache, cachePages)
40 def makeParser(usage=""):
41 parser = optparse.OptionParser(usage=usage)
42 parser.add_option("--cache", type="int", dest="numCachePages",
43 help="number of cache pages to use [default: 100000]")
45 configParser = getConfigParser()
46 section = "altSpliceCounts"
47 numCachePages = getConfigOption(configParser, section, "numCachePages", None)
49 parser.set_defaults(numCachePages=numCachePages)
54 def altSpliceCounts(hitfile, outfilename, doCache=False, cachePages=100000):
59 hitRDS = ReadDataset.ReadDataset(hitfile, verbose=True, cache=doCache)
60 if cachePages > hitRDS.getDefaultCacheSize():
61 hitRDS.setDBcache(cachePages)
63 readlen = hitRDS.getReadSize()
64 hitDict = hitRDS.getSplicesDict(noSense=True)
65 outfile = open(outfilename, "w")
70 resultDict[chrom] = []
74 for read in hitDict[chrom]:
75 tagStart = read["startL"]
76 tagStop = read["stopR"]
78 length = tagStop - tagStart
79 if length < readlen + 5:
82 startDict[chrom].append((tagStart, length))
83 stopDict[chrom].append((tagStop, length))
85 startDict[chrom].sort()
86 stopDict[chrom].sort()
91 for chrom in startDict:
93 maxIndex = len(startDict[chrom])
94 while firstIndex < maxIndex:
95 (fstart, flen) = startDict[chrom][firstIndex]
96 (start, length) = (fstart, flen)
97 secondIndex = firstIndex
99 while (start - fstart) < readlen:
100 if secondIndex >= maxIndex:
103 (start, length) = startDict[chrom][secondIndex]
104 if (start - fstart) < readlen and abs(length - flen) > readlen:
105 line = (chrom, fstart, fstart + flen, chrom, start, start + length)
107 for slength in secondLengths:
108 if abs(slength - length) < readlen:
111 if len(resultDict[chrom]) == 0:
112 resultDict[chrom].append(line)
113 elif line != resultDict[chrom][-1] and not alreadySeen:
114 resultDict[chrom].append(line)
115 secondLengths.append(length)
121 firstIndex = secondIndex
125 maxIndex = len(stopDict[chrom])
126 while firstIndex < maxIndex:
127 (fstop, flen) = stopDict[chrom][firstIndex]
128 (stop, length) = (fstop, flen)
129 secondIndex = firstIndex
131 while (stop - fstop) < readlen:
132 if secondIndex >= maxIndex:
134 (stop, length) = stopDict[chrom][secondIndex]
135 if (stop - fstop) < readlen and abs(length - flen) > readlen:
136 line = (chrom, fstop - flen, fstop, chrom, stop - length, stop)
138 for slength in secondLengths:
139 if abs(slength - length) < readlen:
142 if len(resultDict[chrom]) == 0:
143 resultDict[chrom].append(line)
145 if line != resultDict[chrom][-1] and not alreadySeen:
146 resultDict[chrom].append(line)
147 secondLengths.append(length)
153 firstIndex = secondIndex
156 resultDict[chrom].sort()
157 for line in resultDict[chrom]:
158 outfile.write("alt%d" % alternative + "\tchr%s\t%d\t%d\tchr%s\t%d\t%d\n" % line)
161 print chrom, maxIndex, spliceEvent, altSpliceEvent
163 print spliceEvent, altSpliceEvent
166 if __name__ == "__main__":