Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from Bio.SeqIO.FastaIO import SimpleFastaParser
- from Bio import SeqIO
- SEQ = 'ATCGN'* 10 + '\n'
- # Creating fasta files
- with open('chr1.fa', 'w') as out_f:
- out_f.write('>1\n')
- for _ in range(4979128): # roughly the size of human chromosome 1, nearly 250Mb
- out_f.write(SEQ)
- with open('chrUnknown.fa', 'w') as out_f:
- for i in range(1000000-1):
- out_f.write('>{}\n'.format(i))
- out_f.write(SEQ)
- out_f.write('>Last\n')
- out_f.write(SEQ.rstrip()) # avoid new-line at end-of-file
- # New parser
- def new_parser(handle):
- # Skip any text before the first record (e.g. blank lines, comments)
- for line in handle:
- if line[0] == '>':
- title = line[1:].rstrip()
- break
- # Main work
- # Remove trailing whitespace, and any internal spaces
- # (and any embedded \r which are possible in mangled files
- # when not opened in universal read lines mode)
- lines = []
- for line in handle:
- if line[0] == '>':
- if lines:
- yield title, ''.join(lines).replace(" ", "").replace("\r", "")
- lines = []
- title = line[1:].rstrip()
- continue
- lines.append(line.rstrip())
- if lines:
- yield title, ''.join(lines).replace(" ", "").replace("\r", "")
- def use_parser(parser):
- with open(in_file) as handle:
- return list(parser(handle))
- def use_seqIO_parse():
- return list(SeqIO.parse(in_file, 'fasta'))
- def sanity_check():
- if use_parser(SimpleFastaParser) != use_parser(new_parser):
- raise Exception('Old and new parsers produce different outputs!')
- from timeit import timeit
- def timings():
- print('old simple parser', timeit('use_parser(SimpleFastaParser)', setup='from Bio.SeqIO.FastaIO import SimpleFastaParser; from __main__ import use_parser', number = 20))
- print('new simple parser', timeit('use_parser(new_parser)', setup='from __main__ import new_parser, use_parser', number = 20))
- print('regular parser', timeit('use_seqIO_parse()', setup='from __main__ import use_seqIO_parse', number = 20))
- print('Timings: 1 large sequence')
- in_file = 'chr1.fa'
- sanity_check()
- timings()
- print('\nTimings: 1,000,000 small sequences')
- in_file = 'chrUnknown.fa'
- sanity_check()
- timings()
- # New two line parser
- def new_two_line_parser(handle):
- for idx, line in enumerate(handle):
- if idx % 2 == 0: # header line
- if line[0] != '>':
- if line.strip():
- raise ValueError("Expected FASTA record starting with '>' character. "
- "Perhaps this file is using FASTA line wrapping? "
- "Got: {}".format(line))
- else:
- raise ValueError("Expected FASTA record starting with '>' character. "
- "If using two-lines-per-record, there should be no "
- "blank lines between records, or at the end of file.")
- title = line[1:].rstrip()
- else: # sequence line
- line = line.strip()
- if not line:
- raise ValueError("Premature end of FASTA file (or this is not strict "
- "two-line-per-record FASTA format), expected one line "
- "of sequence following: '{}'".format(title))
- elif line[0] == '>':
- raise ValueError("Two '>' FASTA lines in a row. Missing sequence line "
- "if this is strict two-line-per-record FASTA format. "
- "Have '>{}' and '{}'".format(title, line))
- yield title, line
- if idx % 2 == 0:
- raise ValueError("Should be at end of file, but line= '{}'".format(line))
- print('\nTimings 1,000,000 small sequences, 2 line parsers')
- from Bio.SeqIO.FastaIO import FastaTwoLineParser
- in_file = 'chrUnknown.fa'
- assert use_parser(FastaTwoLineParser) == use_parser(new_two_line_parser)
- print('old simple two-line parser', timeit('use_parser(SimpleFastaParser)', setup='from Bio.SeqIO.FastaIO import SimpleFastaParser; from __main__ import use_parser', number = 20))
- print('new simple two-line parser', timeit('use_parser(new_two_line_parser)', setup='from __main__ import new_two_line_parser, use_parser', number = 20))
Advertisement
Add Comment
Please, Sign In to add comment