Example #1
0
 def test_type(self):
     ont = pronto.Ontology()
     t1 = pronto.SynonymType("declared", "a declared synonym type")
     ont.metadata.synonymtypedefs.add(t1)
     term = ont.create_term("TEST:001")
     syn = term.add_synonym("something", type=t1)
     self.assertEqual(syn.type, t1)
Example #2
0
 def test_type_setter_undeclared(self):
     ont = pronto.Ontology()
     ty1 = pronto.SynonymType("undeclared", "an undeclared synonym type")
     term = ont.create_term("TEST:001")
     syn1 = term.add_synonym("something")
     self.assertIsNone(syn1.type)
     with self.assertRaises(ValueError):
         syn1.type = ty1
Example #3
0
 def test_type_setter(self):
     ont = pronto.Ontology()
     ty1 = pronto.SynonymType("declared", "a declared synonym type")
     ont.metadata.synonymtypedefs.add(ty1)
     term = ont.create_term("TEST:001")
     syn1 = term.add_synonym("something")
     self.assertIsNone(syn1.type)
     syn1.type = ty1
     self.assertEqual(syn1.type, ty1)
Example #4
0
 def test_add_synonym_invalid_type(self):
     term = self.ms["MS:1000031"]
     st = pronto.SynonymType("undeclared", "an undeclared synonym type")
     with self.assertRaises(ValueError):
         s = term.add_synonym('instrument type', type=st)
