def filter_fasta(input_seqs_f, output_seqs_f, seqs_to_keep, negate=False, seqid_f=None):
 if (seqid_f is None):
  seqs_to_keep_lookup = {}.fromkeys([seq_id.split()[0] for seq_id in seqs_to_keep])
  if (not negate):
   def keep_seq(seq_id):
    return (seq_id.split()[0] in seqs_to_keep_lookup)
  else:
   def keep_seq(seq_id):
    return (seq_id.split()[0] not in seqs_to_keep_lookup)
 elif (not negate):
  keep_seq = seqid_f
 else:
  keep_seq = (lambda x: (not seqid_f(x)))
 for (seq_id, seq) in parse_fasta(input_seqs_f):
  if keep_seq(seq_id):
   output_seqs_f.write(('>%s\n%s\n' % (seq_id, seq)))
 output_seqs_f.close()

def filter_fasta ( input_seqs_f , output_seqs_f , seqs_to_keep , negate = False , seqid_f = None ) : if ( seqid_f is None ) : seqs_to_keep_lookup = { } . fromkeys ( [ seq_id . split ( ) [ 0 ] for seq_id in seqs_to_keep ] ) if ( not negate ) : def keep_seq ( seq_id ) : return ( seq_id . split ( ) [ 0 ] in seqs_to_keep_lookup ) else : def keep_seq ( seq_id ) : return ( seq_id . split ( ) [ 0 ] not in seqs_to_keep_lookup ) elif ( not negate ) : keep_seq = seqid_f else : keep_seq = ( lambda x : ( not seqid_f ( x ) ) ) for ( seq_id , seq ) in parse_fasta ( input_seqs_f ) : if keep_seq ( seq_id ) : output_seqs_f . write ( ( '>%s\n%s\n' % ( seq_id , seq ) ) ) output_seqs_f . close ( )
write filtered input_seqs to output_seqs_f which contains only seqs_to_keep input_seqs can be the output of parse_fasta or parse_fastq .