Skip to content

Instantly share code, notes, and snippets.

@daler
Created February 7, 2015 18:39
Show Gist options
  • Save daler/c637e96d640c1bbe5d8b to your computer and use it in GitHub Desktop.
Save daler/c637e96d640c1bbe5d8b to your computer and use it in GitHub Desktop.
fasta from BED-like file with dashes
import pybedtools
# This demo uses files that ship with pybedtools
a = pybedtools.example_bedtool('a.bed')
fasta = pybedtools.example_filename('test.fa')
# Use a properly-formatted BED file, and then post-process the resulting fasta.
x = a.sequence(fi=fasta, s=True)
for i, line in enumerate(open(x.seqfn)):
if line.startswith('>') and i >0:
print '>\n------------'
print line,
# Alternative method: use a text file with dashes, without stripping the dashes first.
# First make a version of `a.bed` that has dashes every other line
with open('a-with-dashes.txt', 'w') as fout:
for i in a:
fout.write(str(i))
fout.write('------------\n')
for line in open('a-with-dashes.txt'):
if line.startswith('------'):
print('>\n--------')
else:
print open(
pybedtools.BedTool(line, from_string=True)\
.sequence(fi=fasta, s=True).seqfn
).read(),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment