original_docstring: 
      Add fasta sequences to the bed file for each line.  The sequences are substituted in at the given "substitutionPosition" column
      if a value is supplied, or they are appended to a new column if it is None.
      
docstring: Add fasta sequences to the bed file for each line.  The sequences are substituted in at the given "substitutionPosition" column
  if a value is supplied, or they are appended to a new column if it is None.
docstring_tokens: Add fasta sequences to the bed file for each line . The sequences are substituted in at the given " substitutionPosition " column if a value is supplied or they are appended to a new column if it is None .
code: def addSequenceToBed(bedFilePath: str, genomeFastaFilePath: str, substitutionPosition = None, verbose = False):
      """
      Add fasta sequences to the bed file for each line.  The sequences are substituted in at the given "substitutionPosition" column
      if a value is supplied, or they are appended to a new column if it is None.
      """
  
      # Get the names of intermediate files from the bed file name.
      fastaSequencesFilePath = bedFilePath.rsplit('.',1)[0] + "_sequences.fa"
      newBedFilePath = bedFilePath.rsplit('.',1)[0] + "_NEW.bed"
  
      # Generate the fasta sequences file.
      if verbose: print("Generating fasta sequences file...")
      bedToFasta(bedFilePath, genomeFastaFilePath, fastaSequencesFilePath)
  
      # Open both the fasta sequence file and original bed file to read from and the new bed file to write to.
      if verbose: print("Writing fasta sequences to new bed file...")
      with open(bedFilePath, 'r') as bedFile:
          with open(fastaSequencesFilePath, 'r') as fastaSequencesFile:
              with open(newBedFilePath, 'w') as newBedFile:
  
                  fastaFileIterator = FastaFileIterator(fastaSequencesFile)
  
                  # For every line in the bed file, grab it and the accompanying fasta sequence and write them to the new file!
                  for fastaEntry in fastaFileIterator:
  
                      bedColumns = bedFile.readline().split()
                      fastaSequence = fastaEntry.sequence
                      assert bedColumns, "No bed line found to accompany fasta sequence: " + fastaSequence
  
                      if substitutionPosition is None: bedColumns.append(fastaSequence)
                      else: bedColumns[substitutionPosition] = fastaSequence
  
                      newBedFile.write('\t'.join(bedColumns) + '\n')
  
                  # Make sure the files had the same number of lines.
                  checkBedFile = bedFile.readline()
                  assert not checkBedFile, "Extra bed line found: " + checkBedFile
  
      # Clean up files by deleting the intermediate sequences file and replacing the old bed file with the new one.
      if verbose: print("Success!  Deleting intermediate fasta sequences file and replacing old bed file...")
      os.remove(fastaSequencesFilePath)
      os.replace(newBedFilePath, bedFilePath)
code_tokens: def addSequenceToBed ( bedFilePath : str , genomeFastaFilePath : str , substitutionPosition = None , verbose = False ) : fastaSequencesFilePath = bedFilePath . rsplit ( '.' , 1 ) [ 0 ] + "_sequences.fa" newBedFilePath = bedFilePath . rsplit ( '.' , 1 ) [ 0 ] + "_NEW.bed" if verbose : print ( "Generating fasta sequences file..." ) bedToFasta ( bedFilePath , genomeFastaFilePath , fastaSequencesFilePath ) if verbose : print ( "Writing fasta sequences to new bed file..." ) with open ( bedFilePath , 'r' ) as bedFile : with open ( fastaSequencesFilePath , 'r' ) as fastaSequencesFile : with open ( newBedFilePath , 'w' ) as newBedFile : fastaFileIterator = FastaFileIterator ( fastaSequencesFile ) for fastaEntry in fastaFileIterator : bedColumns = bedFile . readline ( ) . split ( ) fastaSequence = fastaEntry . sequence assert bedColumns , "No bed line found to accompany fasta sequence: " + fastaSequence if substitutionPosition is None : bedColumns . append ( fastaSequence ) else : bedColumns [ substitutionPosition ] = fastaSequence newBedFile . write ( '\t' . join ( bedColumns ) + '\n' ) checkBedFile = bedFile . readline ( ) assert not checkBedFile , "Extra bed line found: " + checkBedFile if verbose : print ( "Success!  Deleting intermediate fasta sequences file and replacing old bed file..." ) os . remove ( fastaSequencesFilePath ) os . replace ( newBedFilePath , bedFilePath )
short_docstring: Add fasta sequences to the bed file for each line.
short_docstring_tokens: Add fasta sequences to the bed file for each line .
comment: """
      Add fasta sequences to the bed file for each line.  The sequences are substituted in at the given "substitutionPosition" column
      if a value is supplied, or they are appended to a new column if it is None.
      """$SEP$# Get the names of intermediate files from the bed file name.$SEP$# Generate the fasta sequences file.$SEP$# Open both the fasta sequence file and original bed file to read from and the new bed file to write to.$SEP$# For every line in the bed file, grab it and the accompanying fasta sequence and write them to the new file!$SEP$# Make sure the files had the same number of lines.$SEP$# Clean up files by deleting the intermediate sequences file and replacing the old bed file with the new one.