Пример #1
0
    def test_handle_relationships(self):
        # Tests the insertion, update and deletion of relationships between CV terms

        # Populate the database with essentials
        other_dbxref = general.DbxRef(db_id=self.default_db.db_id, accession="otheraccession")
        self.client.add_and_flush(other_dbxref)
        other_cvterm = cv.CvTerm(cv_id=self.default_cv.cv_id, dbxref_id=other_dbxref.dbxref_id, name="otherterm")
        self.client.add_and_flush(other_cvterm)
        default_id = ontology.create_dbxref(self.default_db.name, self.default_dbxref.accession)
        other_id = ontology.create_dbxref(self.default_db.name, other_dbxref.accession)
        all_cvterms = {default_id: self.default_cvterm, other_id: other_cvterm}

        # Check that an ontology term without relationships will not create a cvterm_relationship entry
        term = pronto.Term("defaultdb:defaultaccession")
        first_relationships = self.client._handle_relationships(term, self.default_cvterm, all_cvterms)
        self.assertEqual(len(first_relationships), 0)

        # Insert a relationship
        term.relations = {pronto.Relationship("part_of"): [pronto.Term(other_id, other_cvterm.name)]}
        second_relationships = self.client._handle_relationships(term, self.default_cvterm, all_cvterms)
        self.assertEqual(len(second_relationships), 1)
        self.assertIsNotNone(second_relationships[0].cvterm_relationship_id)
        self.assertEqual(second_relationships[0].subject_id, self.default_cvterm.cvterm_id)
        self.assertEqual(second_relationships[0].object_id, other_cvterm.cvterm_id)
        self.assertEqual(second_relationships[0].type_id, self.client._relationship_terms["part_of"].cvterm_id)

        # Insert a new relationship between the same terms
        term.relations.clear()
        term.relations = {pronto.Relationship("is_a"): [pronto.Term(other_id, other_cvterm.name)]}
        third_relationships = self.client._handle_relationships(term, self.default_cvterm, all_cvterms)
        self.assertEqual(len(third_relationships), 1)
        self.assertEqual(third_relationships[0].type_id, self.client._relationship_terms["is_a"].cvterm_id)
Пример #2
0
 def test_create_cvterm_entry(self):
     # Checks if an ontology term is correctly converted into a CV term entry
     term = pronto.Term(id="testid", name="testname", desc="testdescription", other={"is_obsolete": ["True"]})
     cvterm_entry = ontology.create_cvterm_entry(term, 100, 200)
     self.assertEqual(cvterm_entry, cv.CvTerm(cv_id=100, dbxref_id=200, name="testname",
                                              definition="testdescription", is_obsolete=1))
     term = pronto.Term(id="otherid", name="othername", other={"is_obsolete": ["False"]})
     cvterm_entry = ontology.create_cvterm_entry(term, 10, 20)
     self.assertEqual(cvterm_entry, cv.CvTerm(cv_id=10, dbxref_id=20, name="othername", is_obsolete=0))
Пример #3
0
 def test_filter_ontology(self):
     # Checks the filtering of ontology terms by a database authority
     ont = pronto.Ontology()
     ont.include(pronto.Term("test:001:abc"))
     ont.include(pronto.Term("test:002"))
     ont.include(pronto.Term("other:001"))
     filtered_ontology = ontology.filter_ontology_by_db(ont, "test")
     self.assertEqual(len(filtered_ontology), 2)
     self.assertIn("test:001:abc", filtered_ontology)
     self.assertEqual(filtered_ontology["test:002"].id, "test:002")
Пример #4
0
 def test_are_terms_equivalent(self):
     term1_a = pronto.Term(id='term1')
     term1_b = pronto.Term(id='term1')
     term2 = pronto.Term(id='term2')
     self.assertFalse(term1_a == term1_b)
     self.assertTrue(
         wc_utils.util.ontology.are_terms_equivalent(term1_a, term1_b))
     self.assertTrue(
         wc_utils.util.ontology.are_terms_equivalent(term1_b, term1_a))
     self.assertFalse(
         wc_utils.util.ontology.are_terms_equivalent(term1_a, term2))
     self.assertFalse(
         wc_utils.util.ontology.are_terms_equivalent(term2, term1_a))
