def parse_fasta_header(header, seqid_fn=None):
    """
  Parses a FASTA format header (with our without the initial '>').

  If NCBI SeqID format (gi|gi-number|gb|accession etc, is detected
  the first id in the list is used as the canonical id (see see
  http://www.ncbi.nlm.nih.gov/books/NBK21097/#A631 ).

  Extra processing to parse the seqid can be provided
  by giving a seqid_fn function.

  Returns a tuple of sequence id and sequence name/description.
  """
    if header[0] == '>':
        header = header[1:]
    if header.find('|') != -1 and '|' in header.split()[0]:
        tokens = header.split('|')
        seqid = '%s|%s' % (tokens[0], tokens[1].split()[0])
        name = seqid + ' ' + tokens[-1].strip()
    else:
        tokens = header.split()
        seqid = tokens[0]
        name = header[0:-1].strip()
    if seqid_fn is not None:
        seqid = seqid_fn(seqid)
    return seqid, name
