Example #1
0
 def test_write_dictionary_one_to_set_empty_path_works(self):
     file_name = TestCase.id(self) + '_friends.txt'
     self.assertFalse(os.path.exists(file_name),
                      msg='The file should not exist as precondition')
     write_dictionary_one_to_set(self.friends, '', file_name)
     self.assertTrue(os.path.exists(file_name),
                     msg='The file was not created')
     os.remove(file_name)
Example #2
0
 def test_write_dictionary_one_to_set_non_existent_path_creates_file(self):
     file_name = TestCase.id(self) + '_friends.txt'
     file_path = 'a/path/'
     self.assertFalse(os.path.exists(file_path + file_name),
                      msg='The file should not exist as precondition')
     write_dictionary_one_to_set(self.friends, file_path, file_name)
     self.assertTrue(os.path.exists(file_path + file_name),
                     msg='The file was not created')
     os.remove(file_path + file_name)
     os.rmdir('a/path/')
     os.rmdir('a/')
def get_ppis(examples=10,
             path_swissprot=config.PATH_SWISSPROT,
             file_swissprot_proteins=config.FILE_SWISSPROT_PROTEINS,
             url_swissprot=config.URL_SWISSPROT_PROTEINS,
             path_reactome=config.PATH_REACTOME,
             file_reactome_internal_edges=config.REACTOME_INTERACTIONS,
             file_reactome_ppis=config.REACTOME_PPIS,
             path_pathwaymatcher=config.PATH_TOOLS,
             file_pathwaymatcher=config.FILE_PATHWAYMATCHER,
             url_pathwaymatcher=config.URL_PATHWAYMATCHER):
    """Returns dictionary of lexicographical interactions: accessions --> accessions set"""
    ppis = {}
    if not os.path.exists(path_reactome + file_reactome_ppis):
        create_pathwaymatcher_files(path_swissprot, file_swissprot_proteins,
                                    url_swissprot, path_reactome,
                                    file_reactome_internal_edges,
                                    path_pathwaymatcher, file_pathwaymatcher,
                                    url_pathwaymatcher)

        print("Reading Reactome interactions...")
        ppis = dictionaries.read_dictionary_one_to_set(
            path_reactome,
            file_reactome_internal_edges,
            order_pairs=True,
            col_indices=(0, 1),
            ignore_header=True)
        dictionaries.write_dictionary_one_to_set(ppis, path_reactome,
                                                 file_reactome_ppis)
    else:
        print("Reading Reactome unique interactions...")
        ppis = dictionaries.read_dictionary_one_to_set(path_reactome,
                                                       file_reactome_ppis)

    ppi_subset = {}
    example = 0

    if examples > 8000:
        for key, values in ppis.items():
            for value in values:
                ppi_subset.setdefault(key.strip(), set()).add(value.strip())
                example += 1
                if example >= examples:
                    break
            if example >= examples:
                break
    else:
        random.seed(77)
        keys = random.sample(list(ppis.keys()), int(examples))
        for key in keys:
            ppi_subset.setdefault(key.strip(), set()).add(
                random.sample(ppis[key], 1)[0].strip())

    print("Reactome interactions READY")
    return ppi_subset
Example #4
0
    def test_write_dictionary_one_to_set_all_keys(self):
        file_name = TestCase.id(self) + '_friends.txt'
        self.assertFalse(os.path.exists(file_name),
                         msg='The file should not exist as precondition')
        write_dictionary_one_to_set(self.friends, '', file_name)

        with open(file_name) as file:
            lines = file.readlines()
            self.assertIn('David\tEster\n', lines, msg='Missing entry line')
            self.assertIn('Michael\tDavid\n', lines, msg='Missing entry line')
            self.assertIn('James\tMartha\n', lines, msg='Missing entry line')
        os.remove(file_name)
Example #5
0
    def test_write_dictionary_one_to_set_one_row_each_set_entry(self):
        """Having a key with multiple values, writes one row for each key -- value pair"""
        file_name = TestCase.id(self) + '_friends.txt'
        self.assertFalse(os.path.exists(file_name),
                         msg='The file should not exist as precondition')
        write_dictionary_one_to_set(self.friends, '', file_name)

        with open(file_name) as file:
            lines = file.readlines()
            self.assertEqual(6,
                             len(lines),
                             msg='Wrong number of lines in the file')
            self.assertIn('David\tEster\n', lines, msg='Missing entry line')
            self.assertIn('David\tMartha\n', lines, msg='Missing entry line')
            self.assertIn('David\tMathew\n', lines, msg='Missing entry line')
        os.remove(file_name)
Example #6
0
 def test_write_dictionary_one_to_set_empty_file_name_raises_error(self):
     with self.assertRaises(ValueError,
                            msg="Should not accept empty file name"):
         write_dictionary_one_to_set({}, '', '')