Пример #5
0
    def test_handle_cross_references(self):
        # Tests the insertion, update and deletion of cross references for CV terms

        # Check that an ontology term without cross references will not create a cvterm_dbxref entry
        term = pronto.Term("defaultdb:defaultaccession")
        first_crossrefs = self.client._handle_cross_references(term, self.default_cvterm)
        self.assertEqual(len(first_crossrefs), 0)

        # Insert a further cross reference to the same database
        term.other = {"alt_id": ["defaultdb:furtheraccession"]}
        second_crossrefs = self.client._handle_cross_references(term, self.default_cvterm)
        self.assertEqual(len(second_crossrefs), 1)
        corresponding_dbxref = self.client.find_or_insert(general.DbxRef, db_id=self.default_db.db_id,
                                                          accession="furtheraccession")
        self.assertEqual(corresponding_dbxref.dbxref_id, second_crossrefs[0].dbxref_id)

        # Insert a cross reference to another database
        term.other = {"xref": ["otherdb:otheraccession 'with comment'"]}
        third_crossrefs = self.client._handle_cross_references(term, self.default_cvterm)
        self.assertEqual(len(third_crossrefs), 1)
        other_db = self.client.query_table(general.Db, name="otherdb").first()
        self.assertIsNotNone(other_db)
        corresponding_dbxref = self.client.find_or_insert(general.DbxRef, db_id=other_db.db_id,
                                                          accession="otheraccession")
        self.assertEqual(corresponding_dbxref.dbxref_id, third_crossrefs[0].dbxref_id)
Пример #6
0
    def test_handle_synonyms(self):
        # Tests the insertion, update and deletion of synonyms of CV terms

        # Check that an ontology term without synonym will not create a cvtermsynonym entry
        term = pronto.Term("defaultdb:defaultaccession")
        first_synonyms = self.client._handle_synonyms(term, self.default_cvterm)
        self.assertEqual(len(first_synonyms), 0)

        # Insert a synonym
        term.synonyms = {pronto.Synonym("another_name", "EXACT")}
        second_synonyms = self.client._handle_synonyms(term, self.default_cvterm)
        self.assertEqual(len(second_synonyms), 1)
        self.assertIsNotNone(second_synonyms[0].cvtermsynonym_id)
        self.assertEqual(second_synonyms[0].synonym, "another_name")
        self.assertEqual(second_synonyms[0].type_id, self.client._synonym_type_terms["exact"].cvterm_id)

        # Try to insert an existing synonym with changed scope, and check that this updates the existing entry
        term.synonyms = {pronto.Synonym("another_name", "NARROW")}
        third_synonyms = self.client._handle_synonyms(term, self.default_cvterm)
        self.assertEqual(len(third_synonyms), 1)
        self.assertEqual(third_synonyms[0].cvtermsynonym_id, second_synonyms[0].cvtermsynonym_id)
        self.assertEqual(third_synonyms[0].type_id, self.client._synonym_type_terms["narrow"].cvterm_id)

        # Insert another synonym
        term.synonyms.add(pronto.Synonym("yet_another_name", "BROAD"))
        fourth_synonyms = self.client._handle_synonyms(term, self.default_cvterm)
        self.assertEqual(len(fourth_synonyms), 2)
Пример #7
0
    def _handle_typedef(self, typedef: pronto.Relationship,
                        default_db: general.Db, default_cv: cv.Cv):
        """Inserts CV terms for relationship ontology terms (so-called "typedefs")"""

        # Check if a relationship CV term with this name already exists
        cvterm_entry = self.query_table(cv.CvTerm,
                                        name=typedef.obo_name,
                                        is_relationshiptype=1).first()
        if not cvterm_entry:

            # Create dbxref
            dbxref = create_dbxref(default_db.name, typedef.obo_name)
            term = pronto.Term(dbxref, typedef.obo_name)
            dbxref_entry = self._handle_dbxref(term, default_db)

            # Create CV term
            cvterm_entry = cv.CvTerm(cv_id=default_cv.cv_id,
                                     dbxref_id=dbxref_entry.dbxref_id,
                                     name=typedef.obo_name,
                                     is_relationshiptype=1)
            self.add_and_flush(cvterm_entry)
            self._cvterm_inserts += 1
            self.printer.print("Inserted CV term '" + cvterm_entry.name +
                               "' for dbxref " + dbxref)
        return cvterm_entry
