def primary_transcript(entrystream, parenttype='<STR_LIT>', logstream=stderr):
for entry in entrystream:<EOL><INDENT>if not isinstance(entry, tag.Feature):<EOL><INDENT>yield entry<EOL>continue<EOL><DEDENT>for parent in tag.select.features(entry, parenttype, traverse=True):<EOL><INDENT>if parent.num_children == <NUM_LIT:0>:<EOL><INDENT>continue<EOL><DEDENT>transcripts = defaultdict(list)<EOL>for child in parent.children:<EOL><INDENT>if child.type in type_terms:<EOL><INDENT>transcripts[child.type].append(child)<EOL><DEDENT><DEDENT>if len(transcripts) == <NUM_LIT:0>:<EOL><INDENT>continue<EOL><DEDENT>ttypes = list(transcripts.keys())<EOL>ttype = _get_primary_type(ttypes, parent)<EOL>transcript_list = transcripts[ttype]<EOL>if ttype == '<STR_LIT>':<EOL><INDENT>_emplace_pmrna(transcript_list, parent, strict=True)<EOL><DEDENT>else:<EOL><INDENT>_emplace_transcript(transcript_list, parent)<EOL><DEDENT><DEDENT>yield entry<EOL><DEDENT>
Select a single transcript as a representative for each gene.

This function is a generalization of the `primary_mrna` function that
attempts, under certain conditions, to select a single transcript as a
representative for each gene. If a gene encodes multiple transcript types,
one of those types must be **mRNA** or the function will complain loudly
and fail.

For mRNAs, the primary transcript is selected according to translated
length. For all other transcript types, the length of the transcript
feature itself is used. I'd be eager to hear suggestions for alternative
selection criteria.

Like the `primary_mrna` function, this function **does not** return only
transcript features. It **does** modify gene features to ensure that each
has at most one transcript feature.

>>> reader = tag.GFF3Reader(tag.pkgdata('psyllid-mixed-gene.gff3.gz'))
>>> gene_filter = tag.select.features(reader, type='gene')
>>> trans_filter = tag.transcript.primary_transcript(gene_filter)
>>> for gene in trans_filter:
...     assert gene.num_children == 1

In cases where the direct children of a gene feature have heterogenous
types, the `primary_mrna` function will only discard mRNA features. This
function, however, will discard all direct children of the gene that are
not the primary transcript, including non-transcript children. This is a
retty subtle distinction, and anecdotal experience suggests that cases in
which the distinction actually matters are extremely rare.