5 # Created by Ali Mortazavi on 12/7/08.
17 from commoncode import writeLog, getConfigParser, getConfigOption, getConfigIntOption, getConfigBoolOption
20 verstring = "makerdsfromblat: version 3.10"
27 usage = "usage: python %prog label infilename outrdsfile [propertyName::propertyValue] [options]"
29 parser = getParser(usage)
30 (options, args) = parser.parse_args(argv[1:])
40 if options.geneDataFileName:
52 (pname, pvalue) = arg.strip().split("::")
53 propertyList.append((pname, pvalue))
55 makerdsfromblat(label, filename, outdbname, dataType, options.init,
56 options.doIndex, options.trimReadID, options. minSpliceLength,
57 options.forceRNA, theFlag, options.spliceOnly, options.verbose,
58 options.cachePages, options.geneDataFileName, propertyList)
62 parser = optparse.OptionParser(usage=usage)
63 parser.add_option("--append", action="store_false", dest="init")
64 parser.add_option("--index", action="store_true", dest="doIndex")
65 parser.add_option("--rawreadID", action="store_false", dest="trimReadID")
66 parser.add_option("--forceRNA", action="store_true", dest="forceRNA")
67 parser.add_option("--flag", action="store_true", dest="flagReads")
68 parser.add_option("--strict", type="int", dest="minSpliceLength",
69 help="min required bp on each side of a splice")
70 parser.add_option("--spliceonly", action="store_true", dest="spliceOnly")
71 parser.add_option("--verbose", action="store_true", dest="verbose")
72 parser.add_option("--cache", type="int", dest="cachePages")
73 parser.add_option("--RNA", dest="geneDataFileName")
75 configParser = getConfigParser()
76 section = "makerdsfromblat"
77 init = getConfigBoolOption(configParser, section, "init", True)
78 doIndex = getConfigBoolOption(configParser, section, "doIndex", False)
79 trimReadID = getConfigBoolOption(configParser, section, "trimReadID", True)
80 minSpliceLength = getConfigIntOption(configParser, section, "minSpliceLength", 0)
81 forceRNA = getConfigBoolOption(configParser, section, "forceRNA", False)
82 flagReads = getConfigBoolOption(configParser, section, "flagReads", False)
83 spliceOnly = getConfigBoolOption(configParser, section, "spliceOnly", False)
84 verbose = getConfigBoolOption(configParser, section, "verbose", False)
85 cachePages = getConfigIntOption(configParser, section, "cachePages", 100000)
86 geneDataFileName = getConfigOption(configParser, section, "geneDataFileName", "")
88 parser.set_defaults(init=init, doIndex=doIndex, trimReadID=trimReadID, minSpliceLength=minSpliceLength, forceRNA=forceRNA,
89 flagReads=flagReads, spliceOnly=spliceOnly, verbose=verbose, cachePages=cachePages,
90 geneDataFileName=geneDataFileName)
95 def makerdsfromblat(label, filename, outdbname, dataType="DNA", init=True,
96 doIndex=False,trimReadID=True, minSpliceLength=0,
97 forceRNA=False, theFlag="", spliceOnly=False,
98 verbose=False, cachePages=100000, geneDataFileName="",
108 print "forcing datatype to RNA"
111 if dataType == "RNA":
112 genedatafile = open(geneDataFileName)
114 writeLog(outdbname + ".log", verstring, string.join(sys.argv[1:]))
118 if dataType == "RNA" and not forceRNA:
119 for line in genedatafile:
120 fields = line.strip().split("\t")
121 blockCount = int(fields[7])
128 chromstarts = fields[8][:-1].split(",")
129 chromstops = fields[9][:-1].split(",")
132 for index in range(blockCount):
133 chromstarts[index] = int(chromstarts[index])
134 chromstops[index] = int(chromstops[index])
135 exonLengths.append(chromstops[index] - chromstarts[index])
136 totalLength += exonLengths[index]
138 geneDict[uname] = (sense, blockCount, totalLength, chrom, chromstarts, exonLengths)
143 rds = ReadDataset.ReadDataset(outdbname, init, dataType, verbose=True)
145 #check that our cacheSize is better than the dataset's default cache size
146 defaultCacheSize = rds.getDefaultCacheSize()
147 if cachePages > defaultCacheSize:
149 rds.setDBcache(cachePages, default=True)
151 rds.setDBcache(cachePages)
153 if not init and doIndex:
159 print "couldn't drop Index"
162 if len(propertyList) > 0:
163 rds.insertMetadata(propertyList)
165 # make some assumptions based on first read
166 infile = open(filename, "r")
168 line = infile.readline()
170 fields = line.split()
171 readsize = int(fields[10])
172 pairedTest = fields[9][-2:]
174 if pairedTest in ["/1", "/2"]:
175 print "assuming reads are paired"
178 print "read size: %d bp" % readsize
180 rds.insertMetadata([("readsize", readsize)])
182 rds.insertMetadata([("paired", "True")])
185 if "blat_mapped" not in rds.getMetadata():
186 rds.insertMetadata([("blat_mapped", "True")])
188 minReadScore = readsize - readsize/25 - 1
190 if dataType == "RNA":
191 maxBorder = readsize + trim
193 infile = open(filename, "r")
199 index = uIndex = mIndex = sIndex = lIndex = 0
203 line = infile.readline()
207 fields = line.strip().split()
210 readID = string.join(readID.split(":")[1:], ":")
214 if bestScore > minReadScore:
215 for readData in readList:
216 if readData[1] == bestScore:
217 newReadList.append(readData)
220 prevID = label + "-" + prevID
222 listlen = len(newReadList)
224 parts = int(newReadList[0][0])
225 if parts == 1 and not spliceOnly:
226 (part, score, sense, chrom, start, mismatches) = newReadList[0]
227 stop = start + readsize
228 uInsertList.append((prevID, chrom, start, stop, sense, 1.0, theFlag, mismatches))
230 elif forceRNA and parts == 2:
231 (part, score, sense, chrom, startList, lengthList, mismatchList) = newReadList[0]
232 startL = int(startList[0])
233 stopL = startL + int(lengthList[0])
234 startR = int(startList[1])
235 stopR = startR + int(lengthList[1])
236 if stopL + minIntron < startR:
237 sInsertList.append((prevID, chrom, startL, stopL, startR, stopR, sense, 1.0, theFlag, mismatches))
241 (part, score, sense, chrom, start, mismatches) = newReadList[0]
242 currentSplice = chrom
243 (model, spliceID, regionStart) = currentSplice.split(delimiter)
244 if model not in geneDict:
248 (gsense, blockCount, transLength, chrom, chromstarts, blockSizes) = geneDict[model]
249 spliceID = int(spliceID)
250 rstart = int(start) - 2
251 lefthalf = maxBorder - rstart
252 if lefthalf < 1 or lefthalf > maxBorder:
255 righthalf = readsize - lefthalf
256 startL = int(regionStart) + rstart
257 stopL = startL + lefthalf
258 startR = chromstarts[spliceID + 1]
259 stopR = chromstarts[spliceID + 1] + righthalf
260 if stopL + minIntron < startR:
261 sInsertList.append((prevID, chrom, startL, stopL, startR, stopR, sense, 1.0, theFlag, mismatches))
263 elif listlen > 1 and not spliceOnly:
264 prevID = prevID + "::" + str(listlen)
266 # ignore multireads that can also map across splices
268 for readData in newReadList:
273 for (part, score, sense, chrom, start, mismatches) in newReadList:
274 stop = start + readsize
275 mInsertList.append((prevID, chrom, start, stop, sense, 1.0 / listlen, theFlag, mismatches))
279 if index % insertSize == 0:
280 rds.insertUniqs(uInsertList)
281 rds.insertMulti(mInsertList)
284 if dataType == "RNA":
285 rds.insertSplices(sInsertList)
291 # start processing new read
298 score = int(fields[0])
301 parts = int(fields[17])
304 lengthList = fields[18][:-1].split(",")
305 startList = fields[20][:-1].split(",")
306 listlen = len(lengthList)
307 for lpos in range(listlen):
308 if int(lengthList[lpos]) < minSpliceLength:
311 # throw out deletions, for now
313 if int(lengthList[lpos - 1]) == int(startList[lpos]):
317 start = int(fields[15])
320 if score > bestScore:
324 if int(fields[1]) > 0:
326 mismatches = decodeMismatches(fields[-1].upper(), fields[-2].upper(), sense)
331 readList.append((parts, score, sense, chrom, start, mismatches))
333 readList.append((parts, score, sense, chrom, startList, lengthList, mismatches))
335 if lIndex % 1000000 == 0:
336 print "processed %d lines" % lIndex
338 print "%d lines processed" % lIndex
340 if len(uInsertList) > 0:
341 rds.insertUniqs(uInsertList)
342 if len(mInsertList) > 0:
343 rds.insertMulti(mInsertList)
344 if len(sInsertList) > 0:
345 rds.insertSplices(sInsertList)
347 combString = "%d unique reads" % uIndex
348 combString += "\t%d multi reads" % mIndex
349 if dataType == "RNA":
350 combString += "\t%d spliced reads" % sIndex
353 print combString.replace("\t", "\n")
355 writeLog(outdbname + ".log", verstring, combString)
358 print "building index...."
359 if cachePages > defaultCacheSize:
360 rds.setDBcache(cachePages)
361 rds.buildIndex(cachePages)
363 rds.buildIndex(defaultCacheSize)
366 def decodeMismatches(gString, rString, rsense):
371 for rindex in xrange(rlen):
375 if gString[rindex] == rString[rindex]:
378 genNT = gString[rindex]
379 readNT = rString[rindex]
380 # for eland-compatibility, we are 1-based
381 output.append("%s%d%s" % (readNT, rindex + 1 - partIndex, genNT))
383 return string.join(output, ",")
386 if __name__ == "__main__":