Пример #8
0
 def test_extract_comments(self):
     # Tests the extraction of comments from an ontology term
     term = pronto.Term("testid")
     comment = ontology.extract_comment(term)
     self.assertEqual(comment, "")
     term.other = {"comment": ["testcomment"]}
     comment = ontology.extract_comment(term)
     self.assertEqual(comment, "testcomment")
Пример #9
0
 def test_extract_synonyms(self):
     # Tests the extraction of synonyms from an ontology term
     term = pronto.Term("testid")
     synonyms = ontology.extract_synonyms(term)
     self.assertEqual(len(synonyms), 0)
     term.synonyms = {pronto.Synonym("first_synonym"), pronto.Synonym("second_synonym")}
     synonyms = ontology.extract_synonyms(term)
     self.assertEqual(len(synonyms), 2)
     self.assertIn("second_synonym", synonyms)
Пример #10
0
    def _handle_cross_references(
            self, term: pronto.Term,
            cvterm_entry: cv.CvTerm) -> List[cv.CvTermDbxRef]:
        """Inserts, updates or deletes database cross references to a CV term in the cvterm_dbxref table"""

        # Get existing cross references, i.e. cross references present in database
        existing_crossrefs = self.query_table(
            cv.CvTermDbxRef, cvterm_id=cvterm_entry.cvterm_id).all()
        edited_entries = []

        # Get cross references present in the file and loop over them
        new_crossrefs = extract_cross_references(term)
        for crossref in new_crossrefs:

            # Obtain the corresponding entry from the dbxref table, insert if not existing
            db_authority = split_dbxref(crossref)[0]
            corresponding_db_entry = self._handle_db(db_authority)
            corresponding_dbxref_entry = self._handle_dbxref(
                pronto.Term(crossref), corresponding_db_entry)

            # Check if the cross reference is already present in the database
            matching_crossrefs = utils.filter_objects(
                existing_crossrefs,
                dbxref_id=corresponding_dbxref_entry.dbxref_id
            )  # type: List[cv.CvTermDbxRef]
            if matching_crossrefs:

                # Save the existing cross reference
                edited_entries.append(matching_crossrefs[0])
            else:

                # Insert cross reference
                cvterm_dbxref_entry = cv.CvTermDbxRef(
                    cvterm_id=cvterm_entry.cvterm_id,
                    dbxref_id=corresponding_dbxref_entry.dbxref_id,
                    is_for_definition=0)
                self.add_and_flush(cvterm_dbxref_entry)
                self.printer.print("Inserted cross reference '" + crossref +
                                   "' for CV term '" + cvterm_entry.name + "'")
                self._crossref_inserts += 1
                edited_entries.append(cvterm_dbxref_entry)

        # Delete cross references not present in file
        for cvterm_dbxref_entry in existing_crossrefs:  # type: cv.CvTermDbxRef
            matching_crossrefs = utils.filter_objects(
                edited_entries, dbxref_id=cvterm_dbxref_entry.dbxref_id)
            if not matching_crossrefs:

                # Delete cross reference
                self.session.delete(cvterm_dbxref_entry)
                self.printer.print("Deleted cross reference with dbxref-ID " +
                                   str(cvterm_dbxref_entry.dbxref_id) +
                                   " for CV term '" + cvterm_entry.name + "'")
                self._crossref_deletes += 1

        return edited_entries
Пример #11
0
 def test_extract_cross_references(self):
     # Tests the extraction of cross references from an ontology term
     term = pronto.Term("testid")
     crossrefs = ontology.extract_cross_references(term)
     self.assertEqual(len(crossrefs), 0)
     term.other = {"alt_id": ["alternative_id"], "xref": ["first_ref", "second_ref"]}
     crossrefs = ontology.extract_cross_references(term)
     self.assertEqual(len(crossrefs), 3)
     self.assertIn("alternative_id", crossrefs)
     self.assertIn("second_ref", crossrefs)