Example #5
0
def export_ontos_to_obo(alphabets=None,
                        filename=None,
                        _max_monomers=None,
                        _max_xlinks=None):
    """ Exports alphabets of residues and ontology of crosslinks to OBO format

    Args:
        alphabets (:obj:`list` of :obj:`core.Alphabet`, optional): alphabets to export        
        filename (:obj:`str`, optional): path to export alphabets
        _max_monomers (:obj:`int`, optional): maximum number of monomers to export
        _max_xlinks (:obj:`int`, optional): maximum number of crosslinks to export
    """
    from .xlink.core import crosslinks_onto
    import pronto

    if alphabets is None:
        alphabets = [
            dna.dna_alphabet, rna.rna_alphabet, protein.protein_alphabet
        ]
    if filename is None:
        filename = pkg_resources.resource_filename(
            'bpforms', os.path.join('alphabet', 'onto.obo'))

    # create ontology
    onto = pronto.Ontology()

    onto.metadata.synonymtypedefs.add(
        pronto.SynonymType('structure', 'SMILES-encoded structure', 'EXACT'))
    onto.metadata.synonymtypedefs.add(
        pronto.SynonymType('backbone_bond_atoms', 'Backbone bond atoms',
                           'EXACT'))
    onto.metadata.synonymtypedefs.add(
        pronto.SynonymType('backbone_displaced_atoms',
                           'Backbone displaced atoms', 'EXACT'))
    onto.metadata.synonymtypedefs.add(
        pronto.SynonymType('l_bond_atoms', 'Left bond atoms', 'EXACT'))
    onto.metadata.synonymtypedefs.add(
        pronto.SynonymType('l_displaced_atoms', 'Left displaced atoms',
                           'EXACT'))
    onto.metadata.synonymtypedefs.add(
        pronto.SynonymType('r_bond_atoms', 'Right bond atoms', 'EXACT'))
    onto.metadata.synonymtypedefs.add(
        pronto.SynonymType('r_displaced_atoms', 'Right displaced atoms',
                           'EXACT'))
    onto.metadata.synonymtypedefs.add(
        pronto.SynonymType('delta_mass', 'Delta mass', 'EXACT'))
    onto.metadata.synonymtypedefs.add(
        pronto.SynonymType('delta_charge', 'Delta charge', 'EXACT'))
    onto.metadata.synonymtypedefs.add(
        pronto.SynonymType('bond_order', 'Bond order', 'EXACT'))
    onto.metadata.synonymtypedefs.add(
        pronto.SynonymType('bond_stereo', 'Bond stereochemistry', 'EXACT'))

    is_a_relationship = onto.get_relationship('is_a')
    part_of_relationship = onto.create_relationship('part_of')
    derives_from_relationship = onto.create_relationship('derives_from')
    l_monomer_relationship = onto.create_relationship('l_monomer')
    r_monomer_relationship = onto.create_relationship('r_monomer')

    # add terms for monomers to ontology
    alphabet_type_term = onto.create_term(id='BpForms:alphabet')
    alphabet_type_term.name = 'alphabet'

    monomer_type_term = onto.create_term(id='BpForms:monomer')
    monomer_type_term.name = 'monomeric form'
    monomer_type_term.add_synonym('residue', scope='BROAD')
    monomer_type_term.add_synonym('monomer', scope='BROAD')

    xlink_type_term = onto.create_term(id='BpForms:crosslink')
    xlink_type_term.name = 'crosslink'

    monomer_to_term = {}
    for alphabet in alphabets:
        alphabet_term = onto.create_term(id='BpForms:' + alphabet.id)
        alphabet_term.name = alphabet.name
        alphabet_term.desc = alphabet.description
        alphabet_term.relationships = {
            is_a_relationship: [alphabet_type_term],
        }

        for code, monomer in list(alphabet.monomers.items())[0:_max_monomers]:
            synonyms = []
            for syn in monomer.synonyms:
                synonyms.append(pronto.SynonymData(syn, scope='BROAD'))
            for identifier in monomer.identifiers:
                synonyms.append(
                    pronto.SynonymData('{}: {}'.format(identifier.ns,
                                                       identifier.id),
                                       scope='EXACT'))

            relationships = {
                is_a_relationship: [monomer_type_term],
                part_of_relationship: [alphabet_term],
            }

            other = {'structure': [monomer.export('smiles')]}
            for atom_type in [
                    'backbone_bond_atoms', 'backbone_displaced_atoms',
                    'l_bond_atoms', 'l_displaced_atoms', 'r_bond_atoms',
                    'r_displaced_atoms'
            ]:
                atoms = getattr(monomer, atom_type)
                if atoms:
                    other[atom_type] = [_atom_to_str(atom) for atom in atoms]
            if monomer.delta_mass:
                other['delta_mass'] = [monomer.delta_mass]
            if monomer.delta_charge:
                other['delta_charge'] = [monomer.delta_charge]

            term = onto.create_term(
                id='BpForms:{}_{}'.format(alphabet.id, code))
            term.name = monomer.name or None
            term.desc = monomer.comments or ''
            term.relationships = relationships
            term.other = other
            for syn in synonyms:
                term.add_synonym(syn.description, scope=syn.scope)

            monomer_to_term[monomer] = term

    for alphabet in alphabets:
        monomer_codes = {
            monomer: code
            for code, monomer in alphabet.monomers.items()
        }
        for code, monomer in list(alphabet.monomers.items())[0:_max_monomers]:
            monomer_term = onto['BpForms:{}_{}'.format(alphabet.id, code)]
            relationships = {}
            for base_monomer in monomer.base_monomers:
                base_monomer_term = onto.get(
                    'BpForms:{}_{}'.format(alphabet.id,
                                           monomer_codes[base_monomer]), None)
                if base_monomer_term is not None:
                    relationships[derives_from_relationship] = [
                        base_monomer_term
                    ]
            monomer_term.relationships = relationships

    # add terms for crosslinks to ontology
    for xlink in list(crosslinks_onto.values())[0:_max_xlinks]:
        relationships = {
            is_a_relationship: [xlink_type_term],
        }

        l_monomer_term = monomer_to_term.get(xlink.l_monomer, None)
        r_monomer_term = monomer_to_term.get(xlink.r_monomer, None)
        if l_monomer_term:
            relationships[l_monomer_relationship] = [l_monomer_term]
        if r_monomer_term:
            relationships[r_monomer_relationship] = [r_monomer_term]

        other = {}
        for atom_type in [
                'l_bond_atoms', 'r_bond_atoms', 'l_displaced_atoms',
                'r_displaced_atoms'
        ]:
            other[atom_type] = [
                _atom_to_str(atom) for atom in getattr(xlink, atom_type)
            ]

        other['bond_order'] = [xlink.order.name]
        if xlink.stereo:
            other['bond_stereo'] = [xlink.stereo.name]

        term = onto.create_term(id='BpForms:crosslink_{}'.format(xlink.id))
        term.name = xlink.name or None
        for syn in xlink.synonyms:
            term.add_synonym(syn, scope='BROAD')
        term.desc = xlink.comments or ''
        term.relationships = relationships
        term.other = other

    # export ontology
    with open(filename, 'wb') as file:
        onto.dump(file)
Example #6
0
 def test_type_undeclared(self):
     ont = pronto.Ontology()
     t1 = pronto.SynonymType("undeclared", "an undeclared synonym type")
     term = ont.create_term("TEST:001")
     with self.assertRaises(ValueError):
         term.add_synonym("something", type=t1)
Example #7
0
 def test_type(self):
     ont = pronto.Ontology()
     ty1 = pronto.SynonymType("declared", "a declared synonym type")
     ont.metadata.synonymtypedefs.add(ty1)
     term = ont.create_term("TEST:001")
     term.add_synonym("something", type=ty1)