<commit_before>import pytest
import datetime
import yaml
import json

from ontobio.model import association
from ontobio.model.association import Curie
from ontobio.io import qc
from ontobio.io import gaference
from ontobio.io import assocparser
from ontobio.io import gafparser
from ontobio.io import gpadparser
from ontobio import ontol, ontol_factory

import copy

ontology = ontol_factory.OntologyFactory().create("tests/resources/goslim_generic.json")

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 all_rules_config(ontology=None) -> assocparser.AssocParserConfig:
    return assocparser.AssocParserConfig(
        ontology=ontology,
        rule_set=assocparser.RuleSet.ALL
    )

def test_qc_result():
    assert qc.result(True, qc.FailMode.HARD) == qc.ResultType.PASS
    assert qc.result(False, qc.FailMode.HARD) == qc.ResultType.ERROR
    assert qc.result(False, qc.FailMode.SOFT) == qc.ResultType.WARNING

def test_go_rule02():

    assoc = make_annotation(qualifier="NOT", goid="GO:0005515").associations[0]

    test_result = qc.GoRule02().test(assoc, all_rules_config())
    assert test_result.result_type == qc.ResultType.WARNING

    assoc.negated = False
    test_result = qc.GoRule02().test(assoc, all_rules_config())
    assert test_result.result_type == qc.ResultType.PASS

    assoc.negated = True
    assoc.object.id = Curie.from_str("GO:0003674")
    test_result = qc.GoRule02().test(assoc, all_rules_config())
    assert test_result.result_type == qc.ResultType.PASS

def test_go_rule_06():

    assoc = make_annotation(goid="GO:0005575", evidence="HEP", aspect="C").associations[0]

    test_result = qc.GoRule06().test(assoc, all_rules_config(ontology=ontology))
    assert test_result.result_type == qc.ResultType.ERROR

    assoc.aspect = "P"
    assoc.object.id = Curie.from_str("GO:0008150")
    test_result = qc.GoRule06().test(assoc, all_rules_config(ontology=ontology))
    assert test_result.result_type == qc.ResultType.PASS

    assoc.evidence.type = Curie.from_str("ECO:0000501")
    test_result = qc.GoRule06().test(assoc, all_rules_config(ontology=ontology))
    assert test_result.result_type == qc.ResultType.PASS

def test_go_rule_07():

    assoc = make_annotation(goid="GO:0003824", evidence="IPI").associations[0]

    test_result = qc.GoRule07().test(assoc, all_rules_config(ontology=ontology))
    assert test_result.result_type == qc.ResultType.WARNING

    assoc.object.id = Curie.from_str("GO:1234567")
    test_result = qc.GoRule07().test(assoc, all_rules_config(ontology=ontology))
    assert test_result.result_type == qc.ResultType.PASS

    assoc.object.id = Curie.from_str("GO:0003824")
    assoc.evidence.type = "ECO:0000501" # Not IPI
    test_result = qc.GoRule07().test(assoc, all_rules_config(ontology=ontology))
    assert test_result.result_type == qc.ResultType.PASS

def test_go_rule08():

    assoc = make_annotation(goid="GO:0006810", evidence="IEA").associations[0]

    test_result = qc.GoRule08().test(assoc, all_rules_config(ontology=ontology))
    assert test_result.result_type == qc.ResultType.WARNING

    assoc.evidence.type = Curie.from_str("ECO:0000305")
    test_result = qc.GoRule08().test(assoc, all_rules_config(ontology=ontology))
    assert test_result.result_type == qc.ResultType.WARNING

    assoc.object.id = Curie.from_str("GO:0007049")  # do not manually annotate
    # evidence is IC, non IEA
    test_result = qc.GoRule08().test(assoc, all_rules_config(ontology=ontology))
    assert test_result.result_type == qc.ResultType.WARNING

    assoc.evidence.type = Curie.from_str("ECO:0000501") #IEA
    # IEA, but on not manual, so should pass
    test_result = qc.GoRule08().test(assoc, all_rules_config(ontology=ontology))
    assert test_result.result_type == qc.ResultType.PASS

    assoc.object.id = Curie.from_str("GO:0034655")  # neither
    assoc.evidence.type = Curie.from_str("ECO:0000305")
    test_result = qc.GoRule08().test(assoc, all_rules_config(ontology=ontology))
    assert test_result.result_type == qc.ResultType.PASS