Пример #12
0
    def load(self, filename: str, file_format: str, db_authority: str):
        """Loads CV terms from a file into a database"""

        # Parse the file
        self.printer.print("Parsing ontology file ...")
        ontology = parse_ontology(filename,
                                  file_format)  # type: pronto.Ontology

        # Filter content: Only retain the terms stemming from the database authority of interest
        default_namespace = get_default_namespace(ontology)
        ontology_terms = filter_ontology_by_db(
            ontology, db_authority)  # type: Dict[str, pronto.Term]
        self.printer.print("Retrieved " + str(len(ontology_terms)) +
                           " terms for database authority " + db_authority)

        # Find/create parent vocabulary and db of ontology terms in file; load dependencies
        default_db_entry = self._handle_db(db_authority)
        default_cv_entry = self._handle_cv(pronto.Term(""), default_namespace)

        # Create global containers to reduce the number of database requests
        all_cvterm_entries = {}  # type: Dict[str, cv.CvTerm]

        # Handle typedefs
        for typedef in ontology.typedefs:  # type: pronto.Relationship
            self._handle_typedef(typedef, default_db_entry, default_cv_entry)

        # First loop over all terms retrieved from the file: handle vocabulary, dbxref, CV terms, synonyms, comments
        for term in ontology_terms.values():  # type: pronto.Term

            # Insert, update and/or delete entries in various tables
            cv_entry = self._handle_cv(term, default_namespace)
            dbxref_entry = self._handle_dbxref(term, default_db_entry)
            cvterm_entry = self._handle_cvterms(ontology_terms,
                                                default_db_entry, dbxref_entry,
                                                cv_entry.cv_id)
            self._handle_comments(term, cvterm_entry)
            self._handle_synonyms(term, cvterm_entry)

            # Save CV term in global container
            all_cvterm_entries[term.id] = cvterm_entry

        # Second loop over all terms retrieved from file: cross references and relationships
        for term in ontology_terms.values():  # type: pronto.Term

            # Insert, update and/or delete entries in various tables
            self._handle_cross_references(term, all_cvterm_entries[term.id])
            self._handle_relationships(term, all_cvterm_entries[term.id],
                                       all_cvterm_entries)

        # Mark obsolete CV terms
        self._mark_obsolete_terms(ontology_terms, default_db_entry)

        # Commit changes
        self.session.commit()
        self._print_statistics()
Пример #13
0
    def test_handle_comments(self):
        # Tests the insertion, update and deletion of comments (cvtermprop entries)

        # Check that an ontology term without comment will not create a cvtermprop entry
        term = pronto.Term("defaultdb:defaultaccession")
        first_cvtermprop = self.client._handle_comments(term, self.default_cvterm)
        self.assertIsNone(first_cvtermprop)

        # Insert a comment
        term.other = {"comment": ["testcomment"]}
        second_cvtermprop = self.client._handle_comments(term, self.default_cvterm)
        self.assertIsNotNone(second_cvtermprop.cvtermprop_id)
        self.assertEqual(second_cvtermprop.cvterm_id, self.default_cvterm.cvterm_id)
        self.assertEqual(second_cvtermprop.type_id, self.client._comment_term.cvterm_id)
        self.assertEqual(second_cvtermprop.value, "testcomment")

        # Try to insert another comment and check that this updates the existing entry
        term.other = {"comment": ["othercomment"]}
        third_cvtermprop = self.client._handle_comments(term, self.default_cvterm)
        self.assertEqual(third_cvtermprop.cvtermprop_id, second_cvtermprop.cvtermprop_id)
        self.assertEqual(third_cvtermprop.value, "othercomment")
Пример #14
0
    def test_handle_cv(self):
        # Tests the insertion of CV entries

        # Insert a new entry
        term = pronto.Term("testdb:testaccession")
        first_cv = self.client._handle_cv(term, "testnamespace")
        self.assertEqual(first_cv.name, "testnamespace")
        self.assertIsNotNone(first_cv.cv_id)

        # Try to insert an entry without namespace, and fail
        with self.assertRaises(iobase.InputFileError):
            self.client._handle_cv(term, "")

        # Insert a further entry with another namespace
        term.other = {"namespace": ["othernamespace"]}
        second_cv = self.client._handle_cv(term, "")
        self.assertEqual(second_cv.name, "othernamespace")
        self.assertNotEqual(first_cv.cv_id, second_cv.cv_id)

        # Try to insert an entry with the same namespace, and check that the function returns the existing entry
        third_cv = self.client._handle_cv(term, "")
        self.assertEqual(third_cv.cv_id, second_cv.cv_id)
