def make_annotation(db="blah", db_id="blah12345", db_obj_symb="blah", qualifier="", negated=False, goid="GO:1234567", references="BLAH:54321", evidence="IDA", withfrom="BLAH:12345", aspect="C", db_obj_name="", db_obj_syn="", db_obj_type="blah", taxon="taxon:12345", date="20200330", assigned_by="blah", extension="", gene_form_id="", properties="", version=None, from_gaf=True): if from_gaf: qual_parse = assocparser.Qualifier2_1() if version == "2.2": qual_parse = assocparser.Qualifier2_2() annotation = [ db, db_id, db_obj_symb, qualifier, goid, references, evidence, withfrom, aspect, db_obj_name, db_obj_syn, db_obj_type, taxon, date, assigned_by, extension, gene_form_id ] return gafparser.to_association(annotation, qualifier_parser=qual_parse) else: if version != "2.0": # Default to 1.2 if we're anything but 2.0 annotation = [ db, db_id, qualifier, goid, references, evidence, withfrom, taxon, date, assigned_by, extension, properties ] return gpadparser.to_association(annotation, version="1.2") else: annotation = [ "{}:{}".format(db, db_id), "NOT" if negated else "", qualifier, goid, references, evidence, withfrom, taxon, date, assigned_by, extension, properties ] return gpadparser.to_association(annotation, version="2.0")
def to_association(gaf_line: List[str], report=None, group="unknown", dataset="unknown", qualifier_parser=assocparser.Qualifier2_1(), bio_entities=None) -> assocparser.ParseResult: report = Report(group=group, dataset=dataset) if report is None else report bio_entities = collections.BioEntities(dict()) if bio_entities is None else bio_entities source_line = "\t".join(gaf_line) if source_line == "": report.error(source_line, "Blank Line", "EMPTY", "Blank lines are not allowed", rule=1) return assocparser.ParseResult(source_line, [], True, report=report) if len(gaf_line) > 17: # If we see more than 17 columns, we will just cut off the columns after column 17 report.warning(source_line, assocparser.Report.WRONG_NUMBER_OF_COLUMNS, "", msg="There were more than 17 columns in this line. Proceeding by cutting off extra columns after column 17.", rule=1) gaf_line = gaf_line[:17] if 17 > len(gaf_line) >= 15: gaf_line += [""] * (17 - len(gaf_line)) if len(gaf_line) != 17: report.error(source_line, assocparser.Report.WRONG_NUMBER_OF_COLUMNS, "", msg="There were {columns} columns found in this line, and there should be 15 (for GAF v1) or 17 (for GAF v2)".format(columns=len(gaf_line)), rule=1) return assocparser.ParseResult(source_line, [], True, report=report) ## check for missing columns ## We use indeces here because we run GO RULES before we split the vals into individual variables DB_INDEX = 0 DB_OBJECT_INDEX = 1 TAXON_INDEX = 12 REFERENCE_INDEX = 5 if gaf_line[DB_INDEX] == "": report.error(source_line, Report.INVALID_IDSPACE, "EMPTY", "col1 is empty", taxon=gaf_line[TAXON_INDEX], rule=1) return assocparser.ParseResult(source_line, [], True, report=report) if gaf_line[DB_OBJECT_INDEX] == "": report.error(source_line, Report.INVALID_ID, "EMPTY", "col2 is empty", taxon=gaf_line[TAXON_INDEX], rule=1) return assocparser.ParseResult(source_line, [], True, report=report) if gaf_line[REFERENCE_INDEX] == "": report.error(source_line, Report.INVALID_ID, "EMPTY", "reference column 6 is empty", taxon=gaf_line[TAXON_INDEX], rule=1) return assocparser.ParseResult(source_line, [], True, report=report) parsed_taxons_result = gaf_line_validators["taxon"].validate(gaf_line[TAXON_INDEX]) # type: assocparser.ValidateResult if not parsed_taxons_result.valid: report.error(source_line, Report.INVALID_TAXON, parsed_taxons_result.original, parsed_taxons_result.message, taxon=parsed_taxons_result.original, rule=1) return assocparser.ParseResult(source_line, [], True, report=report) taxon = parsed_taxons_result.parsed[0] date = assocparser.parse_date(gaf_line[13], report, source_line) if date is None: return assocparser.ParseResult(source_line, [], True, report=report) interacting_taxon = parsed_taxons_result.parsed[1] if len(parsed_taxons_result.parsed) == 2 else None subject_curie = association.Curie(gaf_line[0], gaf_line[1]) subject = association.Subject(subject_curie, gaf_line[2], [gaf_line[9]], gaf_line[10].split("|"), [association.map_gp_type_label_to_curie(gaf_line[11])], taxon) gpi_entity = bio_entities.get(subject_curie) if gpi_entity is not None and subject != gpi_entity: subject = gpi_entity # column 4 is qualifiers -> index 3 # For allowed, see http://geneontology.org/docs/go-annotations/#annotation-qualifiers # We use the below validate to check validaty if qualifiers, not as much to *parse* them into the GoAssociation object. # For GoAssociation we will use the above qualifiers list. This is fine because the above does not include `NOT`, etc # This is confusing, and we can fix later on by consolidating qualifier and relation in GoAssociation. parsed_qualifiers = qualifier_parser.validate(gaf_line[3]) if not parsed_qualifiers.valid: report.error(source_line, Report.INVALID_QUALIFIER, parsed_qualifiers.original, parsed_qualifiers.message, taxon=gaf_line[TAXON_INDEX], rule=1) return assocparser.ParseResult(source_line, [], True, report=report) aspect = gaf_line[8] negated, relation_label, qualifiers = assocparser._parse_qualifier(gaf_line[3], aspect) # Note: Relation label is grabbed from qualifiers, if any exist in _parse_qualifier qualifiers = [association.Curie.from_str(curie_util.contract_uri(relations.lookup_label(q), strict=False)[0]) for q in qualifiers] object = association.Term(association.Curie.from_str(gaf_line[4]), taxon) if isinstance(object, association.Error): report.error(source_line, Report.INVALID_SYMBOL, gaf_line[4], "Problem parsing GO Term", taxon=gaf_line[TAXON_INDEX], rule=1) # References references = [association.Curie.from_str(e) for e in gaf_line[5].split("|") if e] for r in references: if isinstance(r, association.Error): report.error(source_line, Report.INVALID_SYMBOL, gaf_line[5], "Problem parsing references", taxon=gaf_line[TAXON_INDEX], rule=1) return assocparser.ParseResult(source_line, [], True, report=report) gorefs = [ref for ref in references if ref.namespace == "GO_REF"] + [None] eco_curie = ecomap.coderef_to_ecoclass(gaf_line[6], reference=gorefs[0]) if eco_curie is None: report.error(source_line, Report.UNKNOWN_EVIDENCE_CLASS, gaf_line[6], msg="Expecting a known ECO GAF code, e.g ISS", rule=1) return assocparser.ParseResult(source_line, [], True, report=report) withfroms = association.ConjunctiveSet.str_to_conjunctions(gaf_line[7]) if isinstance(withfroms, association.Error): report.error(source_line, Report.INVALID_SYMBOL, gaf_line[7], "Problem parsing with/from", taxon=gaf_line[TAXON_INDEX], rule=1) return assocparser.ParseResult(source_line, [], True, report=report) evidence_type = association.Curie.from_str(eco_curie) if isinstance(evidence_type, association.Error): report.error(source_line, Report.INVALID_SYMBOL, gaf_line[6], "Problem parsing evidence type", taxon=gaf_line[TAXON_INDEX], rule=1) evidence = association.Evidence(association.Curie.from_str(eco_curie), references, withfroms) if any([isinstance(e, association.Error) for e in evidence.has_supporting_reference]): first_error = [e for e in evidence.has_supporting_reference if isinstance(e, association.Error)][0] report.error(source_line, Report.INVALID_SYMBOL, gaf_line[5], first_error.info, taxon=str(taxon), rule=1) return assocparser.ParseResult(source_line, [], True, report=report) subject_extensions = [] if gaf_line[16]: subject_filler = association.Curie.from_str(gaf_line[16]) if isinstance(subject_filler, association.Error): report.error(source_line, assocparser.Report.INVALID_ID, gaf_line[16], subject_filler.info, taxon=str(taxon), rule=1) return assocparser.ParseResult(source_line, [], True, report=report) # filler is not an Error, so keep moving subject_extensions.append(association.ExtensionUnit(association.Curie.from_str("rdfs:subClassOf"), subject_filler)) conjunctions = [] if gaf_line[15]: conjunctions = association.ConjunctiveSet.str_to_conjunctions( gaf_line[15], conjunct_element_builder=lambda el: association.ExtensionUnit.from_str(el)) if isinstance(conjunctions, association.Error): report.error(source_line, Report.EXTENSION_SYNTAX_ERROR, conjunctions.info, "extensions should be relation(curie) and relation should have corresponding URI", taxon=str(taxon), rule=1) return assocparser.ParseResult(source_line, [], True, report=report) relation_uri = relations.lookup_label(relation_label) if relation_uri is None: report.error(source_line, assocparser.Report.INVALID_QUALIFIER, relation_label, "Could not find CURIE for relation `{}`".format(relation_label), taxon=str(taxon), rule=1) return assocparser.ParseResult(source_line, [], True, report=report) # We don't have to check that this is well formed because we're grabbing it from the known relations URI map. relation_curie = association.Curie.from_str(curie_util.contract_uri(relation_uri)[0]) a = association.GoAssociation( source_line="\t".join(gaf_line), subject=subject, relation=relation_curie, object=object, negated=negated, qualifiers=qualifiers, aspect=aspect, interacting_taxon=interacting_taxon, evidence=evidence, subject_extensions=subject_extensions, object_extensions=conjunctions, provided_by=gaf_line[14], date=date, properties={}) return assocparser.ParseResult(source_line, [a], False, report=report)
def qualifier_parser(self) -> assocparser.ColumnValidator: if self.gaf_version() == "2.2": return assocparser.Qualifier2_2() return assocparser.Qualifier2_1()
from ontobio.rdfgen import relations from ontobio.ontol import Ontology import dateutil.parser import functools import click logger = logging.getLogger(__name__) gaf_line_validators = { "default": assocparser.ColumnValidator(), "qualifier2_1": assocparser.Qualifier2_1(), "qualifier2_2": assocparser.Qualifier2_2(), "curie": assocparser.CurieValidator(), "taxon": assocparser.TaxonValidator() } def protein_complex_sublcass_closure(ontology: Ontology) -> Set[str]: protein_containing_complex = association.Curie(namespace="GO", identity="0032991") children_of_complexes = set(ontology.descendants(str(protein_containing_complex), relations=["subClassOf"], reflexive=True)) return children_of_complexes class GafParser(assocparser.AssocParser): """ Parser for GO GAF format