def check_index(self): """Function to check bowtie index. Returns True is index exist on disk. """ if hasattr(self, 'bowtie2_index'): return (pu.check_bowtie2index(self.bowtie2_index)) return False
def __init__(self, bowtie2_index, **kwargs): """Bowtie2 constructor. Initialize bowtie2 index and other parameters. """ super().__init__() self.programName = "bowtie2" self.dep_list = [self.programName] if not pe.check_dependencies(self.dep_list): raise Exception("ERROR: " + self.programName + " not found.") self.valid_args = [ '-x', '-1', '-2', '-U', '--interleaved', '-S', '-b', '-q', '--tab5', '--tab6', '--qseq', '-f', '-r', '-F', '-c', '-s', '-u', '-5', '-3', '--trim-to', '--phred33', '--phred64', '--int-quals', '--very-fast', '--fast', '--sensitive', '--very-sensitive', '--very-fast-local', '--fast-local', '--sensitive-local', '--very-sensitive-local', '-N', '-L', '-i', '--n-ceil', '--dpad', '--gbar', '--ignore-quals', '--nofw', '--norc', '--no-1mm-upfront', '--end-to-end', '--local', '--ma', '--mp', '--np', '--rdg', '--rfg', '--score-min', '-k', '-a', '-D', '-R', '-I', '-X', '--fr', '--rf', '--ff', '--no-mixed', '--no-discordant', '--dovetail', '--no-contain', '--no-overlap', '--align-paired-reads', '--preserve-tags', '-t', '--un', '--al', '--un-conc', '--al-conc', '--un-gz', '--quiet', '--met-file', '--met-stderr', '--met', '--no-unal', '--no-head', '--no-sq', '--rg-id', '--rg', '--omit-sec-seq', '--sam-no-qname-trunc', '--xeq', '--soft-clipped-unmapped-tlen', '-p', '--threads', '--reorder', '--mm', '--qc-filter', '--seed', '--non-deterministic', '--version', '-h', '--help' ] #initialize the passed arguments self.passedArgumentDict = kwargs #if index is passed, update the passed arguments if len(bowtie2_index) > 0 and pu.check_bowtie2index(bowtie2_index): print("Bowtie2 index is: " + bowtie2_index) self.bowtie2_index = bowtie2_index self.passedArgumentDict['-x'] = self.bowtie2_index else: print( "No Bowtie2 index provided. Please build index now to generate an index..." )
def build_index(self, index_path, index_name, *args, threads=None, overwrite=False, verbose=False, quiet=False, logs=True, objectid="NA", **kwargs): """Build a bowtie2 index with given parameters and saves the new index to self.bowtie2_index. Parameters ---------- index_path: string Path where the index will be created index_name: string A name for the index args: tuple Path to reference input files threads: int Num threads to use overwrite: bool Overwrite already existing index verbose: bool Print stdout and std error quiet: bool Print nothing logs: bool Log this command to pyrpipe logs objectid: str Provide an id to attach with this command e.g. the SRR accession. This is useful for debugging, benchmarking and reports. kwargs: dict Options to pass to bowtie2. This will override the existing options in self.passed_args_dict (only replace existing arguments and not replace all the arguments). :return: Returns the status of bowtie-build :rtype: bool """ #check input references if len(args) < 1: pu.print_boldred( "No reference sequence provided to bowtie2-build. Exiting") return False if not pu.check_files_exist(*args): pu.print_boldred( "Please check input reference sequences provided to bowtie2-build. Exiting" ) return False bowtie2_build_args = [ '-f', '-c', '--large-index', '--debug', '--sanitized', '--verbose', '-a', '--noauto', '-p', '--packed', '--bmax', '--bmaxdivn', '--dcv', '--nodc', '-r', '--noref', '-3', '--justref', '-o', '--offrate', '-t', '--ftabchars', '--threads', '--seed', '-q', '--quiet', '-h', '--help', '--usage', '--version' ] #create the out dir if not pu.check_paths_exist(index_path): if not pu.mkdir(index_path): print( "ERROR in building bowtie2 index. Failed to create index directory." ) return False if not overwrite: #check if files exists if pu.check_bowtie2index(os.path.join(index_path, index_name)): print( "bowtie2 index with same name already exists. Exiting...") self.bowtie2_index = os.path.join(index_path, index_name) return True bowtie2Build_Cmd = ['bowtie2-build'] if not threads: threads = self.threads newopts = {"--threads": str(threads)} mergedopts = {**newopts, **kwargs} #add options bowtie2Build_Cmd.extend( pu.parse_unix_args(bowtie2_build_args, mergedopts)) #add input files bowtie2Build_Cmd.append(str(",".join(args))) #add dir/basenae bowtie2Build_Cmd.append(os.path.join(index_path, index_name)) #print("Executing:"+str(" ".join(hisat2Build_Cmd))) #start ececution status = pe.execute_command(bowtie2Build_Cmd, verbose=verbose, quiet=quiet, logs=logs, objectid=objectid) if not status: pu.print_boldred("bowtie2-build failed") return False #check index files if not pu.check_bowtie2index(os.path.join(index_path, index_name)): pu.print_boldred("bowtie2-build failed") return False #set the index path self.bowtie2_index = os.path.join(index_path, index_name) #return status return True
def build_index(self, index_path, genome, objectid="NA"): """Build a bowtie2 index with given parameters and saves the new index to self.index. Parameters ---------- index_path: string Path where the index will be created genome: string Path to the reference genome objectid : string Provide an id to attach with this command e.g. the SRR accession. This is useful for debugging, benchmarking and reports. :return: Returns the status of bowtie2-build :rtype: bool """ #check input references if not _force: if pu.check_bowtie2index(index_path): pu.print_green( "bowtie index {} already exists.".format(index_path)) self.index = index_path return True #check input files if not (pu.check_files_exist(genome)): pu.print_boldred( "Please provide a valid input fasta file to build bowtie2 index" ) raise ValueError("Please check input to star build index") return False bowtie2_build_args = [ '-f', '-c', '--large-index', '--debug', '--sanitized', '--verbose', '-a', '--noauto', '-p', '--packed', '--bmax', '--bmaxdivn', '--dcv', '--nodc', '-r', '--noref', '-3', '--justref', '-o', '--offrate', '-t', '--ftabchars', '--threads', '--seed', '-q', '--quiet' ] #create the out dir indexdir = pu.get_file_directory(index_path) if not pu.check_paths_exist(indexdir): if not pu.mkdir(indexdir): raise OSError( "Error creating bowtie2 index. Failed to create index directory." ) return False args = (genome, index_path) internal_kwargs = {"--threads": self._threads} #read build parameters yamlfile = os.path.join(_params_dir, 'bowtie2_index.yaml') if pu.check_files_exist(yamlfile): yaml_params = pl.YAML_loader(yamlfile) yaml_kwargs = yaml_params.get_kwargs() internal_kwargs = {**yaml_kwargs, **internal_kwargs} #add positional args internal_kwargs['--'] = args bowtie2Build_Cmd = ['bowtie2-build'] #add options bowtie2Build_Cmd.extend( pu.parse_unix_args(bowtie2_build_args, internal_kwargs)) #start ececution status = pe.execute_command(bowtie2Build_Cmd, objectid=objectid) if not status: pu.print_boldred("bowtie2-build failed") return False if status: if pu.check_bowtie2index(index_path) and not _dryrun: #update object's index self.index = index_path if self.check_index(): return True else: raise OSError("Error building bowtie2 index") return True