Пример #15
0
    def test_handle_dbxref(self):
        # Tests the insertion and update of dbxref entries

        # Insert a new entry
        term = pronto.Term("defaultdb:testaccession")
        first_dbxref = self.client._handle_dbxref(term, self.default_db)
        self.assertEqual(first_dbxref.accession, "testaccession")
        self.assertEqual(first_dbxref.version, "")
        self.assertIsNotNone(first_dbxref.dbxref_id)

        # Insert a further entry with another accession
        term.id = "defaultdb:otheraccession"
        second_dbxref = self.client._handle_dbxref(term, self.default_db)
        self.assertEqual(second_dbxref.accession, "otheraccession")
        self.assertNotEqual(first_dbxref.dbxref_id, second_dbxref.dbxref_id)

        # Try to insert an entry with the same accession, but other version, and check that the function returns
        # the existing entry with updated version
        term.id = "defaultdb:testaccession:testversion"
        third_dbxref = self.client._handle_dbxref(term, self.default_db)
        self.assertEqual(third_dbxref.accession, "testaccession")
        self.assertEqual(third_dbxref.version, "testversion")
        self.assertEqual(first_dbxref.dbxref_id, third_dbxref.dbxref_id)
def metric_143_ncbitaxon(doc):
    taxonomies = list(
        map(
            json.loads,
            set(
                json.dumps({
                    'value':
                    isAbout['name'],
                    'valueIRI':
                    isAbout.get('identifier', {}).get('identifierSource', '') +
                    isAbout.get('identifier', {}).get('identifier', '')
                }) for node in jsonld_frame(
                    doc, {
                        '@type': 'Dataset',
                        'isAbout': {
                            '@type': 'TaxonomicInformation',
                            'name': {},
                            'identifier': {
                                'identifier': {},
                                'identifierSource': {
                                    '@default': ''
                                },
                            }
                        }
                    })['@graph'] if node['isAbout']
                for isAbout in force_list(node['isAbout'])
                if isAbout['name'])))
    if taxonomies:
        for taxonomy in taxonomies:
            value_ns = IRI_to_NS(taxonomy.get('valueIRI'))
            if taxonomy.get(
                    'value') and taxonomy.get('valueIRI') and pronto.Term(
                        value_ns, taxonomy['value']) in NCBITaxon:
                yield {
                    'value':
                    1,
                    'comment':
                    'Ontological IRI for taxonomy {} and term match what is found in NCBITaxon.'
                    .format(value_ns),
                }
            elif taxonomy.get('valueIRI') and value_ns in NCBITaxon:
                yield {
                    'value':
                    0.75,
                    'comment':
                    'Ontological IRI for taxonomy {} found in NCBITaxon.'.
                    format(value_ns),
                }
            elif taxonomy.get(
                    'value') and taxonomy['value'] in NCBITaxon_reversed:
                yield {
                    'value':
                    0.75,
                    'comment':
                    'Taxonomy `{}` found in NCBITaxon.'.format(
                        taxonomy['value']),
                }
            elif taxonomy.get('value') and taxonomy[
                    'value'] in NCBITaxon_reversed_synonyms:
                yield {
                    'value':
                    0.5,
                    'comment':
                    'Taxonomy `{}` found in NCBITaxon synonyms.'.format(
                        taxonomy['value']),
                }
            else:
                yield {
                    'value':
                    0.25,
                    'comment':
                    'Taxonomy `{}` found but not in NCBITaxon.'.format(
                        taxonomy.get('value', '') +
                        (('<' + value_ns + '>') if value_ns else '')),
                }
    else:
        yield {
            'value': 0.0,
            'comment': 'Taxonomy could not be identified',
        }
