Skip to content

Instantly share code, notes, and snippets.

@IgorFobia
Created August 5, 2013 00:30
Show Gist options
  • Save IgorFobia/6152690 to your computer and use it in GitHub Desktop.
Save IgorFobia/6152690 to your computer and use it in GitHub Desktop.
Roll a FASTA file of a circular sequence, changing the starting base of the FASTA sequence
#!/usr/bin/python
# Roll a FASTA file of a circular sequence, changing the starting base of the FASTA sequence to new_startN
from Bio import SeqIO
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
import sys
import os
def roll_fasta(fasta_file, new_startN):
'''
roll fasta of circular genome so that new_startN is new start of sequence
'''
python_new_startN = new_startN - 1 # change to python index
SeqRecordObj = SeqIO.read(fasta_file, "fasta") # load fasta
# Create new record
new_first_part_fasta = SeqRecordObj.seq[python_new_startN : ]
new_last_part_fasta = SeqRecordObj.seq[: python_new_startN]
rolled_sequence = new_first_part_fasta + new_last_part_fasta
record = SeqRecord(rolled_sequence, '_'.join([SeqRecordObj.id , 'rolled', str(new_startN)]),'','')
# Create output file name
(dirName, fileName) = os.path.split(fasta_file)
(fileBaseName, fileExtension)=os.path.splitext(fileName)
output_fileName = '_'.join([fileBaseName, 'rolled', str(new_startN)]) + fileExtension
output_file = os.path.join(dirName, output_fileName)
# Save new fasta
output_handle = open(output_file , "w")
SeqIO.write(record, output_handle, "fasta")
output_handle.close()
def main(*argv):
fasta_file = argv[1]
new_startN = int(argv[2])
roll_fasta(fasta_file, new_startN)
if __name__ == '__main__':
sys.exit(main(*sys.argv))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment