def sort_fasta_by_abundance(fasta_lines, fasta_out_f):
 seq_index = {}
 count = 0
 for (seq_id, seq) in parse_fasta(fasta_lines):
  count += 1
  try:
   seq_index[seq].append(seq_id)
  except KeyError:
   seq_index[seq] = [seq_id]
 seqs = []
 for (k, v) in seq_index.items():
  seqs.append((len(v), k, v))
  del seq_index[k]
 seqs.sort()
 for (count, seq, seq_ids) in seqs[::(-1)]:
  for seq_id in seq_ids:
   fasta_out_f.write(('>%s\n%s\n' % (seq_id, seq)))

def sort_fasta_by_abundance ( fasta_lines , fasta_out_f ) : seq_index = { } count = 0 for ( seq_id , seq ) in parse_fasta ( fasta_lines ) : count += 1 try : seq_index [ seq ] . append ( seq_id ) except KeyError : seq_index [ seq ] = [ seq_id ] seqs = [ ] for ( k , v ) in seq_index . items ( ) : seqs . append ( ( len ( v ) , k , v ) ) del seq_index [ k ] seqs . sort ( ) for ( count , seq , seq_ids ) in seqs [ : : ( - 1 ) ] : for seq_id in seq_ids : fasta_out_f . write ( ( '>%s\n%s\n' % ( seq_id , seq ) ) )
sort seqs in fasta_line by abundance .