def test_validate_pipe_separated(): parser = gafparser.GafParser() ids = parser.validate_pipe_separated_ids( "PMID:12345", assocparser.SplitLine("", [""] * 17, "taxon:foo")) assert set(ids) == set(["PMID:12345"]) ids = parser.validate_pipe_separated_ids( "PMID:12345|PMID:11111", assocparser.SplitLine("", [""] * 17, "taxon:foo")) assert set(ids) == set(["PMID:12345", "PMID:11111"])
def test_validate_pipe_separated_with_bad_ids(): parser = gafparser.GafParser() ids = parser.validate_pipe_separated_ids( "PMID:123[2]|PMID:11111", assocparser.SplitLine("", [""] * 17, "taxon:foo")) assert ids == None ids = parser.validate_pipe_separated_ids( "PMID:123[2]", assocparser.SplitLine("", [""] * 17, "taxon:foo")) assert ids == None
def skim(self, file): file = self._ensure_file(file) tuples = [] for line in file: if line.startswith("!"): continue vals = line.split("\t") if len(vals) < 15: logging.error( "Unexpected number of vals: {}. GAFv1 has 15, GAFv2 has 17." .format(vals)) split_line = assocparser.SplitLine(line=line, values=vals, taxon=vals[12]) negated, relation, _ = self._parse_qualifier(vals[3], vals[8]) # never include NOTs in a skim if negated: continue if self._is_exclude_relation(relation): continue id = self._pair_to_id(vals[0], vals[1]) if not self._validate_id(id, split_line, context=ENTITY): continue n = vals[2] t = vals[4] tuples.append((id, n, t)) return tuples
def test_empty_id(): parser = gafparser.GafParser() valid = parser._validate_id( "", assocparser.SplitLine("", [""] * 17, "taxon:foo")) assert not valid assert len(parser.report.messages) == 1 assert parser.report.messages[0]["level"] == assocparser.Report.ERROR
def test_no_flag_valid_id(): ont = OntologyFactory().create(ONT) p = GafParser() p.config.ontology = ont p._validate_ontology_class_id( "GO:0000785", assocparser.SplitLine("fake", [""] * 17, taxon="foo")) assert len(p.report.messages) == 0
def test_validate_with_allowed_ids(): parser = gafparser.GafParser() valid = parser._validate_id("FOO:123", assocparser.SplitLine("", [""] * 17, "taxon:foo"), allowed_ids=["FOO"]) assert valid
def test_pipe_in_id(): parser = gafparser.GafParser() valid = parser._validate_id( "F|OO:123", assocparser.SplitLine("", [""] * 17, "taxon:foo")) assert valid assert len(parser.report.messages) == 1 assert parser.report.messages[0]["level"] == assocparser.Report.WARNING
def test_validate_pipe_separated_empty_allowed(): parser = gafparser.GafParser() ids = parser.validate_pipe_separated_ids("", assocparser.SplitLine( "", [""] * 17, "taxon:foo"), empty_allowed=True) assert ids == []
def test_normalize_refs_good_and_bad_refs(): parser = gafparser.GafParser() refs = parser.normalize_refs(["FB:123", "PMID:234"], assocparser.SplitLine("", [""] * 17, "taxon:foo")) assert len(parser.report.messages) == 1 assert parser.report.messages[0][ "type"] == assocparser.Report.INVALID_IDSPACE
def test_validate_with_disallowed_id(): parser = gafparser.GafParser() valid = parser._validate_id("FOO:123", assocparser.SplitLine("", [""] * 17, "taxon:foo"), allowed_ids=["BAR"]) assert len(parser.report.messages) == 1 assert parser.report.messages[0]["level"] == assocparser.Report.WARNING
def test_normalize_refs_single_bad_ref(): parser = gafparser.GafParser() ref = parser.normalize_refs(["FB:123"], assocparser.SplitLine("", [""] * 17, "taxon:foo")) assert ref == ["FB:123"] assert len(parser.report.messages) == 1 assert parser.report.messages[0][ "type"] == assocparser.Report.INVALID_IDSPACE
def test_validate_pipe_with_additional_delims(): parser = gafparser.GafParser() ids = parser.validate_pipe_separated_ids("F:123,B:234|B:111", assocparser.SplitLine( "", [""] * 17, "taxon:foo"), extra_delims=",") assert set(ids) == set(["F:123", "B:234", "B:111"]) result = parser.parse_line( "PomBase\tSPAC25B8.17\typf1\t\tGO:1990578\tGO_REF:0000024\tISO\tUniProtKB:Q9CXD9|ensembl:ENSMUSP00000038569,PMID:11111\tC\tintramembrane aspartyl protease of the perinuclear ER membrane Ypf1 (predicted)\tppp81\tprotein\ttaxon:4896\t20150305\tPomBase\t\t" ) assert set(result.associations[0]["evidence"]["with_support_from"]) == set( ["UniProtKB:Q9CXD9", "ensembl:ENSMUSP00000038569", "PMID:11111"])
def test_already_normalized_mapping(): parser = gafparser.GafParser() mapped = parser._taxon_id( "NCBITaxon:123", assocparser.SplitLine("", [""] * 17, "NCBITaxon:123")) assert mapped == "NCBITaxon:123"
def test_normalize_refs_good(): parser = gafparser.GafParser() refs = parser.normalize_refs(["PMID:123"], assocparser.SplitLine("", [""] * 17, "taxon:foo")) assert refs == ["PMID:123"]
def parse_line(self, line): """Parses a single line of a GPAD. Return a tuple `(processed_line, associations)`. Typically there will be a single association, but in some cases there may be none (invalid line) or multiple (disjunctive clause in annotation extensions) Note: most applications will only need to call this directly if they require fine-grained control of parsing. For most purposes, :method:`parse_file` can be used over the whole file Arguments --------- line : str A single tab-seperated line from a GPAD file """ parsed = super().validate_line(line) if parsed: return parsed if self.is_header(line): return assocparser.ParseResult(line, [{ "header": True, "line": line.strip() }], False) vals = [el.strip() for el in line.split("\t")] parsed = to_association(list(vals), report=self.report) if parsed.associations == []: return parsed assoc = parsed.associations[0] go_rule_results = qc.test_go_rules(assoc, self.config) for rule, result in go_rule_results.all_results.items(): if result.result_type == qc.ResultType.WARNING: self.report.warning(line, assocparser.Report.VIOLATES_GO_RULE, "", msg="{id}: {message}".format( id=rule.id, message=result.message), rule=int(rule.id.split(":")[1])) if result.result_type == qc.ResultType.ERROR: self.report.error(line, assocparser.Report.VIOLATES_GO_RULE, "", msg="{id}: {message}".format( id=rule.id, message=result.message), rule=int(rule.id.split(":")[1])) # Skip the annotation return assocparser.ParseResult(line, [], True) if result.result_type == qc.ResultType.PASS: self.report.message(assocparser.Report.INFO, line, Report.RULE_PASS, "", msg="Passing Rule", rule=int(rule.id.split(":")[1])) vals = list(go_rule_results.annotation.to_gpad_tsv()) [ db, db_object_id, qualifier, goid, reference, evidence, withfrom, interacting_taxon_id, date, assigned_by, annotation_xp, annotation_properties ] = vals split_line = assocparser.SplitLine(line=line, values=vals, taxon="") id = self._pair_to_id(db, db_object_id) if not self._validate_id(id, split_line, context=ENTITY): return assocparser.ParseResult(line, [], True) if not self._validate_id(goid, split_line, context=ANNOTATION): return assocparser.ParseResult(line, [], True) valid_goid = self._validate_ontology_class_id(goid, split_line) if valid_goid == None: return assocparser.ParseResult(line, [], True) goid = valid_goid date = self._normalize_gaf_date(date, split_line) if reference == "": self.report.error(line, Report.INVALID_ID, "EMPTY", "reference column 6 is empty") return assocparser.ParseResult(line, [], True) self._validate_id(evidence, split_line) interacting_taxon = None if interacting_taxon_id == "" else interacting_taxon_id if interacting_taxon != None: interacting_taxon = self._taxon_id(interacting_taxon_id, split_line) if interacting_taxon == None: self.report.error(line, assocparser.Report.INVALID_TAXON, interacting_taxon_id, msg="Taxon ID is invalid") return assocparser.ParseResult(line, [], True) #TODO: ecomap is currently one-way only #ecomap = self.config.ecomap #if ecomap != None: # if ecomap.ecoclass_to_coderef(evidence) == (None,None): # self.report.error(line, Report.UNKNOWN_EVIDENCE_CLASS, evidence, # msg="Expecting a known ECO class ID") ## -- ## qualifier ## -- negated, relation, other_qualifiers = self._parse_qualifier( qualifier, None) # Reference Column references = self.validate_pipe_separated_ids(reference, split_line) if references == None: # Reporting occurs in above function call return assocparser.ParseResult(line, [], True) # With/From withfroms = self.validate_pipe_separated_ids(withfrom, split_line, empty_allowed=True, extra_delims=",") if withfroms == None: # Reporting occurs in above function call return assocparser.ParseResult(line, [], True) ## -- ## parse annotation extension ## See appending in http://doi.org/10.1186/1471-2105-15-155 ## -- object_or_exprs = self._parse_full_extension_expression( annotation_xp, line=split_line) subject_symbol = id subject_fullname = id subject_synonyms = [] if self.gpi is not None: gp = self.gpi.get(id, {}) if gp is not {}: subject_symbol = gp["symbol"] subject_fullname = gp["name"] subject_synonyms = gp["synonyms"].split("|") assoc = { 'source_line': line, 'subject': { 'id': id, 'label': subject_symbol, 'fullname': subject_fullname, 'synonyms': subject_synonyms, 'taxon': { 'id': interacting_taxon }, }, 'object': { 'id': goid }, 'negated': negated, 'relation': { 'id': relation }, 'interacting_taxon': interacting_taxon, 'evidence': { 'type': evidence, 'with_support_from': withfroms, 'has_supporting_reference': references }, 'subject_extensions': [], 'object_extensions': {}, 'aspect': self.compute_aspect(goid), 'provided_by': assigned_by, 'date': date, } if len(other_qualifiers) > 0: assoc['qualifiers'] = other_qualifiers if object_or_exprs is not None and len(object_or_exprs) > 0: assoc['object_extensions'] = {'union_of': object_or_exprs} return assocparser.ParseResult(line, [assoc], False)
def test_doi_id(): parser = gafparser.GafParser() valid = parser._validate_id( "DOI:10.1007/BF00127499", assocparser.SplitLine("", [""] * 17, "taxon:foo")) assert valid
def parse_line(self, line): """Parses a single line of a GPI. Return a tuple `(processed_line, entities)`. Typically there will be a single entity, but in some cases there may be none (invalid line) or multiple (disjunctive clause in annotation extensions) Note: most applications will only need to call this directly if they require fine-grained control of parsing. For most purposes, :method:`parse_file` can be used over the whole file Arguments --------- line : str A single tab-seperated line from a GPAD file """ if self.is_header(line): if self.version is None: parsed = parser_version_regex.findall(line) if len(parsed) == 1: filetype, version, _ = parsed[0] if version == "2.0": logger.info("Detected GPI version 2.0") self.version = version else: logger.info("Detected version {}, so using 1.2".format( version)) self.version = self.default_version return (line, [{"header": True, "line": line.strip()}]) if self.version is None: logger.warning( "No version number found for this file so we will assum GPI version: {}" .format(self.default_version)) self.version = self.default_version vals = line.split("\t") if len(vals) < 7: self.report.error(line, assocparser.Report.WRONG_NUMBER_OF_COLUMNS, "") return line, [] # If we are 1.2, then we can upconvert into a 2.0 "line", and validate from there if self.gpi_version() == "1.2": if len(vals) < 10 and len(vals) >= 7: missing_columns = 10 - len(vals) vals += ["" for i in range(missing_columns)] # Convert a 1.2 set of values to a 2.0 set of values vals = self.line_as_2_0(vals) else: # We are gpi 2.0 if len(vals) < 11 and len(vals) >= 7: missing_columns = 11 - len(vals) vals += ["" for i in range(missing_columns)] vals = [el.strip() for el in vals] # End Housekeeping #================================================================= [ object_id, db_object_symbol, db_object_name, synonyms, entity_types, taxon, encoded_by, parents, contained_complex_members, xrefs, properties ] = vals split_line = assocparser.SplitLine(line=line, values=vals, taxon=taxon) ## -- ## db + db_object_id. CARD=1 ## -- if not self._validate_id(object_id, split_line): return line, [] fullnames = self.list_field(db_object_name) ## -- ## db_object_synonym CARD=0..* ## -- synonyms = self.list_field(synonyms) types = self.list_field(entity_types) encoded_by = self.list_field(encoded_by) for encoded in encoded_by: self._validate_id(encoded, split_line) parents = [self._normalize_id(x) for x in self.list_field(parents)] for p in parents: self._validate_id(p, split_line) contained_complex_members = self.list_field(contained_complex_members) for members in contained_complex_members: self._validate_id(members, split_line) xref_ids = self.list_field(xrefs) obj = { 'id': object_id, 'label': db_object_symbol, 'full_name': fullnames, 'synonyms': synonyms, 'type': types, 'parents': parents, 'encoded_by': encoded_by, 'contained_complex_members': contained_complex_members, 'xrefs': xref_ids, 'taxon': { 'id': self._taxon_id(taxon, split_line) }, 'properties': properties } return line, [obj]
def parse_line(self, line): """ Parses a single line of a GAF Return a tuple `(processed_line, associations)`. Typically there will be a single association, but in some cases there may be none (invalid line) or multiple (disjunctive clause in annotation extensions) Note: most applications will only need to call this directly if they require fine-grained control of parsing. For most purposes, :method:`parse_file` can be used over the whole file Arguments --------- line : str A single tab-seperated line from a GAF file """ # Returns assocparser.ParseResult parsed = super().validate_line(line) if parsed: return parsed if self.is_header(line): return assocparser.ParseResult(line, [{ "header": True, "line": line.strip() }], True) vals = [el.strip() for el in line.split("\t")] # GAF v1 is defined as 15 cols, GAF v2 as 17. # We treat everything as GAF2 by adding two blank columns. # TODO: check header metadata to see if columns corresponds to declared dataformat version if 17 > len(vals) >= 15: vals += [""] * (17 - len(vals)) if len(vals) > 17: # If we see more than 17 columns, we will just cut off the columns after column 17 self.report.warning(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) vals = vals[:17] if len(vals) != 17: self.report.error(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(vals)), rule=1) return assocparser.ParseResult(line, [], True) [db, db_object_id, db_object_symbol, qualifier, goid, reference, evidence, withfrom, aspect, db_object_name, db_object_synonym, db_object_type, taxon, date, assigned_by, annotation_xp, gene_product_isoform] = vals split_line = assocparser.SplitLine(line=line, values=vals, taxon=taxon) ## check for missing columns if db == "": self.report.error(line, Report.INVALID_IDSPACE, "EMPTY", "col1 is empty", taxon=taxon, rule=1) return assocparser.ParseResult(line, [], True) if db_object_id == "": self.report.error(line, Report.INVALID_ID, "EMPTY", "col2 is empty", taxon=taxon, rule=1) return assocparser.ParseResult(line, [], True) if taxon == "": self.report.error(line, Report.INVALID_TAXON, "EMPTY", "taxon column is empty", taxon=taxon, rule=1) return assocparser.ParseResult(line, [], True) if reference == "": self.report.error(line, Report.INVALID_ID, "EMPTY", "reference column 6 is empty", taxon=taxon, rule=1) return assocparser.ParseResult(line, [], True) ## -- ## db + db_object_id. CARD=1 ## -- id = self._pair_to_id(db, db_object_id) if not self._validate_id(id, split_line, ENTITY): print("skipping because {} not validated!".format(id)) return assocparser.ParseResult(line, [], True) # Using a given gpi file to validate the gene object if self.gpi is not None: entity = self.gpi.get(id, None) if entity is not None: db_object_symbol = entity["symbol"] db_object_name = entity["name"] db_object_synonym = entity["synonyms"] db_object_type = entity["type"] if not self._validate_id(goid, split_line, ANNOTATION): print("skipping because {} not validated!".format(goid)) return assocparser.ParseResult(line, [], True) valid_goid = self._validate_ontology_class_id(goid, split_line) if valid_goid == None: return assocparser.ParseResult(line, [], True) goid = valid_goid date = self._normalize_gaf_date(date, split_line) if date == None: return assocparser.ParseResult(line, [], True) vals[13] = date ecomap = self.config.ecomap if ecomap is not None: if ecomap.coderef_to_ecoclass(evidence, reference) is None: self.report.error(line, assocparser.Report.UNKNOWN_EVIDENCE_CLASS, evidence, msg="Expecting a known ECO GAF code, e.g ISS") return assocparser.ParseResult(line, [], True) # Throw out the line if it uses GO_REF:0000033, see https://github.com/geneontology/go-site/issues/563#event-1519351033 if "GO_REF:0000033" in reference.split("|"): self.report.error(line, assocparser.Report.INVALID_ID, reference, msg="Disallowing GO_REF:0000033 in reference field as of 03/13/2018") return assocparser.ParseResult(line, [], True) references = self.validate_pipe_separated_ids(reference, split_line) if references == None: # Reporting occurs in above function call return assocparser.ParseResult(line, [], True) # With/From withfroms = self.validate_pipe_separated_ids(withfrom, split_line, empty_allowed=True, extra_delims=",") if withfroms == None: # Reporting occurs in above function call return assocparser.ParseResult(line, [], True) # validation self._validate_symbol(db_object_symbol, split_line) # Example use case: mapping from UniProtKB to MOD ID if self.config.entity_map is not None: id = self.map_id(id, self.config.entity_map) toks = id.split(":") db = toks[0] db_object_id = toks[1:] vals[1] = db_object_id if goid.startswith("GO:") and aspect.upper() not in ["C", "F", "P"]: self.report.error(line, assocparser.Report.INVALID_ASPECT, aspect) return assocparser.ParseResult(line, [], True) go_rule_results = qc.test_go_rules(vals, self.config) for rule_id, result in go_rule_results.items(): if result.result_type == qc.ResultType.WARNING: self.report.warning(line, assocparser.Report.VIOLATES_GO_RULE, goid, msg="{id}: {message}".format(id=rule_id, message=result.message)) # Skip the annotation return assocparser.ParseResult(line, [], True) if result.result_type == qc.ResultType.ERROR: self.report.error(line, assocparser.Report.VIOLATES_GO_RULE, goid, msg="{id}: {message}".format(id=rule_id, message=result.message)) # Skip the annotation return assocparser.ParseResult(line, [], True) ## -- ## end of line re-processing ## -- # regenerate line post-mapping line = "\t".join(vals) ## -- ## taxon CARD={1,2} ## -- ## if a second value is specified, this is the interacting taxon ## We do not use the second value normalized_taxon = self._taxon_id(taxon.split("|")[0], split_line) if normalized_taxon == None: self.report.error(line, assocparser.Report.INVALID_TAXON, taxon, msg="Taxon ID is invalid") return assocparser.ParseResult(line, [], True) self._validate_taxon(normalized_taxon, split_line) ## -- ## db_object_synonym CARD=0..* ## -- synonyms = db_object_synonym.split("|") if db_object_synonym == "": synonyms = [] ## -- ## parse annotation extension ## See appendix in http://doi.org/10.1186/1471-2105-15-155 ## -- object_or_exprs = self._parse_full_extension_expression(annotation_xp, line=split_line) ## -- ## qualifier ## -- negated, relation, other_qualifiers = self._parse_qualifier(qualifier, aspect) ## -- ## goid ## -- # TODO We shouldn't overload buildin keywords/functions object = {'id': goid, 'taxon': normalized_taxon} # construct subject dict subject = { 'id': id, 'label': db_object_symbol, 'type': db_object_type, 'fullname': db_object_name, 'synonyms': synonyms, 'taxon': { 'id': normalized_taxon } } ## -- ## gene_product_isoform ## -- ## This is mapped to a more generic concept of subject_extensions subject_extns = [] if gene_product_isoform is not None and gene_product_isoform != '': subject_extns.append({'property':'isoform', 'filler':gene_product_isoform}) ## -- ## evidence ## reference ## withfrom ## -- evidence_obj = { 'type': evidence, 'has_supporting_reference': references, 'with_support_from': withfroms } ## Construct main return dict assoc = { 'source_line': line, 'subject': subject, 'object': object, 'negated': negated, 'qualifiers': other_qualifiers, 'aspect': aspect, 'relation': { 'id': relation }, 'evidence': evidence_obj, 'provided_by': assigned_by, 'date': date, } if len(subject_extns) > 0: assoc['subject_extensions'] = subject_extns if object_or_exprs is not None and len(object_or_exprs) > 0: assoc['object_extensions'] = {'union_of': object_or_exprs} return assocparser.ParseResult(line, [assoc], False, evidence.upper())
def parse_line(self, line): """ Parses a single line of a GAF Return a tuple `(processed_line, associations)`. Typically there will be a single association, but in some cases there may be none (invalid line) or multiple (disjunctive clause in annotation extensions) Note: most applications will only need to call this directly if they require fine-grained control of parsing. For most purposes, :method:`parse_file` can be used over the whole file Arguments --------- line : str A single tab-seperated line from a GAF file """ # Returns assocparser.ParseResult parsed = super().validate_line(line) if parsed: return parsed if self.is_header(line): return assocparser.ParseResult(line, [{ "header": True, "line": line.strip() }], False) vals = [el.strip() for el in line.split("\t")] # GAF v1 is defined as 15 cols, GAF v2 as 17. # We treat everything as GAF2 by adding two blank columns. # TODO: check header metadata to see if columns corresponds to declared dataformat version parsed = to_association(list(vals), report=self.report) if parsed.associations == []: return parsed assoc = parsed.associations[0] # self.report = parsed.report ## Run GO Rules, save split values into individual variables go_rule_results = qc.test_go_rules(assoc, self.config, group=self.group) for rule, result in go_rule_results.all_results.items(): if result.result_type == qc.ResultType.WARNING: self.report.warning(line, assocparser.Report.VIOLATES_GO_RULE, "", msg="{id}: {message}".format( id=rule.id, message=result.message), rule=int(rule.id.split(":")[1])) if result.result_type == qc.ResultType.ERROR: self.report.error(line, assocparser.Report.VIOLATES_GO_RULE, "", msg="{id}: {message}".format( id=rule.id, message=result.message), rule=int(rule.id.split(":")[1])) # Skip the annotation return assocparser.ParseResult(line, [], True) if result.result_type == qc.ResultType.PASS: self.report.message(assocparser.Report.INFO, line, Report.RULE_PASS, "", msg="Passing Rule", rule=int(rule.id.split(":")[1])) vals = list(go_rule_results.annotation.to_gaf_tsv()) [ db, db_object_id, db_object_symbol, qualifier, goid, reference, evidence, withfrom, aspect, db_object_name, db_object_synonym, db_object_type, taxon, date, assigned_by, annotation_xp, gene_product_isoform ] = vals split_line = assocparser.SplitLine(line=line, values=vals, taxon=taxon) if self.config.group_idspace is not None and assigned_by not in self.config.group_idspace: self.report.warning( line, Report.INVALID_ID, assigned_by, "GORULE:0000027: assigned_by is not present in groups reference", taxon=taxon, rule=27) if self.config.entity_idspaces is not None and db not in self.config.entity_idspaces: # Are we a synonym? upgrade = self.config.entity_idspaces.reverse(db) if upgrade is not None: # If we found a synonym self.report.warning( line, Report.INVALID_ID_DBXREF, db, "GORULE:0000027: {} is a synonym for the correct ID {}, and has been updated" .format(db, upgrade), taxon=taxon, rule=27) db = upgrade ## -- ## db + db_object_id. CARD=1 ## -- id = self._pair_to_id(db, db_object_id) if not self._validate_id( id, split_line, allowed_ids=self.config.entity_idspaces): return assocparser.ParseResult(line, [], True) # Using a given gpi file to validate the gene object if self.gpi is not None: entity = self.gpi.get(id, None) if entity is not None: db_object_symbol = entity["symbol"] db_object_name = entity["name"] db_object_synonym = entity["synonyms"] db_object_type = entity["type"] if not self._validate_id(goid, split_line, context=ANNOTATION): print("skipping because {} not validated!".format(goid)) return assocparser.ParseResult(line, [], True) valid_goid = self._validate_ontology_class_id(goid, split_line) if valid_goid == None: return assocparser.ParseResult(line, [], True) goid = valid_goid date = self._normalize_gaf_date(date, split_line) if date == None: return assocparser.ParseResult(line, [], True) vals[13] = date ecomap = self.config.ecomap if ecomap is not None: if ecomap.coderef_to_ecoclass(evidence, reference) is None: self.report.error( line, assocparser.Report.UNKNOWN_EVIDENCE_CLASS, evidence, msg="Expecting a known ECO GAF code, e.g ISS", rule=1) return assocparser.ParseResult(line, [], True) references = self.validate_pipe_separated_ids(reference, split_line) if references == None: # Reporting occurs in above function call return assocparser.ParseResult(line, [], True) # With/From withfroms = self.validate_pipe_separated_ids(withfrom, split_line, empty_allowed=True, extra_delims=",") if withfroms == None: # Reporting occurs in above function call return assocparser.ParseResult(line, [], True) # validation self._validate_symbol(db_object_symbol, split_line) # Example use case: mapping from UniProtKB to MOD ID if self.config.entity_map is not None: id = self.map_id(id, self.config.entity_map) toks = id.split(":") db = toks[0] db_object_id = toks[1:] vals[1] = db_object_id ## -- ## end of line re-processing ## -- # regenerate line post-mapping line = "\t".join(vals) ## -- ## taxon CARD={1,2} ## -- ## if a second value is specified, this is the interacting taxon ## We do not use the second value taxons = taxon.split("|") normalized_taxon = self._taxon_id(taxons[0], split_line) if normalized_taxon == None: self.report.error(line, assocparser.Report.INVALID_TAXON, taxon, msg="Taxon ID is invalid") return assocparser.ParseResult(line, [], True) self._validate_taxon(normalized_taxon, split_line) interacting_taxon = None if len(taxons) == 2: interacting_taxon = self._taxon_id(taxons[1], split_line) if interacting_taxon == None: self.report.error(line, assocparser.Report.INVALID_TAXON, taxon, msg="Taxon ID is invalid") return assocparser.ParseResult(line, [], True) ## -- ## db_object_synonym CARD=0..* ## -- synonyms = db_object_synonym.split("|") if db_object_synonym == "": synonyms = [] ## -- ## parse annotation extension ## See appendix in http://doi.org/10.1186/1471-2105-15-155 ## -- object_or_exprs = self._parse_full_extension_expression( annotation_xp, line=split_line) ## -- ## qualifier ## -- negated, relation, other_qualifiers = self._parse_qualifier( qualifier, aspect) ## -- ## goid ## -- # TODO We shouldn't overload buildin keywords/functions object = {'id': goid, 'taxon': normalized_taxon} # construct subject dict subject = { 'id': id, 'label': db_object_symbol, 'type': db_object_type, 'fullname': db_object_name, 'synonyms': synonyms, 'taxon': { 'id': normalized_taxon } } ## -- ## gene_product_isoform ## -- ## This is mapped to a more generic concept of subject_extensions subject_extns = [] if gene_product_isoform is not None and gene_product_isoform != '': subject_extns.append({ 'property': 'isoform', 'filler': gene_product_isoform }) object_extensions = {} if object_or_exprs is not None and len(object_or_exprs) > 0: object_extensions['union_of'] = object_or_exprs ## -- ## evidence ## reference ## withfrom ## -- evidence_obj = { 'type': evidence, 'has_supporting_reference': references, 'with_support_from': withfroms } ## Construct main return dict assoc = { 'source_line': line, 'subject': subject, 'object': object, 'negated': negated, 'qualifiers': other_qualifiers, 'aspect': aspect, 'relation': { 'id': relation }, 'interacting_taxon': interacting_taxon, 'evidence': evidence_obj, 'provided_by': assigned_by, 'date': date, 'subject_extensions': subject_extns, 'object_extensions': object_extensions } return assocparser.ParseResult(line, [assoc], False, evidence.upper())
def parse_line(self, line): """Parses a single line of a GPAD. Return a tuple `(processed_line, associations)`. Typically there will be a single association, but in some cases there may be none (invalid line) or multiple (disjunctive clause in annotation extensions) Note: most applications will only need to call this directly if they require fine-grained control of parsing. For most purposes, :method:`parse_file` can be used over the whole file Arguments --------- line : str A single tab-seperated line from a GPAD file """ parsed = super().validate_line(line) if parsed: return parsed if self.is_header(line): if self.version is None: # We are still looking parsed = parser_version_regex.findall(line) if len(parsed) == 1: filetype, version, _ = parsed[0] if version == "2.0": logger.info("Detected GPAD version 2.0") self.version = version else: logger.info( "Detected GPAD version {}, so defaulting to 1.2". format(version)) self.version = self.default_version return assocparser.ParseResult(line, [{ "header": True, "line": line.strip() }], False) # At this point, we should have gone through all the header, and a version number should be established if self.version is None: logger.warning( "No version number found for this file so we will assume GPAD version: {}" .format(self.default_version)) self.version = self.default_version vals = [el.strip() for el in line.split("\t")] parsed = to_association(list(vals), report=self.report, version=self.gpad_version(), bio_entities=self.bio_entities) if parsed.associations == []: return parsed assoc = parsed.associations[0] go_rule_results = qc.test_go_rules(assoc, self.config) for rule, result in go_rule_results.all_results.items(): if result.result_type == qc.ResultType.WARNING: self.report.warning(line, assocparser.Report.VIOLATES_GO_RULE, "", msg="{id}: {message}".format( id=rule.id, message=result.message), rule=int(rule.id.split(":")[1])) if result.result_type == qc.ResultType.ERROR: self.report.error(line, assocparser.Report.VIOLATES_GO_RULE, "", msg="{id}: {message}".format( id=rule.id, message=result.message), rule=int(rule.id.split(":")[1])) # Skip the annotation return assocparser.ParseResult(line, [], True) if result.result_type == qc.ResultType.PASS: self.report.message(assocparser.Report.INFO, line, Report.RULE_PASS, "", msg="Passing Rule", rule=int(rule.id.split(":")[1])) assoc = go_rule_results.annotation # type: association.GoAssociation split_line = assocparser.SplitLine(line=line, values=vals, taxon="") if not self._validate_id( str(assoc.subject.id), split_line, context=ENTITY): return assocparser.ParseResult(line, [], True) if not self._validate_id( str(assoc.object.id), split_line, context=ANNOTATION): return assocparser.ParseResult(line, [], True) valid_goid = self._validate_ontology_class_id(str(assoc.object.id), split_line) if valid_goid is None: return assocparser.ParseResult(line, [], True) assoc.object.id = association.Curie.from_str(valid_goid) if not self._validate_id(str(assoc.evidence.type), split_line): return assocparser.ParseResult(line, [], True) if assoc.interacting_taxon: if not self._validate_taxon(str(assoc.interacting_taxon), split_line): self.report.error(line, assocparser.Report.INVALID_TAXON, str(assoc.interacting_taxon), "Taxon ID is invalid", rule=27) return assocparser.ParseResult(line, [], True) #TODO: ecomap is currently one-way only #ecomap = self.config.ecomap #if ecomap != None: # if ecomap.ecoclass_to_coderef(evidence) == (None,None): # self.report.error(line, Report.UNKNOWN_EVIDENCE_CLASS, evidence, # msg="Expecting a known ECO class ID") # Reference Column references = self.validate_curie_ids( assoc.evidence.has_supporting_reference, split_line) if references is None: return assocparser.ParseResult(line, [], True) # With/From for wf in assoc.evidence.with_support_from: validated = self.validate_curie_ids(wf.elements, split_line) if validated is None: return assocparser.ParseResult(line, [], True) return assocparser.ParseResult(line, [assoc], False)
def parse_line(self, line): """Parses a single line of a GPAD. Return a tuple `(processed_line, associations)`. Typically there will be a single association, but in some cases there may be none (invalid line) or multiple (disjunctive clause in annotation extensions) Note: most applications will only need to call this directly if they require fine-grained control of parsing. For most purposes, :method:`parse_file` can be used over the whole file Arguments --------- line : str A single tab-seperated line from a GPAD file """ parsed = super().validate_line(line) if parsed: return parsed if self.is_header(line): return assocparser.ParseResult(line, [], False) vals = [el.strip() for el in line.split("\t")] if len(vals) < 10 or len(vals) > 12: self.report.error( line, assocparser.Report.WRONG_NUMBER_OF_COLUMNS, "", msg= "There were {columns} columns found in this line, and there should be between 10 and 12" .format(columns=len(vals))) return assocparser.ParseResult(line, [], True) if len(vals) < 12: vals += [""] * (12 - len(vals)) [ db, db_object_id, qualifier, goid, reference, evidence, withfrom, interacting_taxon_id, date, assigned_by, annotation_xp, annotation_properties ] = vals split_line = assocparser.SplitLine(line=line, values=vals, taxon="") id = self._pair_to_id(db, db_object_id) if not self._validate_id(id, split_line, context=ENTITY): return assocparser.ParseResult(line, [], True) if not self._validate_id(goid, split_line, context=ANNOTATION): return assocparser.ParseResult(line, [], True) valid_goid = self._validate_ontology_class_id(goid, split_line) if valid_goid == None: return assocparser.ParseResult(line, [], True) goid = valid_goid date = self._normalize_gaf_date(date, split_line) if reference == "": self.report.error(line, Report.INVALID_ID, "EMPTY", "reference column 6 is empty") return assocparser.ParseResult(line, [], True) self._validate_id(evidence, split_line) interacting_taxon = None if interacting_taxon_id == "" else interacting_taxon_id if interacting_taxon != None: interacting_taxon = self._taxon_id(interacting_taxon_id, split_line) if interacting_taxon == None: self.report.error(line, assocparser.Report.INVALID_TAXON, interacting_taxon_id, msg="Taxon ID is invalid") return assocparser.ParseResult(line, [], True) #TODO: ecomap is currently one-way only #ecomap = self.config.ecomap #if ecomap != None: # if ecomap.ecoclass_to_coderef(evidence) == (None,None): # self.report.error(line, Report.UNKNOWN_EVIDENCE_CLASS, evidence, # msg="Expecting a known ECO class ID") ## -- ## qualifier ## -- negated, relation, other_qualifiers = self._parse_qualifier( qualifier, None) # Reference Column references = self.validate_pipe_separated_ids(reference, split_line) if references == None: # Reporting occurs in above function call return assocparser.ParseResult(line, [], True) # With/From withfroms = self.validate_pipe_separated_ids(withfrom, split_line, empty_allowed=True, extra_delims=",") if withfroms == None: # Reporting occurs in above function call return assocparser.ParseResult(line, [], True) ## -- ## parse annotation extension ## See appending in http://doi.org/10.1186/1471-2105-15-155 ## -- object_or_exprs = self._parse_full_extension_expression( annotation_xp, line=split_line) assoc = { 'source_line': line, 'subject': { 'id': id }, 'object': { 'id': goid }, 'negated': negated, 'relation': { 'id': relation }, 'interacting_taxon': interacting_taxon, 'evidence': { 'type': evidence, 'with_support_from': withfroms, 'has_supporting_reference': references }, 'provided_by': assigned_by, 'date': date, } if len(other_qualifiers) > 0: assoc['qualifiers'] = other_qualifiers if object_or_exprs is not None and len(object_or_exprs) > 0: assoc['object']['extensions'] = {'union_of': object_or_exprs} return assocparser.ParseResult(line, [assoc], False)
def test_correct_taxon_mapping(): parser = gafparser.GafParser() mapped = parser._taxon_id( "taxon:123", assocparser.SplitLine("", [""] * 17, "taxon:123")) assert mapped == "NCBITaxon:123"
def parse_line(self, line): """Parses a single line of a GPI. Return a tuple `(processed_line, entities)`. Typically there will be a single entity, but in some cases there may be none (invalid line) or multiple (disjunctive clause in annotation extensions) Note: most applications will only need to call this directly if they require fine-grained control of parsing. For most purposes, :method:`parse_file` can be used over the whole file Arguments --------- line : str A single tab-seperated line from a GPAD file """ vals = line.split("\t") if len(vals) < 7: self.report.error(line, Report.WRONG_NUMBER_OF_COLUMNS, "") return line, [] if len(vals) < 10 and len(vals) >= 7: missing_columns = 10 - len(vals) vals += ["" for i in range(missing_columns)] [ db, db_object_id, db_object_symbol, db_object_name, db_object_synonym, db_object_type, taxon, parent_object_id, xrefs, properties ] = vals split_line = assocparser.SplitLine(line=line, values=vals, taxon=taxon) ## -- ## db + db_object_id. CARD=1 ## -- id = self._pair_to_id(db, db_object_id) if not self._validate_id(id, split_line, ENTITY): return line, [] ## -- ## db_object_synonym CARD=0..* ## -- synonyms = db_object_synonym.split("|") if db_object_synonym == "": synonyms = [] # TODO: DRY parents = parent_object_id.split("|") if parent_object_id == "": parents = [] else: parents = [self._normalize_id(x) for x in parents] for p in parents: self._validate_id(p, split_line, ENTITY) xref_ids = xrefs.split("|") if xrefs == "": xref_ids = [] obj = { 'id': id, 'label': db_object_symbol, 'full_name': db_object_name, 'synonyms': synonyms, 'type': db_object_type, 'parents': parents, 'xrefs': xref_ids, 'taxon': { 'id': self._taxon_id(taxon, split_line) } } return line, [obj]
def parse_line(self, line): """ Parses a single line of a GAF Return a tuple `(processed_line, associations)`. Typically there will be a single association, but in some cases there may be none (invalid line) or multiple (disjunctive clause in annotation extensions) Note: most applications will only need to call this directly if they require fine-grained control of parsing. For most purposes, :method:`parse_file` can be used over the whole file Arguments --------- line : str A single tab-seperated line from a GAF file """ # Returns assocparser.ParseResult parsed = super().validate_line(line) if parsed: return parsed if self.is_header(line): # Save off version info here if self.version is None: # We are still looking parsed = parser_version_regex.findall(line) if len(parsed) == 1: filetype, version, _ = parsed[0] if version == "2.2": logger.info("Detected GAF version 2.2") self.version = version else: logger.info("Detected GAF version {}, so using 2.1".format(version)) self.version = self.default_version # Compute the cell component subclass closure self.make_internal_cell_component_closure() return assocparser.ParseResult(line, [{ "header": True, "line": line.strip() }], False) # At this point, we should have gone through all the header, and a version number should be established if self.version is None: logger.warning("No version number found for this file so we will assume GAF version: {}".format(self.default_version)) self.version = self.default_version self.make_internal_cell_component_closure() vals = [el.strip() for el in line.split("\t")] # GAF v1 is defined as 15 cols, GAF v2 as 17. # We treat everything as GAF2 by adding two blank columns. # TODO: check header metadata to see if columns corresponds to declared dataformat version parsed = to_association(list(vals), report=self.report, qualifier_parser=self.qualifier_parser(), bio_entities=self.bio_entities) if parsed.associations == []: return parsed assoc = parsed.associations[0] # Qualifier is index 3 # If we are 2.1, and qualifier has no relation # Also must have an ontology # Then upgrade # For https://github.com/geneontology/go-site/issues/1558 if self.gaf_version() == "2.1" and (vals[3] == "" or vals[3] == "NOT") and self.config.ontology: assoc = self.upgrade_empty_qualifier(assoc) ## Run GO Rules, save split values into individual variables # print("Config is {}".format(self.config.__dict__.keys())) go_rule_results = qc.test_go_rules(assoc, self.config, group=self.group) for rule, result in go_rule_results.all_results.items(): if result.result_type == qc.ResultType.WARNING: self.report.warning(line, assocparser.Report.VIOLATES_GO_RULE, "", msg="{id}: {message}".format(id=rule.id, message=result.message), rule=int(rule.id.split(":")[1])) if result.result_type == qc.ResultType.ERROR: self.report.error(line, assocparser.Report.VIOLATES_GO_RULE, "", msg="{id}: {message}".format(id=rule.id, message=result.message), rule=int(rule.id.split(":")[1])) # Skip the annotation return assocparser.ParseResult(line, [], True) if result.result_type == qc.ResultType.PASS: self.report.message(assocparser.Report.INFO, line, Report.RULE_PASS, "", msg="Passing Rule", rule=int(rule.id.split(":")[1])) assoc = go_rule_results.annotation # type: association.GoAssociation split_line = assocparser.SplitLine(line=line, values=vals, taxon=str(assoc.object.taxon)) if self.config.group_idspace is not None and assoc.provided_by not in self.config.group_idspace: self.report.warning(line, Report.INVALID_ID, assoc.provided_by, "GORULE:0000027: assigned_by is not present in groups reference", taxon=str(assoc.object.taxon), rule=27) db = assoc.subject.id.namespace if self.config.entity_idspaces is not None and db not in self.config.entity_idspaces: # Are we a synonym? upgrade = self.config.entity_idspaces.reverse(db) if upgrade is not None: # If we found a synonym self.report.warning(line, Report.INVALID_ID_DBXREF, db, "GORULE:0000027: {} is a synonym for the correct ID {}, and has been updated".format(db, upgrade), taxon=str(assoc.object.taxon), rule=27) assoc.subject.id.namespace = upgrade ## -- ## db + db_object_id. CARD=1 ## --assigned_by if not self._validate_id(str(assoc.subject.id), split_line, allowed_ids=self.config.entity_idspaces): return assocparser.ParseResult(line, [], True) # Using a given gpi file to validate the gene object # if self.gpi is not None: # entity = self.gpi.get(str(assoc.subject.id), None) # if entity is not None: # assoc.subject.label = entity["symbol"] # assoc.subject.fullname = entity["name"] # assoc.subject.synonyms = entity["synonyms"].split("|") # assoc.subject.type = entity["type"] if not self._validate_id(str(assoc.object.id), split_line, context=ANNOTATION): print("skipping because {} not validated!".format(assoc.object.id)) return assocparser.ParseResult(line, [], True) valid_goid = self._validate_ontology_class_id(str(assoc.object.id), split_line) if valid_goid is None: return assocparser.ParseResult(line, [], True) assoc.object.id = association.Curie.from_str(valid_goid) references = self.validate_curie_ids(assoc.evidence.has_supporting_reference, split_line) if references is None: # Reporting occurs in above function call return assocparser.ParseResult(line, [], True) # With/From for wf in assoc.evidence.with_support_from: validated = self.validate_curie_ids(wf.elements, split_line) if validated is None: return assocparser.ParseResult(line, [], True) with_support_from = self._unroll_withfrom_and_replair_obsoletes(split_line, 'gaf') if with_support_from is None: return assocparser.ParseResult(line, [], True) assoc.evidence.with_support_from = with_support_from # validation self._validate_symbol(assoc.subject.label, split_line) ## -- ## taxon CARD={1,2} ## -- ## if a second value is specified, this is the interacting taxon ## We do not use the second value valid_taxon = self._validate_taxon(str(assoc.object.taxon), split_line) valid_interacting = self._validate_taxon(str(assoc.interacting_taxon), split_line) if assoc.interacting_taxon else True if not valid_taxon: self.report.error(line, assocparser.Report.INVALID_TAXON, str(assoc.object.taxon), "Taxon ID is invalid", rule=27) if not valid_interacting: self.report.error(line, assocparser.Report.INVALID_TAXON, str(assoc.interacting_taxon), "Taxon ID is invalid", rule=27) if (not valid_taxon) or (not valid_interacting): return assocparser.ParseResult(line, [], True) return assocparser.ParseResult(line, [assoc], False, vals[6])
def test_invalid_mapping(): parser = gafparser.GafParser() mapped = parser._taxon_id("taxon:", assocparser.SplitLine("", [""] * 17, "taxon:")) assert mapped == None
def parse_line(self, line): """ Parses a single line of a HPOA file Return a tuple `(processed_line, associations)`. Typically there will be a single association, but in some cases there may be none (invalid line) or multiple (disjunctive clause in annotation extensions) Note: most applications will only need to call this directly if they require fine-grained control of parsing. For most purposes, :method:`parse_file` can be used over the whole file Arguments --------- line : str A single tab-seperated line from a GPAD file """ config = self.config parsed = super().validate_line(line) if parsed: return parsed if self.is_header(line): return assocparser.ParseResult(line, [], False) # http://human-phenotype-ontology.github.io/documentation.html#annot vals = line.split("\t") if len(vals) != 14: self.report.error( line, assocparser.Report.WRONG_NUMBER_OF_COLUMNS, "", msg= "There were {columns} columns found in this line, and there should be 14" .format(columns=len(vals))) return assocparser.ParseResult(line, [], True) [ db, db_object_id, db_object_symbol, qualifier, hpoid, reference, evidence, onset, frequency, withfrom, aspect, db_object_synonym, date, assigned_by ] = vals # hardcode this, as HPOA is currently human-only taxon = 'NCBITaxon:9606' split_line = assocparser.SplitLine(line=line, values=vals, taxon=taxon) # hardcode this, as HPOA is currently disease-only db_object_type = 'disease' ## -- ## db + db_object_id. CARD=1 ## -- id = self._pair_to_id(db, db_object_id) if not self._validate_id(id, split_line, context=ENTITY): return assocparser.ParseResult(line, [], True) if not self._validate_id(hpoid, split_line, context=ANNOTATION): return assocparser.ParseResult(line, [], True) valid_hpoid = self._validate_ontology_class_id(hpoid, split_line) if valid_hpoid == None: return assocparser.ParseResult(line, [], True) hpoid = valid_hpoid # validation #self._validate_symbol(db_object_symbol, line) #TODO: HPOA has different date styles #date = self._normalize_gaf_date(date, line) # Example use case: mapping from OMIM to Orphanet if config.entity_map is not None: id = self.map_id(id, config.entity_map) toks = id.split(":") db = toks[0] db_object_id = toks[1:] vals[1] = db_object_id ## -- ## end of line re-processing ## -- # regenerate line post-mapping line = "\t".join(vals) ## -- ## db_object_synonym CARD=0..* ## -- synonyms = db_object_synonym.split("|") if db_object_synonym == "": synonyms = [] ## -- ## qualifier ## -- ## we generate both qualifier and relation field relation = None qualifiers = qualifier.split("|") if qualifier == '': qualifiers = [] negated = 'NOT' in qualifiers other_qualifiers = [q for q in qualifiers if q != 'NOT'] ## CURRENTLY NOT USED if len(other_qualifiers) > 0: relation = other_qualifiers[0] else: if aspect == 'O': relation = 'has_phenotype' elif aspect == 'I': relation = 'has_inheritance' elif aspect == 'M': relation = 'mortality' elif aspect == 'C': relation = 'has_onset' else: relation = None # With/From withfroms = self.validate_pipe_separated_ids(withfrom, split_line, empty_allowed=True, extra_delims=",") if withfroms is None: # Reporting occurs in above function call return assocparser.ParseResult(line, [], True) ## -- ## hpoid ## -- object = {'id': hpoid, 'taxon': taxon} # construct subject dict subject = { 'id': id, 'label': db_object_symbol, 'type': db_object_type, 'synonyms': synonyms, 'taxon': { 'id': taxon } } ## -- ## evidence ## reference ## withfrom ## -- evidence = { 'type': evidence, 'has_supporting_reference': reference.split("; "), 'with_support_from': withfroms } ## Construct main return dict assoc = { 'source_line': line, 'subject': subject, 'object': object, 'negated': negated, 'qualifiers': qualifiers, 'relation': { 'id': relation }, 'interacting_taxon': None, 'evidence': evidence, 'provided_by': assigned_by, 'date': date, } return assocparser.ParseResult(line, [assoc], False)