Skip to content

Instantly share code, notes, and snippets.

@mattravenhall
Created February 16, 2018 16:44
Show Gist options
  • Save mattravenhall/2acb93626a1d72c06735448ab10256cc to your computer and use it in GitHub Desktop.
Save mattravenhall/2acb93626a1d72c06735448ab10256cc to your computer and use it in GitHub Desktop.
Given a list of sequences and contig names, write out a fasta.
def writeFasta(titles=[], sequences=[], filename='tmp.fasta'):
if len(titles) != len(sequences):
titles = ['contig_{}'.format(i) for i in range(len(sequences))]
# Initial new file
with open(filename, 'w') as fasta:
fasta.write('')
with open(filename, 'a') as fasta:
for t, s in zip(titles,sequences):
fasta.write('> {}\n'.format(t))
s_wNewlines = re.sub("(.{80})","\\1\n", s, 0, re.DOTALL)
fasta.write(s_wNewlines+'\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment