def runSam2bam(inFile, outFile, log, index=True, sort=True, delinFile=False, onlyUnique=False, onlyProperPaired=False, filterMQ=0, L=None, threads=1, verbose=False, dry=False): if(delinFile and files_exist(outFile) and not files_exist(inFile)): print("Skipping sam2bam for " + outFile, file=log) else: if(onlyUnique and filterMQ == 0): filterMQ = 1; success = True cmd = [getBinary("samtools"), "view", "-@", str(threads), "-Sb", "-o", outFile, inFile] if filterMQ > 0: cmd+=["-q", str(filterMQ)] if onlyProperPaired: cmd+=["-f", "2"] if not L is None: cmd+=["-L", L] run(" ".join(cmd), log, verbose=verbose, dry=dry) if(sort): tmp = outFile + "_tmp" if(not dry): os.rename(outFile, tmp) run(" ".join([getBinary("samtools"), "sort", "-@", str(threads), "-o", outFile, tmp]), log, verbose=verbose, dry=dry) if(success): removeFile(tmp) if(success and delinFile): if(not dry): removeFile(inFile) if(index): pysamIndex(outFile)
def runSam2bam(inFile, outFile, log, index=True, sort=None, delinFile=False, onlyUnique=False, onlyProperPaired=False, filterMQ=0, L=None, threads=1, verbose=False, dry=False): if delinFile and files_exist(outFile) and not files_exist(inFile): print("Skipping sam2bam for %s" % outFile, file=log) else: if onlyUnique and filterMQ == 0: filterMQ = 1 success = True cmd = [ "samtools view", "-@", str(threads), "-Sb", "-o", outFile, inFile ] if filterMQ > 0: cmd += ["-q", str(filterMQ)] if onlyProperPaired: cmd += ["-f", "2"] if L is not None: cmd += ["-L", L] run(" ".join(cmd), log, verbose=verbose, dry=dry) if sort is not None: tmp = outFile + "_tmp" if not dry: os.rename(outFile, tmp) if sort.lower() == "index": run(" ".join( ["samtools sort", "-@", str(threads), "-o", outFile, tmp]), log, verbose=verbose, dry=dry) elif sort.lower() == "name": run(" ".join([ "samtools sort -n", "-@", str(threads), "-o", outFile, tmp ]), log, verbose=verbose, dry=dry) if success: removeFile(tmp) if success and delinFile: if not dry: removeFile(inFile) if index: pysamIndex(outFile)
def bamSort(outputBAM, log, newHeader, verbose): tmp = outputBAM + "_tmp" if(newHeader != None): pyOutputBAM = pysam.AlignmentFile(outputBAM, "rb") pyTmp = pysam.AlignmentFile(tmp, "wb", header=newHeader) for read in pyOutputBAM: pyTmp.write(read) pyOutputBAM.close() pyTmp.close() else: os.rename(outputBAM, tmp) #run(" ".join(["samtools", "sort", "-@", str(threads) , tmp, replaceExtension(outFile, "")]), log, verbose=verbose, dry=dry) run(" ".join([getBinary("samtools"), "sort", "-o", outputBAM, tmp]), log, verbose=verbose, dry=False) #pysam.sort(tmp, outputBAM) # @UndefinedVariable removeFile(tmp)
def bamSort(outputBAM, log, newHeader, verbose): tmp = outputBAM + "_tmp" if(newHeader != None): pyOutputBAM = pysam.AlignmentFile(outputBAM, "rb") pyTmp = pysam.AlignmentFile(tmp, "wb", header=newHeader) for read in pyOutputBAM: pyTmp.write(read) pyOutputBAM.close() pyTmp.close() else: os.rename(outputBAM, tmp) #run(" ".join(["samtools", "sort", "-@", str(threads) , tmp, replaceExtension(outFile, "")]), log, verbose=verbose, dry=dry) run(" ".join([getBinary("samtools"), "sort", "-o", outputBAM, tmp]), log, verbose=verbose, dry=False) #pysam.sort(tmp, outputBAM) # @UndefinedVariable removeFile(tmp)
def bamSort(outputBAM, log, newHeader, paired, verbose): tmp = outputBAM + "_tmp" if newHeader is not None: pyOutputBAM = pysam.AlignmentFile(outputBAM, "rb") pyTmp = pysam.AlignmentFile(tmp, "wb", header=newHeader) for read in pyOutputBAM: pyTmp.write(read) pyOutputBAM.close() pyTmp.close() else: os.rename(outputBAM, tmp) if not paired: run("samtools sort %s -o %s" % (tmp, outputBAM), log, verbose=verbose, dry=False) else: run("samtools sort -n %s -o %s" % (tmp, outputBAM), log, verbose=verbose) removeFile(tmp)