def test_go_rule11():

    assoc = make_annotation(goid="GO:0003674", evidence="ND").associations[0]
    test_result = qc.GoRule11().test(assoc, all_rules_config())
    assert test_result.result_type == qc.ResultType.PASS

    # Bad GO ID
    assoc = make_annotation(goid="GO:1234567", evidence="ND").associations[0]
    test_result = qc.GoRule11().test(assoc, all_rules_config())
    assert test_result.result_type == qc.ResultType.ERROR

    # Not ND and not Root
    assoc = make_annotation(goid="GO:1234567", evidence="IEA").associations[0]
    test_result = qc.GoRule11().test(assoc, all_rules_config())
    assert test_result.result_type == qc.ResultType.PASS

    # Root, but not ND
    assoc = make_annotation(goid="GO:0003674", evidence="IEA").associations[0]
    test_result = qc.GoRule11().test(assoc, all_rules_config())
    assert test_result.result_type == qc.ResultType.ERROR

def test_go_rules_13():

    a = ["PomBase", "SPBC11B10.09", "cdc2", "", "GO:0007275", "PMID:21873635", "IBA", "PANTHER:PTN000623979|TAIR:locus:2099478", "P", "Cyclin-dependent kinase 1", "UniProtKB:P04551|PTN000624043", "protein", "taxon:284812", "20170228", "GO_Central", "", ""]
    assoc = gafparser.to_association(a).associations[0]
    gaferences = gaference.load_gaferencer_inferences_from_file("tests/resources/test.inferences.json")
    test_result = qc.GoRule13().test(assoc, assocparser.AssocParserConfig(annotation_inferences=gaferences, rule_set=assocparser.RuleSet.ALL))
    assert test_result.result_type == qc.ResultType.ERROR

    a = ["PomBase", "SPBC11B10.09", "cdc2", "", "GO:0007275", "PMID:21873635", "EXP", "PANTHER:PTN000623979|TAIR:locus:2099478", "P", "Cyclin-dependent kinase 1", "UniProtKB:P04551|PTN000624043", "protein", "taxon:284812", "20170228", "GO_Central", "", ""]
    assoc = gafparser.to_association(a).associations[0]
    gaferences = gaference.load_gaferencer_inferences_from_file("tests/resources/test.inferences.json")
    test_result = qc.GoRule13().test(assoc, assocparser.AssocParserConfig(annotation_inferences=gaferences, rule_set=assocparser.RuleSet.ALL))
    assert test_result.result_type == qc.ResultType.WARNING

    a = ["PomBase", "SPBC11B10.09", "cdc2", "NOT", "GO:0007275", "PMID:21873635", "EXP", "PANTHER:PTN000623979|TAIR:locus:2099478", "P", "Cyclin-dependent kinase 1", "UniProtKB:P04551|PTN000624043", "protein", "taxon:284812", "20170228", "GO_Central", "", ""]
    assoc = gafparser.to_association(a).associations[0]
    gaferences = gaference.load_gaferencer_inferences_from_file("tests/resources/test.inferences.json")
    test_result = qc.GoRule13().test(assoc, assocparser.AssocParserConfig(annotation_inferences=gaferences, rule_set=assocparser.RuleSet.ALL))
    assert test_result.result_type == qc.ResultType.PASS

    a = ["AspGD", "ASPL0000059928", "AN0127", "", "GO:0032258", "AspGD_REF:ASPL0000000005", "IEA", "SGD:S000001917", "P", "", "AN0127|ANID_00127|ANIA_00127", "gene_product", "taxon:227321", "20200201", "AspGD", "", ""]
    assoc = gafparser.to_association(a).associations[0]
    gaferences = gaference.load_gaferencer_inferences_from_file("tests/resources/test.inferences.json")
    test_result = qc.GoRule13().test(assoc, assocparser.AssocParserConfig(annotation_inferences=gaferences, rule_set=assocparser.RuleSet.ALL))
    assert test_result.result_type == qc.ResultType.ERROR

def test_go_rules_15():

    assoc = make_annotation(goid="GO:0044419", taxon="taxon:123|taxon:456").associations[0]

    ontology = ontol_factory.OntologyFactory().create("tests/resources/go-interspecies.json")

    test_result = qc.GoRule15().test(assoc, all_rules_config(ontology=ontology))
    assert test_result.result_type == qc.ResultType.PASS

    assoc.object.id = Curie.from_str("GO:1234567")
    test_result = qc.GoRule15().test(assoc, all_rules_config(ontology=ontology))
    assert test_result.result_type == qc.ResultType.WARNING

    assoc.object.id = Curie.from_str("GO:0002812")
    test_result = qc.GoRule15().test(assoc, all_rules_config(ontology=ontology))
    assert test_result.result_type == qc.ResultType.PASS

    assoc.object.id = Curie.from_str("GO:0044215")
    assoc.object.id = Curie.from_str("NCBITaxon:123")
    assoc.interacting_taxon = None # This is the important part, no interacting taxon
    test_result = qc.GoRule15().test(assoc, all_rules_config(ontology=ontology))
    assert test_result.result_type == qc.ResultType.PASS

def test_go_rule_16():
    # No GO term w/ID
    assoc = make_annotation(evidence="IC", withfrom="BLAH:12345").associations[0]
<commit_msg>Update filename in go rule 15 test<commit_after>import pytest
import datetime
import yaml
import json

from ontobio.model import association
from ontobio.model.association import Curie
from ontobio.io import qc
from ontobio.io import gaference
from ontobio.io import assocparser
from ontobio.io import gafparser
from ontobio.io import gpadparser
from ontobio import ontol, ontol_factory

import copy

ontology = ontol_factory.OntologyFactory().create("tests/resources/goslim_generic.json")

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 all_rules_config(ontology=None) -> assocparser.AssocParserConfig:
    return assocparser.AssocParserConfig(
        ontology=ontology,
        rule_set=assocparser.RuleSet.ALL
    )

def test_qc_result():
    assert qc.result(True, qc.FailMode.HARD) == qc.ResultType.PASS
    assert qc.result(False, qc.FailMode.HARD) == qc.ResultType.ERROR
    assert qc.result(False, qc.FailMode.SOFT) == qc.ResultType.WARNING

def test_go_rule02():

    assoc = make_annotation(qualifier="NOT", goid="GO:0005515").associations[0]

    test_result = qc.GoRule02().test(assoc, all_rules_config())
    assert test_result.result_type == qc.ResultType.WARNING

    assoc.negated = False
    test_result = qc.GoRule02().test(assoc, all_rules_config())
    assert test_result.result_type == qc.ResultType.PASS

    assoc.negated = True
    assoc.object.id = Curie.from_str("GO:0003674")
    test_result = qc.GoRule02().test(assoc, all_rules_config())
    assert test_result.result_type == qc.ResultType.PASS

def test_go_rule_06():

    assoc = make_annotation(goid="GO:0005575", evidence="HEP", aspect="C").associations[0]

    test_result = qc.GoRule06().test(assoc, all_rules_config(ontology=ontology))
    assert test_result.result_type == qc.ResultType.ERROR

    assoc.aspect = "P"
    assoc.object.id = Curie.from_str("GO:0008150")
    test_result = qc.GoRule06().test(assoc, all_rules_config(ontology=ontology))
    assert test_result.result_type == qc.ResultType.PASS

    assoc.evidence.type = Curie.from_str("ECO:0000501")
    test_result = qc.GoRule06().test(assoc, all_rules_config(ontology=ontology))
    assert test_result.result_type == qc.ResultType.PASS

