def __init__(self):
     '''Constructor.'''
     self.__nodes = {}
     self.__reac_ids = {}
     self.__reac_enz_rels = []
     self.__org_enz_rels = []
     self.__enz_man = EnzymeManager()
class TestEnzymeManager(unittest.TestCase):
    '''Test class for EnzymeManager.'''
    def setUp(self):
        unittest.TestCase.setUp(self)
        self.__manager = EnzymeManager()

    def test_add_uniprot_data(self):
        '''Tests add_uniprot_data method.'''
        enzyme_ids = ['P19367', 'Q2KNB7']

        # Test unthreaded:
        self.__manager.add_uniprot_data(enzyme_ids, source='test')
        self.assertEqual(len(enzyme_ids), len(self.__manager.get_nodes()))

        # Test threaded:
        self.__manager.add_uniprot_data(enzyme_ids,
                                        source='test',
                                        num_threads=24)
        self.assertEqual(len(enzyme_ids), len(self.__manager.get_nodes()))
 def setUp(self):
     unittest.TestCase.setUp(self)
     self.__manager = EnzymeManager()
class ReactionManager(object):
    '''Class to implement a manager of Reaction data.'''

    def __init__(self):
        '''Constructor.'''
        self.__nodes = {}
        self.__reac_ids = {}
        self.__reac_enz_rels = []
        self.__org_enz_rels = []
        self.__enz_man = EnzymeManager()

    def get_files(self):
        '''Gets neo4j import files.'''
        return ([utils.write_nodes(self.__nodes.values(), 'Reaction'),
                 utils.write_nodes(self.__enz_man.get_nodes(), 'Enzyme')],
                [utils.write_rels(self.__reac_enz_rels, 'Reaction', 'Enzyme'),
                 utils.write_rels(self.__enz_man.get_org_enz_rels(),
                                  'Organism', 'Enzyme')])

    def add_reaction(self, source, reac_id, properties):
        '''Adds a reaction to the collection of nodes, ensuring uniqueness.'''
        reac_id = self.__reac_ids[source + reac_id] \
            if source + reac_id in self.__reac_ids else reac_id

        if reac_id not in self.__nodes:
            properties[':LABEL'] = 'Reaction'
            properties['id:ID(Reaction)'] = reac_id
            properties['source'] = source
            properties[source] = reac_id
            self.__nodes[reac_id] = properties

            if 'mnx' in properties:
                self.__reac_ids['mnx' + properties['mnx']] = reac_id

            if 'kegg.reaction' in properties:
                self.__reac_ids[
                    'kegg.reaction' + properties['kegg.reaction']] = reac_id

            if 'rhea' in properties:
                self.__reac_ids['rhea' + properties['rhea']] = reac_id
        else:
            self.__nodes[reac_id].update(properties)

        return reac_id

    def add_react_to_enz(self, data, source):
        '''Submit data to the graph.'''
        # Create Reaction and Enzyme nodes:
        enzyme_ids = self.__create_react_enz(data, source)

        # Create Enzyme nodes:
        self.__enz_man.add_uniprot_data(enzyme_ids, source)

    def __create_react_enz(self, data, source):
        '''Creates Reaction and Enzyme nodes and their Relationships.'''
        enzyme_ids = []

        for reac_id, uniprot_ids in data.iteritems():
            reac_id = self.add_reaction(source, reac_id, {})

            for uniprot_id in uniprot_ids:
                enzyme_ids.append(uniprot_id)
                self.__reac_enz_rels.append([reac_id, 'catalysed_by',
                                             uniprot_id,
                                             {'source': source}])

        return list(set(enzyme_ids))