Sequence annotation¶
|
Sequence annotation: a collection of |
|
Single annotated interval on a sequence, such as a gene, mRNA, exon or CDS. |
- class picea.SequenceAnnotation(sequence=None)[source]¶
Sequence annotation: a collection of
SequenceIntervalobjects (genes, mRNAs, exons, …) stored by ID, and linked into a directed acyclic graph by theirParentattributes.Intervals with duplicate IDs are renamed (
ID_1,ID_2, …) with a warning.Examples
>>> gff = ( ... "ctg1\t.\tgene\t1\t90\t.\t+\t.\tID=gene1\n" ... "ctg1\t.\tmRNA\t1\t90\t.\t+\t.\tID=mRNA1;Parent=gene1\n" ... ) >>> annotation = SequenceAnnotation.from_gff(string=gff) >>> len(annotation) 2 >>> annotation["mRNA1"].parent ['gene1'] >>> [interval.ID for interval in annotation["gene1"].children] ['mRNA1']
- Parameters:
sequence (Optional[Sequence]) – Annotated sequence. If given, its
annotationis set to this annotation.
- property intervals¶
List of all intervals
- classmethod from_gtf(filename=None, string=None, sequence=None, link_parents=True)[source]¶
Read a GTF formatted file or string. Exactly one of
filenameorstringmust be given.GTF lines are linked by their
gene_idandtranscript_idattributes, andtranscriptlines becomemRNAintervals. Genes and transcripts that have no line of their own are created, spanning all their child intervals. Other intervals get IDs based on their transcript and type, e.g.<transcript_id>.exon_0. Lines without agene_idbecome intervals without parents, with IDs likerepeat_region_0.Examples
>>> gtf = ( ... 'ctg1\t.\texon\t100\t200\t.\t+\t.\tgene_id "g1"; transcript_id "t1";\n' ... 'ctg1\t.\texon\t300\t400\t.\t+\t.\tgene_id "g1"; transcript_id "t1";\n' ... ) >>> annotation = SequenceAnnotation.from_gtf(string=gtf) >>> gene = annotation["g1"] >>> gene.start, gene.end (100, 400) >>> [interval.ID for interval in gene.children] ['t1', 't1.exon_0', 't1.exon_1']
- Parameters:
- Returns:
Sequence annotation
- Return type:
- to_gtf()[source]¶
GTF formatted string with all intervals
- Returns:
GTF formatted string
- Return type:
- classmethod from_gff(filename=None, string=None, sequence=None, link_parents=True)[source]¶
Read a GFF3 formatted file or string. Exactly one of
filenameorstringmust be given. Comment lines are skipped, and reading stops at a##FASTAline.
- to_gff()[source]¶
GFF3 formatted string with all intervals (without header lines)
- Returns:
GFF3 formatted string
- Return type:
- classmethod from_json(filename=None, string=None, sequence=None)[source]¶
Read a json formatted file or string, as written by
to_json(). Exactly one offilenameorstringmust be given.The json must be a list of interval dictionaries (see
SequenceInterval.to_dict()). Each interval dictionary can have achildrenlist with more interval dictionaries.- Parameters:
- Returns:
Sequence annotation
- Return type:
- to_json(indent=None)[source]¶
json formatted string with a list of interval dictionaries (see
SequenceInterval.to_dict())- Parameters:
indent (Optional[int]) – Indentation, passed to
json.dumps()- Returns:
json formatted string
- Return type:
- add(element)¶
Add an element, stored by its ID
- Parameters:
element (DAGElement) – Element to add
- filter(filter_func)¶
Filter elements in the current collection based on the output of filter_func
- Parameters:
filter_func (Callable[[DAGElement], bool]) – Callable[[DagElement], bool] receives as input an element of the current collection and returns a boolean indicating whether the element should be kept (i.e. an element with a ‘True’ return value will be kept)
- Returns:
DirectedAcyclicGraph subclass with unwanted elements removed
- Return type:
DirectedAcyclicGraph
- groupby(group_func)¶
Group elements in the current collection by calling ‘group_func’ and using the results as a dictionary key
- Parameters:
group_func (Callable[[DAGElement], Hashable]) – Callable[[DagElement], Hashable] receives as input an element of the current collection and should return something that can be used as a dictionary key (i.e. it should be hashable)
- Returns:
Dictionary with keys for groups and values individual DirectedAcyclicGraph subclasses
- Return type:
DefaultDict[Hashable, DirectedAcyclicGraph]
- class picea.SequenceInterval(ID=None, seqid=None, source=None, interval_type=None, start=None, end=None, score=None, strand=None, phase=None, children=None, container=None, **kwargs)[source]¶
Single annotated interval on a sequence, such as a gene, mRNA, exon or CDS. Corresponds to one line in a GFF3 or GTF file.
The eight fixed GFF3 columns are stored as attributes (
seqid,source,interval_type,start,end,score,strand,phase). Column 9 attributes are stored as additional attributes with lowercase keys (exceptID) and list values, and can also be accessed by key (interval["name"]). Predefined GFF3 attributes that are not set areNone.Examples
>>> interval = SequenceInterval.from_gff_line("ctg1\t.\tgene\t1000\t9000\t.\t+\t.\tID=gene1;Name=EDEN") >>> interval.interval_type, interval.start, interval.end, interval.strand ('gene', 1000, 9000, '+') >>> interval.name ['EDEN']
- Parameters:
ID (Optional[str]) – Unique identifier
seqid (Optional[str]) – Name of the sequence the interval is on (e.g. a chromosome)
source (Optional[str]) – Program or database that produced the interval
interval_type (Optional[str]) – Feature type, e.g.
gene,mRNA,exonorCDSstart (Optional[int]) – Start position (as in GFF3: 1-based, inclusive)
end (Optional[int]) – End position (as in GFF3: 1-based, inclusive)
score (Optional[float]) – Score
strand (Optional[str]) –
+,-or.phase (Optional[str]) – Phase of CDS intervals:
0,1,2or.children (Optional[List[str]]) – IDs of child intervals
container (Optional[SequenceAnnotation]) – Annotation the interval belongs to
**kwargs – Additional attributes.
parentis the list of parent interval IDs.
- property parent¶
IDs of the direct parent intervals (the GFF3
Parentattribute). Can be set with a single ID or a list of IDs. Seeparentsfor all ancestors.
- property gff_attributes: Dict[str, str]¶
Column 9 attributes as a dictionary, including
IDandParent. Attributes that areNoneare left out.
- property gtf_attributes: Dict[str, str]¶
gff_attributeswithoutIDandParent, plus a<type>_idattribute for the interval itself if it is a gene or mRNA, and for every ancestor (e.g.transcript_idandgene_idfor an exon). Intervals with multiple parents of the same type get a single<type>_id.- Type:
Attributes for GTF output
- classmethod from_gtf_line(gtf_line=None, line_number=None)[source]¶
Create an interval from a single GTF line
- Parameters:
- Returns:
Interval
- Return type:
- Raises:
ValueError – If the start, end, score, strand, or phase column has an invalid value
- to_gtf_line()[source]¶
GTF formatted line (without a trailing newline). The
mRNAtype is written astranscript.- Returns:
GTF formatted line
- Return type:
- classmethod from_gff_line(gff_line=None, line_number=None, attribute_parser=<function parse_gff_attribute_string>)[source]¶
Create an interval from a single GFF3 line. Intervals without an
IDattribute get a random UUID.- Parameters:
- Returns:
Interval
- Return type:
- Raises:
ValueError – If the line does not have 9 columns, or if the start, end, score, strand, or phase column has an invalid value
- classmethod from_dict(interval_dict)[source]¶
Create an interval from a dictionary, as created by
to_dict()- Parameters:
interval_dict (Dict[str, Any]) – The eight fixed GFF3 fields,
ID, and anattributesdictionary- Returns:
Interval
- Return type:
- to_dict(include_children=False)[source]¶
Dictionary with the eight fixed GFF3 fields,
ID, and anattributesdictionary with all other attributes
- to_json(include_children=False, indent=None)[source]¶
json formatted string of
to_dict()- Parameters:
include_children (bool) – Include all descendant intervals
indent (Optional[int]) – Indentation, passed to
json.dumps()
- Returns:
json formatted string
- Return type:
- property ID¶
Unique identifier of the element. Setting a new ID also updates the references of parent and child elements, and the key of the element in its container.
- property children: DirectedAcyclicGraph¶
All descendants of this element (children, their children, etc.), as a new graph of the same type as the container
- property parents: DirectedAcyclicGraph¶
All ancestors of this element (parents, their parents, etc.), as a new graph of the same type as the container