original_docstring: Adds contaminants and reverses entries for a FASTA protein database.
  
      Call with single fasta file name.
      If "forward", make sequences plus contaminants,
      if "reverse", make reversed sequences with reversed contaminants,
      if "both", make concatenated target/decoy with contaminants.
      "contam_path" is optional fullpath name of a contaminants database to use instead of default
      
docstring: Adds contaminants and reverses entries for a FASTA protein database.
  Call with single fasta file name.
docstring_tokens: Adds contaminants and reverses entries for a FASTA protein database . Call with single fasta file name .
code: def main(fasta_file, forward=False, reverse=False, both=True, log_obj=None, contam_path=""):
      """Adds contaminants and reverses entries for a FASTA protein database.
  
      Call with single fasta file name.
      If "forward", make sequences plus contaminants,
      if "reverse", make reversed sequences with reversed contaminants,
      if "both", make concatenated target/decoy with contaminants.
      "contam_path" is optional fullpath name of a contaminants database to use instead of default
      """
      decoy_string = 'REV_'   # the string to denote decoy sequences
      ######################################
      # Change default contaminants file name here:
      CONTAMS = 'Thermo_contams.fasta'
      # or pass in a "contams_path"
      ######################################
  
      # open the "forward" and "reversed" output files
      if fasta_file.lower().endswith('.gz'):
          _file = os.path.splitext(fasta_file[:-3])[0]
      else:
          _file = os.path.splitext(fasta_file)[0]
      for_name = _file + '_for.fasta'
      for_file_obj = open(for_name, 'w')
      rev_name = _file + '_rev.fasta'
      rev_file_obj = open(rev_name, 'w')
  
      # create the name for the concatenated file (if later needed)
      both_name = _file + '_both.fasta'
  
      # create a log file to mirror screen output
      _folder = os.path.split(fasta_file)[0]
      if not log_obj:
          log_obj = open(os.path.join(_folder, 'fasta_utilities.log'), 'a')
      write = [None, log_obj]
      fasta_lib.time_stamp_logfile('\n>>> starting: reverse_fasta.py', log_obj)
  
      # create instances protein object and initialize counter
      prot = fasta_lib.Protein()
      pcount = 0
  
      # try to find the contaminants database file
      # If no contam file path provided, search for it in current directory
      _file = None
      if not contam_path:
          if os.path.exists(CONTAMS):
              _file = CONTAMS
          else:
              path = os.path.split(fasta_file)[0]
              if os.path.exists(os.path.join(path, CONTAMS)):
                  _file = os.path.join(path, CONTAMS)
      elif os.path.exists(contam_path) and os.path.isfile(contam_path):
          _file = contam_path
      elif os.path.isdir(contam_path) and os.path.exists(os.path.join(contam_path, CONTAMS)):
          _file = os.path.join(contam_path, CONTAMS)
  
      # create reader and add contaminants (if contams file was found)
      if _file:
          f = fasta_lib.FastaReader(_file)
          while f.readNextProtein(prot, check_for_errs=True):
              pcount += 1
              prot.printProtein(for_file_obj)
              rev = prot.reverseProtein(decoy_string)
              rev.printProtein(rev_file_obj)
          for obj in write:
              print('...there were %s contaminant entries in %s' %
                    ("{0:,d}".format(pcount), os.path.split(_file)[1]), file=obj)
      else:
          for obj in write:
              print('...WARNING: contaminants were not added', file=obj)
  
      # read proteins until EOF and write proteins to "forward" and "reversed" files
      f = fasta_lib.FastaReader(fasta_file)
  
      # error checking slows program execution, turn on if needed.
      # Reading and writing sequences always removes spaces and blank lines.
      while f.readNextProtein(prot, check_for_errs=False):
          pcount += 1
          prot.printProtein(for_file_obj)    # write to "forward" file
          rev = prot.reverseProtein(decoy_string)
          rev.printProtein(rev_file_obj)   # write to "reversed" file
      for_file_obj.close()
      rev_file_obj.close()
  
      # make concatenated output file if desired and print summary stats
      if both:
          both_file_obj = open(both_name, 'w')
          for_file_obj = open(for_name, 'r')
          rev_file_obj = open(rev_name, 'r')
          while True:
              line = for_file_obj.readline()
              if not line: break
              both_file_obj.write(str(line))
          while True:
              line = rev_file_obj.readline()
              if not line: break
              both_file_obj.write(str(line))
          both_file_obj.close()
          for obj in write:
              print('...%s total proteins written to %s' %
                    ("{0:,d}".format(2*pcount), os.path.split(both_name)[1]), file=obj)
  
      if forward:
          for obj in write:
              print('...%s proteins written to %s' %
                    ("{0:,d}".format(pcount), os.path.split(for_name)[1]), file=obj)
      if reverse:
          for obj in write:
              print('...%s proteins reversed and written to %s' %
                    ("{0:,d}".format(pcount), os.path.split(rev_name)[1]), file=obj)
  
      # close files and delete unwanted files
      for_file_obj.close()
      rev_file_obj.close()
      fasta_lib.time_stamp_logfile('>>> ending: reverse_fasta.py', log_obj)
      log_obj.close()
      if not forward:
          os.remove(for_name)
      if not reverse:
          os.remove(rev_name)
      return