Пример #17
0
def metric_142_edam(doc):
  filetypes = list(map(json.loads,set(
    json.dumps({
      'value': information['value'],
      'valueIRI': information['valueIRI'],
    })
    for node in jsonld_frame(doc, {
      '@type': 'Dataset',
      'types': {
        'information': {
          'value': { '@default': '' },
          'valueIRI': { '@default': '' }
        }
      }
    })['@graph']
    if node['types']
    for types in force_list(node['types'])
    if types['information']
    for information in force_list(types['information'])
    if information['value'] and information['valueIRI']
  )))
  if filetypes:
    for filetype in filetypes:
      value_ns = IRI_to_NS(filetype.get('valueIRI'))
      if filetype.get('value') and filetype.get('valueIRI') and pronto.Term(value_ns, filetype['value']) in EDAM:
        yield {
          'value': 1,
          'comment': 'Ontological IRI for file type {} and term match what is found in EDAM.'.format(
            value_ns
          ),
        }
      elif filetype.get('valueIRI') and value_ns in EDAM:
        yield {
          'value': 0.75,
          'comment': 'Ontological IRI for filetype {} found in EDAM.'.format(
            value_ns
          ),
        }
      elif filetype.get('value') and filetype['value'] in EDAM_reversed:
        yield {
          'value': 0.75,
          'comment': 'Filetype `{}` found in EDAM.'.format(
            filetype['value']
          ),
        }
      elif filetype.get('value') and filetype['value'] in EDAM_reversed_synonyms:
        yield {
          'value': 0.5,
          'comment': 'Filetype `{}` found in EDAM synonyms.'.format(
            filetype['value']
          ),
        }
      else:
        yield {
          'value': 0.25,
          'comment': 'Filetype `{}` found but not in EDAM.'.format(
            filetype.get('value', '') + (('<' + value_ns + '>') if value_ns else '')
          ),
        }
  else:
    yield {
      'value': 0.0,
      'comment': 'filetype could not be identified',
    }
Пример #18
0
def metric_140_uberon(doc):
    anatomical_parts = list(
        map(
            json.loads,
            set(
                json.dumps({
                    'value':
                    isAbout['name'],
                    'valueIRI':
                    isAbout.get('identifier', {}).get('identifierSource', '') +
                    isAbout.get('identifier', {}).get('identifier', '')
                }) for node in jsonld_frame(
                    doc, {
                        '@type': 'Dataset',
                        'isAbout': {
                            '@type': 'AnatomicalPart',
                            'name': {},
                            'identifier': {
                                'identifier': {},
                                'identifierSource': {
                                    '@default': ''
                                },
                            }
                        }
                    })['@graph'] if node['isAbout']
                for isAbout in force_list(node['isAbout'])
                if isAbout['name'])))
    if anatomical_parts:
        for anatomical_part in anatomical_parts:
            value_ns = IRI_to_NS(anatomical_part.get('valueIRI'))
            if anatomical_part.get('value') and anatomical_part.get(
                    'valueIRI') and pronto.Term(
                        value_ns, anatomical_part['value']) in UBERON:
                yield {
                    'value':
                    1,
                    'comment':
                    'Ontological IRI for anatomical part {} and term match what is found in UBERON.'
                    .format(value_ns),
                }
            elif anatomical_part.get('valueIRI') and value_ns in UBERON:
                yield {
                    'value':
                    0.75,
                    'comment':
                    'Ontological IRI for anatomical part {} found in UBERON.'.
                    format(value_ns),
                }
            elif anatomical_part.get(
                    'value') and anatomical_part['value'] in UBERON_reversed:
                yield {
                    'value':
                    0.75,
                    'comment':
                    'Anatomical part `{}` found in UBERON.'.format(
                        anatomical_part['value']),
                }
            elif anatomical_part.get('value') and anatomical_part[
                    'value'] in UBERON_reversed_synonyms:
                yield {
                    'value':
                    0.5,
                    'comment':
                    'Anatomical part `{}` found in UBERON synonyms.'.format(
                        anatomical_part['value']),
                }
            else:
                yield {
                    'value':
                    0.25,
                    'comment':
                    'Anatomical part `{}` found but not in UBERON.'.format(
                        anatomical_part.get('value', '') +
                        (('<' + value_ns + '>') if value_ns else '')),
                }
    else:
        yield {
            'value': 0.0,
            'comment': 'Anatomical part could not be identified',
        }