def test_go_rule_07():

    assoc = make_annotation(goid="GO:0003824", evidence="IPI").associations[0]

    test_result = qc.GoRule07().test(assoc, all_rules_config(ontology=ontology))
    assert test_result.result_type == qc.ResultType.WARNING

    assoc.object.id = Curie.from_str("GO:1234567")
    test_result = qc.GoRule07().test(assoc, all_rules_config(ontology=ontology))
    assert test_result.result_type == qc.ResultType.PASS

    assoc.object.id = Curie.from_str("GO:0003824")
    assoc.evidence.type = "ECO:0000501" # Not IPI
    test_result = qc.GoRule07().test(assoc, all_rules_config(ontology=ontology))
    assert test_result.result_type == qc.ResultType.PASS

def test_go_rule08():

    assoc = make_annotation(goid="GO:0006810", evidence="IEA").associations[0]

    test_result = qc.GoRule08().test(assoc, all_rules_config(ontology=ontology))
    assert test_result.result_type == qc.ResultType.WARNING

    assoc.evidence.type = Curie.from_str("ECO:0000305")
    test_result = qc.GoRule08().test(assoc, all_rules_config(ontology=ontology))
    assert test_result.result_type == qc.ResultType.WARNING

    assoc.object.id = Curie.from_str("GO:0007049")  # do not manually annotate
    # evidence is IC, non IEA
    test_result = qc.GoRule08().test(assoc, all_rules_config(ontology=ontology))
    assert test_result.result_type == qc.ResultType.WARNING

    assoc.evidence.type = Curie.from_str("ECO:0000501") #IEA
    # IEA, but on not manual, so should pass
    test_result = qc.GoRule08().test(assoc, all_rules_config(ontology=ontology))
    assert test_result.result_type == qc.ResultType.PASS

    assoc.object.id = Curie.from_str("GO:0034655")  # neither
    assoc.evidence.type = Curie.from_str("ECO:0000305")
    test_result = qc.GoRule08().test(assoc, all_rules_config(ontology=ontology))
    assert test_result.result_type == qc.ResultType.PASS

def test_go_rule11():

    assoc = make_annotation(goid="GO:0003674", evidence="ND").associations[0]
    test_result = qc.GoRule11().test(assoc, all_rules_config())
    assert test_result.result_type == qc.ResultType.PASS

    # Bad GO ID
    assoc = make_annotation(goid="GO:1234567", evidence="ND").associations[0]
    test_result = qc.GoRule11().test(assoc, all_rules_config())
    assert test_result.result_type == qc.ResultType.ERROR

    # Not ND and not Root
    assoc = make_annotation(goid="GO:1234567", evidence="IEA").associations[0]
    test_result = qc.GoRule11().test(assoc, all_rules_config())
    assert test_result.result_type == qc.ResultType.PASS

    # Root, but not ND
    assoc = make_annotation(goid="GO:0003674", evidence="IEA").associations[0]
    test_result = qc.GoRule11().test(assoc, all_rules_config())
    assert test_result.result_type == qc.ResultType.ERROR