code_tokens: def main ( fasta_file , forward = False , reverse = False , both = True , log_obj = None , contam_path = "" ) : decoy_string = 'REV_' CONTAMS = 'Thermo_contams.fasta' if fasta_file . lower ( ) . endswith ( '.gz' ) : _file = os . path . splitext ( fasta_file [ : - 3 ] ) [ 0 ] else : _file = os . path . splitext ( fasta_file ) [ 0 ] for_name = _file + '_for.fasta' for_file_obj = open ( for_name , 'w' ) rev_name = _file + '_rev.fasta' rev_file_obj = open ( rev_name , 'w' ) both_name = _file + '_both.fasta' _folder = os . path . split ( fasta_file ) [ 0 ] if not log_obj : log_obj = open ( os . path . join ( _folder , 'fasta_utilities.log' ) , 'a' ) write = [ None , log_obj ] fasta_lib . time_stamp_logfile ( '\n>>> starting: reverse_fasta.py' , log_obj ) prot = fasta_lib . Protein ( ) pcount = 0 _file = None if not contam_path : if os . path . exists ( CONTAMS ) : _file = CONTAMS else : path = os . path . split ( fasta_file ) [ 0 ] if os . path . exists ( os . path . join ( path , CONTAMS ) ) : _file = os . path . join ( path , CONTAMS ) elif os . path . exists ( contam_path ) and os . path . isfile ( contam_path ) : _file = contam_path elif os . path . isdir ( contam_path ) and os . path . exists ( os . path . join ( contam_path , CONTAMS ) ) : _file = os . path . join ( contam_path , CONTAMS ) if _file : f = fasta_lib . FastaReader ( _file ) while f . readNextProtein ( prot , check_for_errs = True ) : pcount += 1 prot . printProtein ( for_file_obj ) rev = prot . reverseProtein ( decoy_string ) rev . printProtein ( rev_file_obj ) for obj in write : print ( '...there were %s contaminant entries in %s' % ( "{0:,d}" . format ( pcount ) , os . path . split ( _file ) [ 1 ] ) , file = obj ) else : for obj in write : print ( '...WARNING: contaminants were not added' , file = obj ) f = fasta_lib . FastaReader ( fasta_file ) while f . readNextProtein ( prot , check_for_errs = False ) : pcount += 1 prot . printProtein ( for_file_obj ) rev = prot . reverseProtein ( decoy_string ) rev . printProtein ( rev_file_obj ) for_file_obj . close ( ) rev_file_obj . close ( ) if both : both_file_obj = open ( both_name , 'w' ) for_file_obj = open ( for_name , 'r' ) rev_file_obj = open ( rev_name , 'r' ) while True : line = for_file_obj . readline ( ) if not line : break both_file_obj . write ( str ( line ) ) while True : line = rev_file_obj . readline ( ) if not line : break both_file_obj . write ( str ( line ) ) both_file_obj . close ( ) for obj in write : print ( '...%s total proteins written to %s' % ( "{0:,d}" . format ( 2 * pcount ) , os . path . split ( both_name ) [ 1 ] ) , file = obj ) if forward : for obj in write : print ( '...%s proteins written to %s' % ( "{0:,d}" . format ( pcount ) , os . path . split ( for_name ) [ 1 ] ) , file = obj ) if reverse : for obj in write : print ( '...%s proteins reversed and written to %s' % ( "{0:,d}" . format ( pcount ) , os . path . split ( rev_name ) [ 1 ] ) , file = obj ) for_file_obj . close ( ) rev_file_obj . close ( ) fasta_lib . time_stamp_logfile ( '>>> ending: reverse_fasta.py' , log_obj ) log_obj . close ( ) if not forward : os . remove ( for_name ) if not reverse : os . remove ( rev_name ) return
short_docstring: Adds contaminants and reverses entries for a FASTA protein database.
short_docstring_tokens: Adds contaminants and reverses entries for a FASTA protein database .
comment: """Adds contaminants and reverses entries for a FASTA protein database.
  
      Call with single fasta file name.
      If "forward", make sequences plus contaminants,
      if "reverse", make reversed sequences with reversed contaminants,
      if "both", make concatenated target/decoy with contaminants.
      "contam_path" is optional fullpath name of a contaminants database to use instead of default
      """$SEP$# the string to denote decoy sequences$SEP$######################################$SEP$# Change default contaminants file name here:$SEP$# or pass in a "contams_path"$SEP$######################################$SEP$# open the "forward" and "reversed" output files$SEP$# create the name for the concatenated file (if later needed)$SEP$# create a log file to mirror screen output$SEP$# create instances protein object and initialize counter$SEP$# try to find the contaminants database file$SEP$# If no contam file path provided, search for it in current directory$SEP$# create reader and add contaminants (if contams file was found)$SEP$# read proteins until EOF and write proteins to "forward" and "reversed" files$SEP$# error checking slows program execution, turn on if needed.$SEP$# Reading and writing sequences always removes spaces and blank lines.$SEP$# write to "forward" file$SEP$# write to "reversed" file$SEP$# make concatenated output file if desired and print summary stats$SEP$# close files and delete unwanted files