def _parse_input_old_style(self, input_str):
        """Re-initialize the class with a new sequence using the old style

        Args:
            input_str (str): possible input formats include the following examples

        Examples:
            '+H2O2H2-OH',
            '+{0}'.format('H2O'),
            '{peptide}'.format(pepitde='ELVISLIVES'),
            '{peptide}+{0}'.format('PO3', peptide='ELVISLIVES'),
            '{peptide}#{unimod}:{pos}'.format(
                peptide = 'ELVISLIVES',
                unimod = 'Oxidation',
                pos = 1
            )
        """

        self.clear()
        # reset the shiznit
        if "#" in input_str:
            # Unimod Style format
            if self._unimod_parser is None:
                self._unimod_parser = unimod_mapper.UnimodMapper(
                    xml_file_list=self.unimod_files,
                    add_default_files=self.add_default_files,
                )
            self._parse_sequence_unimod_style(input_str)
        else:
            self._parse_sequence_piqdb_style(input_str)
Exemple #2
0
 def test_read_usermod_file_exclude_defaults(self):
     # the order of the files shouldn't change the unimodIDs
     for data in MULTIFILE_TESTS:
         um = unimod_mapper.UnimodMapper(xml_file_list=data["order"],
                                         add_default_files=False)
         assert len(um.data_list) == data["entries"]
         for case in data["excluded"]:
             assert case["out"] == um.name2id_list(case["in"])
Exemple #3
0
#!/usr/bin/env python
# encoding: utf-8
import os
import sys

import pytest

# this block is not needed anymore, when we have a proper package
sys.path.append(
    os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)))
# EOBlock
import unimod_mapper

M = unimod_mapper.UnimodMapper()

CONVERSIONS = [
    {
        "function": M.name2mass,
        "cases": [{
            "in": {
                "args": ["ICAT-G:2H(8)"]
            },
            "out": 494.30142
        }],
    },
    {
        "function":
        M.name2composition,
        "cases": [{
            "in": {
                "args": ["ICAT-G:2H(8)"]
    def add_modifications(self, modifications):
        """Adds modifications to the instance.

        Args:
            modifications (str): modifications given as unimod names with positional information,
                separated by semicolons, i.e: "unimod1:pos;unimod2:pos"
        """
        if self._unimod_parser is None:
            self._unimod_parser = unimod_mapper.UnimodMapper(
                xml_file_list=self.unimod_files,
                add_default_files=self.add_default_files,
            )
        modification_list = []
        if self.modifications is not None:
            modification_list = self.modifications.split(";")
        modification_list.extend(modifications.split(";"))
        self.modifications = ";".join(modification_list)

        pattern = re.compile(r""":(?P<pos>[0-9]*$)""")
        for unimod in modifications.split(";"):
            if unimod == "":
                continue
            unimod = unimod.strip()
            if ":" not in unimod:
                raise Exception("""
                    Error in chemical_composition.py:
                    This unimod: {0} requires positional information
                    """.format(unimod))

            for occ, match in enumerate(pattern.finditer(unimod)):
                unimodcomposition = self._unimod_parser.name_to_composition(
                    unimod[:match.start()])
                if len(unimodcomposition) == 0:
                    raise Exception("""
                        Error in chemical_composition.py:
                        Cannot map unimod {0}
                        """.format(unimod[:match.start()]))
                unimodcomposition = unimodcomposition[0]
                if occ >= 1:
                    raise Exception("""
                        Error in chemical_composition.py:
                        The unimod {0} contains multiple ":", preventing to map the position correctly
                        """.format(unimod))
                position = int(match.group("pos"))
                if position not in self.unimod_at_pos.keys():
                    self.unimod_at_pos[position] = []
                self.unimod_at_pos[position].append(unimod[:match.start()])

            for k, v in unimodcomposition.items():
                self[k] += v
            # Storing position related modifications
            position = int(match.group("pos"))
            if position == 0:
                # E.g. Acetylation at pos 0 indicates N-Term
                # but has to be counted for position 1 in this class
                position = 1

            if position not in self.composition_of_mod_at_pos.keys():
                self.composition_of_mod_at_pos[position] = ddict(int)
            if position not in self.composition_at_pos.keys():
                self.composition_at_pos[position] = ddict(int)
            for k, v in unimodcomposition.items():
                self.composition_of_mod_at_pos[position][k] += v
                self.composition_at_pos[position][k] += v
        return
Exemple #5
0
 def test_unimod_files_is_none(self):
     um = unimod_mapper.UnimodMapper(xml_file_list=None)
     names = [x.name for x in um.unimod_xml_names]
     assert names == ["usermod.xml", "unimod.xml"]
Exemple #6
0
 def test_read_multiple_unimod_files(self):
     # the order of the files shouldn't change the unimodIDs
     for data in MULTIFILE_TESTS:
         um = unimod_mapper.UnimodMapper(xml_file_list=data["order"])
         for case in data["included"]:
             assert case["out"] == um.name2id_list(case["in"])
Exemple #7
0
# this block is not needed anymore, when we have a proper package
# test_dir = Path(__file__).parent
# package_dir = test_dir.parent
# sys.path.append(package_dir)
# EOBlock
import unimod_mapper

package_dir = Path(unimod_mapper.__file__).parent
test_dir = Path(__file__).parent

# test_dir = Path(unimod_mapper.__file__).parent.parent / "tests"
# package_dir = test_dir.parent

unimod_path = package_dir.joinpath("unimod.xml")
usermod_path = test_dir.joinpath("usermod.xml")
M = unimod_mapper.UnimodMapper(
    xml_file_list=[unimod_path, usermod_path, unimod_path])

CONVERSIONS = [
    {
        "function":
        M.name2mass_list,
        "cases": [{
            "in": {
                "args": ["ICAT-G:2H(8)"]
            },
            "out": [494.30142, 494.30142]
        }],
    },
    {
        "function": M.name2first_mass,
        "cases": [{