def metric_141_mondo(doc):
    diseases = list(
        map(
            json.loads,
            set(
                json.dumps({
                    'value':
                    isAbout['name'],
                    'valueIRI': (
                        isAbout['identifier'].get('identifierSource', '') +
                        isAbout['identifier'].get('identifier', '')
                    ) if isAbout['identifier'] else ''
                }) for node in jsonld_frame(
                    doc, {
                        '@type': 'Dataset',
                        'isAbout': {
                            '@type': 'Disease',
                            'name': {},
                            'identifier': {
                                'identifier': {},
                                'identifierSource': {
                                    '@default': ''
                                },
                            }
                        }
                    })['@graph'] if node['isAbout']
                for isAbout in force_list(node['isAbout'])
                if isAbout['name'])))
    if diseases:
        for disease in diseases:
            value_ns = IRI_to_NS(disease.get('valueIRI'))
            if disease.get(
                    'value') and disease.get('valueIRI') and pronto.Term(
                        value_ns, disease['value']) in MONDO:
                yield {
                    'value':
                    1,
                    'comment':
                    'Ontological IRI for disease {} and term match what is found in MONDO.'
                    .format(value_ns),
                }
            elif disease.get('valueIRI') and disease['valueIRI'] in MONDO:
                yield {
                    'value':
                    0.75,
                    'comment':
                    'Ontological IRI for disease {} found in MONDO.'.format(
                        value_ns),
                }
            elif disease.get('value') and disease['value'] in MONDO_reversed:
                yield {
                    'value':
                    0.75,
                    'comment':
                    'Disease `{}` found in MONDO.'.format(disease['value']),
                }
            elif disease.get(
                    'value') and disease['value'] in MONDO_reversed_synonyms:
                yield {
                    'value':
                    0.5,
                    'comment':
                    'Disease `{}` found in MONDO synonyms.'.format(
                        disease['value']),
                }
            else:
                yield {
                    'value':
                    0.5,
                    'comment':
                    'Disease `{}` found but not in MONDO.'.format(
                        disease.get('value', '') +
                        (('<' + value_ns + '>') if value_ns else '')),
                }
    else:
        yield {
            'value': 0.0,
            'comment': 'Disease could not be identified',
        }
Пример #20
0
    def test_handle_cvterm(self):
        # Tests the insertion and update of cvterm entries

        # Populate the database with essentials
        ontology_terms = {"defaultdb:001": pronto.Term("defaultdb:001", "term1"),
                          "defaultdb:002": pronto.Term("defaultdb:002", "term2")}

        # Call the function with a dbxref that is not part of the ontology, and check that the corresponding CV term
        # is marked as obsolete
        obsolete_cvterm = self.client._handle_cvterms(ontology_terms, self.default_db, self.default_dbxref,
                                                      self.default_cv.cv_id)
        self.assertEqual(obsolete_cvterm.is_obsolete, 1)
        self.assertEqual(self.client._cvterm_inserts, 0)
        self.assertEqual(self.client._cvterm_updates, 0)
        self.assertEqual(self.client._cvterm_deletes, 1)

        # Call the function with a dbxref that is part of the ontology, and check that a CV term is created
        novel_dbxref = general.DbxRef(db_id=self.default_db.db_id, accession="001")
        self.client.add_and_flush(novel_dbxref)
        novel_cvterm = self.client._handle_cvterms(ontology_terms, self.default_db, novel_dbxref, self.default_cv.cv_id)
        self.assertIsNotNone(novel_cvterm.cvterm_id)
        self.assertEqual(novel_cvterm.dbxref_id, novel_dbxref.dbxref_id)
        self.assertEqual(novel_cvterm.name, "term1")
        self.assertEqual(self.client._cvterm_inserts, 1)
        self.assertEqual(self.client._cvterm_updates, 0)
        self.assertEqual(self.client._cvterm_deletes, 1)

        # Call the function with changed ontology term, and check that this updates the existing entry
        ontology_terms["defaultdb:001"].name = "newname"
        updated_cvterm = self.client._handle_cvterms(ontology_terms, self.default_db, novel_dbxref,
                                                     self.default_cv.cv_id)
        self.assertEqual(novel_cvterm.cvterm_id, updated_cvterm.cvterm_id)
        self.assertEqual(updated_cvterm.name, "newname")
        self.assertEqual(self.client._cvterm_inserts, 1)
        self.assertEqual(self.client._cvterm_updates, 1)
        self.assertEqual(self.client._cvterm_deletes, 1)

        # Call the function with a dbxref that corresponds to an existing CV term, and check that nothing happens
        unaltered_cvterm = self.client._handle_cvterms(ontology_terms, self.default_db, novel_dbxref,
                                                       self.default_cv.cv_id)
        self.assertEqual(unaltered_cvterm, updated_cvterm)
        self.assertEqual(self.client._cvterm_inserts, 1)
        self.assertEqual(self.client._cvterm_updates, 1)
        self.assertEqual(self.client._cvterm_deletes, 1)

        # Call the function with an obsolete ontology term that has the same properties as an existing CV term, and
        # check that the new CV term is marked as 'more obsolete' before insertion
        ontology_terms["defaultdb:003"] = pronto.Term("defaultdb:003", "testterm", other={"is_obsolete": ["True"]})
        duplicate_obsolete_dbxref = general.DbxRef(db_id=self.default_db.db_id, accession="003")
        self.client.add_and_flush(duplicate_obsolete_dbxref)
        duplicate_obsolete_cvterm = self.client._handle_cvterms(ontology_terms, self.default_db,
                                                                duplicate_obsolete_dbxref, self.default_cv.cv_id)
        self.assertEqual(obsolete_cvterm.is_obsolete, 1)
        self.assertEqual(duplicate_obsolete_cvterm.is_obsolete, 2)
        self.assertEqual(self.client._cvterm_inserts, 2)
        self.assertEqual(self.client._cvterm_updates, 1)
        self.assertEqual(self.client._cvterm_deletes, 1)

        # Call the function with a non-obsolete ontology term that has the same properties as an existing CV term, and
        # check that the existing term is updated first
        ontology_terms["defaultdb:001"].name = "oldname"
        ontology_terms["defaultdb:004"] = pronto.Term("defaultdb:004", "newname")
        duplicate_dbxref = general.DbxRef(db_id=self.default_db.db_id, accession="004")
        self.client.add_and_flush(duplicate_dbxref)
        duplicate_cvterm = self.client._handle_cvterms(ontology_terms, self.default_db, duplicate_dbxref,
                                                       self.default_cv.cv_id)
        self.assertEqual(novel_cvterm.name, "oldname")
        self.assertEqual(duplicate_cvterm.name, "newname")
        self.assertEqual(self.client._cvterm_inserts, 3)
        self.assertEqual(self.client._cvterm_updates, 2)
        self.assertEqual(self.client._cvterm_deletes, 1)
def metric_139_bao(doc):
    assays = list(
        map(
            json.loads,
            set(
                json.dumps({
                    'value':
                    method if type(method) == str else method['value'],
                    'valueIRI':
                    '' if type(method) == str else method['valueIRI'],
                }) for node in jsonld_frame(
                    doc, {
                        '@type': 'Dataset',
                        'types': {
                            'method': {
                                'value': {
                                    '@default': ''
                                },
                                'valueIRI': {
                                    '@default': ''
                                }
                            }
                        }
                    })['@graph'] if node['types']
                for types in force_list(node['types']) if types['method']
                for method in force_list(types['method'])
                if type(method) == str or
                (type(method) == dict and method['value'] or method['valueIRI']
                 ))))
    if assays:
        for assay in assays:
            value_ns = IRI_to_NS(assay.get('valueIRI'))
            if assay.get('value') and assay.get('valueIRI') and pronto.Term(
                    value_ns, assay.get('value')) in BAO:
                yield {
                    'value':
                    1,
                    'comment':
                    'Ontological IRI for Assay {} and term match what is found in BAO.'
                    .format(assay['valueIRI']),
                }
            elif value_ns and assay['valueIRI'] in BAO:
                yield {
                    'value':
                    0.75,
                    'comment':
                    'Ontological IRI for Assay {} found in BAO.'.format(
                        assay['valueIRI']),
                }
            elif assay.get('value') and assay['value'] in BAO_reversed:
                yield {
                    'value': 0.75,
                    'comment': 'Assay {} found in BAO.'.format(assay['value']),
                }
            elif assay.get(
                    'value') and assay['value'] in BAO_reversed_synonyms:
                yield {
                    'value':
                    0.5,
                    'comment':
                    'Assay `{}` found in BAO synonyms.'.format(assay['value']),
                }
            else:
                yield {
                    'value':
                    0.25,
                    'comment':
                    'Assay {} found but not in BAO.'.format(
                        assay.get('value', '') +
                        (('<' + value_ns + '>') if value_ns else '')),
                }
    else:
        yield {
            'value': 0.0,
            'comment': 'Assay could not be identified',
        }