def test_extractRead(self): bowtie2Wrap.extractRead(self.targetAlignFile, self.fastqOutFile) expectedReadId = [ "HWI-ST998R:270:H7NJ9ADXX:1:1101:1797:1927", "HWI-ST998R:270:H7NJ9ADXX:1:1101:1797:1927:A" ] with open(self.fastqOutFile, 'r') as fp: count = 0 for r in seqParse.parse(fp, 'fastq'): self.assertTrue( count < len(expectedReadId), "Extract Read: Expected number of reads Mismatch!") self.assertTrue(r.id == expectedReadId[count], "Extract Read: Expected Reads Mismatch!") count += 1 self.assertTrue( count == len(expectedReadId), "Extract Read: Expected number of reads Mismatch!")
def processPathoMap(pathoMapOptions): procPathoMapOptions = copyPathoMapOptions(pathoMapOptions) # Splitting reference files if bigger than MAX_REF_FILE_SIZE ptargetRefFiles = [] for filePath in pathoMapOptions.targetRefFiles: if pathoMapOptions.verbose: print "Checking whether the file: " + filePath + " needs to be split" files = splitCheck(filePath, pathoMapOptions.MAX_REF_FILE_SIZE) for f in files: ptargetRefFiles.append(f) procPathoMapOptions.targetRefFiles = ptargetRefFiles pfilterRefFiles = [] for filePath in pathoMapOptions.filterRefFiles: if pathoMapOptions.verbose: print "Checking whether the file: " + filePath + " needs to be split" files = splitCheck(filePath, pathoMapOptions.MAX_REF_FILE_SIZE) for f in files: pfilterRefFiles.append(f) procPathoMapOptions.filterRefFiles = pfilterRefFiles # Creating Index if it does not exist bowtie2Options = bowtie2Wrap.Bowtie2Options() bowtie2Options.verbose = procPathoMapOptions.verbose bowtie2Options.btHome = procPathoMapOptions.btHome bowtie2Options.indexDir = procPathoMapOptions.indexDir for filePath in ptargetRefFiles: bowtie2Options.refFile = filePath (_, tail) = os.path.split(filePath) (base, _) = os.path.splitext(tail) bowtie2Options.btIndexPrefix = base if pathoMapOptions.verbose: print "Creating bowtie2 index for: " + filePath bowtie2Wrap.create_bowtie2_index(bowtie2Options) procPathoMapOptions.targetIndexPrefixes.append(base) for filePath in pfilterRefFiles: bowtie2Options.refFile = filePath (_, tail) = os.path.split(filePath) (base, _) = os.path.splitext(tail) bowtie2Options.btIndexPrefix = base if pathoMapOptions.verbose: print "Creating bowtie2 index for: " + filePath bowtie2Wrap.create_bowtie2_index(bowtie2Options) procPathoMapOptions.filterIndexPrefixes.append(base) # Creating the Alignment file bowtie2Options = bowtie2Wrap.Bowtie2Options() bowtie2Options.verbose = procPathoMapOptions.verbose bowtie2Options.btHome = procPathoMapOptions.btHome bowtie2Options.numThreads = procPathoMapOptions.numThreads bowtie2Options.outDir = procPathoMapOptions.outDir bowtie2Options.indexDir = procPathoMapOptions.indexDir bowtie2Options.readFile = procPathoMapOptions.inReadFile bowtie2Options.readFilePair1 = procPathoMapOptions.inReadFilePair1 bowtie2Options.readFilePair2 = procPathoMapOptions.inReadFilePair2 if (len(procPathoMapOptions.inReadFilePair1) > 0 and len(procPathoMapOptions.inReadFilePair2) > 0): bowtie2Options.pairedReadFlag = True if procPathoMapOptions.targetAlignParameters is not None: bowtie2Options.additionalOptions = procPathoMapOptions.targetAlignParameters for indexPrefix in procPathoMapOptions.targetIndexPrefixes: bowtie2Options.btIndexPrefix = procPathoMapOptions.indexDir + os.sep + indexPrefix bowtie2Options.outAlignFile = procPathoMapOptions.exp_tag + indexPrefix + ".sam" if pathoMapOptions.verbose: print "Creating bowtie2 alignment: " + bowtie2Options.outAlignFile bowtie2Wrap.run_bowtie2(bowtie2Options) procPathoMapOptions.targetAlignFiles.append( procPathoMapOptions.outDir + os.sep + bowtie2Options.outAlignFile) # Appending the Alignment files and Filtering if len(procPathoMapOptions.targetAlignFiles) > 1: appendAlignFile = procPathoMapOptions.outDir + os.sep + procPathoMapOptions.exp_tag + "appendAlign.sam" if pathoMapOptions.verbose: print "Appending alignment files to: " + appendAlignFile append_file(appendAlignFile, procPathoMapOptions.targetAlignFiles) else: appendAlignFile = procPathoMapOptions.targetAlignFiles[0] if len(procPathoMapOptions.filterIndexPrefixes) > 0: bowtie2Options.readFile = procPathoMapOptions.outDir + os.sep + procPathoMapOptions.exp_tag + "appendAlign.fq" bowtie2Options.readFilePair1 = "" bowtie2Options.readFilePair2 = "" bowtie2Options.pairedReadFlag = False if procPathoMapOptions.filterAlignParameters is not None: bowtie2Options.additionalOptions = procPathoMapOptions.filterAlignParameters bowtie2Wrap.extractRead(appendAlignFile, bowtie2Options.readFile) for indexPrefix in procPathoMapOptions.filterIndexPrefixes: bowtie2Options.btIndexPrefix = procPathoMapOptions.indexDir + os.sep + indexPrefix bowtie2Options.outAlignFile = procPathoMapOptions.exp_tag + indexPrefix + ".sam" if pathoMapOptions.verbose: print "Creating bowtie2 alignment: " + bowtie2Options.outAlignFile bowtie2Wrap.run_bowtie2(bowtie2Options) procPathoMapOptions.filterAlignFiles.append( procPathoMapOptions.outDir + os.sep + bowtie2Options.outAlignFile) # Filtering the Alignment file outAlignFile = procPathoMapOptions.outDir + os.sep + procPathoMapOptions.outAlignFile if pathoMapOptions.verbose: print "Filtering and creating the alignment: " + outAlignFile if len(procPathoMapOptions.filterAlignFiles) > 0: filter_alignment(appendAlignFile, procPathoMapOptions.filterAlignFiles, outAlignFile) elif ((len(procPathoMapOptions.targetAlignFiles) > 1) or \ (len(procPathoMapOptions.targetIndexPrefixes) > 0)): os.rename(appendAlignFile, outAlignFile) else: # Input appendAlignFile provided by user, hence make a copy for outAlignFile shutil.copy(appendAlignFile, outAlignFile)