def test_go_rules_13():

    a = ["PomBase", "SPBC11B10.09", "cdc2", "", "GO:0007275", "PMID:21873635", "IBA", "PANTHER:PTN000623979|TAIR:locus:2099478", "P", "Cyclin-dependent kinase 1", "UniProtKB:P04551|PTN000624043", "protein", "taxon:284812", "20170228", "GO_Central", "", ""]
    assoc = gafparser.to_association(a).associations[0]
    gaferences = gaference.load_gaferencer_inferences_from_file("tests/resources/test.inferences.json")
    test_result = qc.GoRule13().test(assoc, assocparser.AssocParserConfig(annotation_inferences=gaferences, rule_set=assocparser.RuleSet.ALL))
    assert test_result.result_type == qc.ResultType.ERROR

    a = ["PomBase", "SPBC11B10.09", "cdc2", "", "GO:0007275", "PMID:21873635", "EXP", "PANTHER:PTN000623979|TAIR:locus:2099478", "P", "Cyclin-dependent kinase 1", "UniProtKB:P04551|PTN000624043", "protein", "taxon:284812", "20170228", "GO_Central", "", ""]
    assoc = gafparser.to_association(a).associations[0]
    gaferences = gaference.load_gaferencer_inferences_from_file("tests/resources/test.inferences.json")
    test_result = qc.GoRule13().test(assoc, assocparser.AssocParserConfig(annotation_inferences=gaferences, rule_set=assocparser.RuleSet.ALL))
    assert test_result.result_type == qc.ResultType.WARNING

    a = ["PomBase", "SPBC11B10.09", "cdc2", "NOT", "GO:0007275", "PMID:21873635", "EXP", "PANTHER:PTN000623979|TAIR:locus:2099478", "P", "Cyclin-dependent kinase 1", "UniProtKB:P04551|PTN000624043", "protein", "taxon:284812", "20170228", "GO_Central", "", ""]
    assoc = gafparser.to_association(a).associations[0]
    gaferences = gaference.load_gaferencer_inferences_from_file("tests/resources/test.inferences.json")
    test_result = qc.GoRule13().test(assoc, assocparser.AssocParserConfig(annotation_inferences=gaferences, rule_set=assocparser.RuleSet.ALL))
    assert test_result.result_type == qc.ResultType.PASS

    a = ["AspGD", "ASPL0000059928", "AN0127", "", "GO:0032258", "AspGD_REF:ASPL0000000005", "IEA", "SGD:S000001917", "P", "", "AN0127|ANID_00127|ANIA_00127", "gene_product", "taxon:227321", "20200201", "AspGD", "", ""]
    assoc = gafparser.to_association(a).associations[0]
    gaferences = gaference.load_gaferencer_inferences_from_file("tests/resources/test.inferences.json")
    test_result = qc.GoRule13().test(assoc, assocparser.AssocParserConfig(annotation_inferences=gaferences, rule_set=assocparser.RuleSet.ALL))
    assert test_result.result_type == qc.ResultType.ERROR

def test_go_rules_15():

    assoc = make_annotation(goid="GO:0044419", taxon="taxon:123|taxon:456").associations[0]

    ontology = ontol_factory.OntologyFactory().create("tests/resources/go-interspecies-20210520.json")

    test_result = qc.GoRule15().test(assoc, all_rules_config(ontology=ontology))
    assert test_result.result_type == qc.ResultType.PASS

    assoc.object.id = Curie.from_str("GO:1234567")
    test_result = qc.GoRule15().test(assoc, all_rules_config(ontology=ontology))
    assert test_result.result_type == qc.ResultType.WARNING

    assoc.object.id = Curie.from_str("GO:0002812")
    test_result = qc.GoRule15().test(assoc, all_rules_config(ontology=ontology))
    assert test_result.result_type == qc.ResultType.PASS

    assoc.object.id = Curie.from_str("GO:0044215")
    assoc.object.id = Curie.from_str("NCBITaxon:123")
    assoc.interacting_taxon = None # This is the important part, no interacting taxon
    test_result = qc.GoRule15().test(assoc, all_rules_config(ontology=ontology))
    assert test_result.result_type == qc.ResultType.PASS

def test_go_rule_16():
    # No GO term w/ID
    assoc = make_annotation(evidence="IC", withfrom="BLAH:12345").associations[0]

{'fraction_non_alphanumeric': 0.1314133591481123, 'fraction_numerical': 0.05634075508228461, 'mean_word_length': 4.2253414264036415, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1728083', 'n_tokens_mistral': 7966, 'n_tokens_neox': 7118, 'n_words': 1088}