Exemple #1
0
 def test_import_network_source(self):
     """
     Tests if the IoConnection uses the source dict.
     :return:
     """
     conn_object = BiomConnection()
     conn_object.create_tables()
     conn_object.add_summary(('banana', 2, 5))
     nx.write_graphml(g, path="test.graphml")
     import_networks("test.graphml", sources={"test": "banana"})
     os.remove("test.graphml")
     conn = psycopg2.connect(
         **{
             "host": "localhost",
             "database": "test",
             "user": "******",
             "password": "******"
         })
     cur = conn.cursor()
     cur.execute("SELECT networkID " "FROM networks " "LIMIT 1;")
     result = cur.fetchall()
     cur.close()
     conn.close()
     conn_object.delete_tables()
     self.assertEqual(result[0][0], 'test')
Exemple #2
0
 def test_add_network(self):
     """
     Tests if the network file is added
     to the database.
     :return:
     """
     conn_object = BiomConnection()
     conn_object.create_tables()
     conn_object.add_biom(testbiom, 'banana')
     conn_object = IoConnection()
     conn_object.add_network(g, 'banana', 'banana')
     conn = psycopg2.connect(
         **{
             "host": "localhost",
             "database": "test",
             "user": "******",
             "password": "******"
         })
     cur = conn.cursor()
     cur.execute("SELECT * from edges;")
     result = cur.fetchall()
     # 3 edges in g
     cur.close()
     conn.close()
     conn_object.delete_tables()
     self.assertEqual(len(result), 3)
Exemple #3
0
 def test_import_networks(self):
     """
     Tests if the import_networks function reads the correct database file,
     and imports the network file, by querying the networks table.
     :return:
     """
     conn_object = BiomConnection()
     conn_object.create_tables()
     conn_object.add_summary(('test', 2, 5))
     nx.write_graphml(g, path="test.graphml")
     import_networks("test.graphml")
     os.remove("test.graphml")
     conn = psycopg2.connect(
         **{
             "host": "localhost",
             "database": "test",
             "user": "******",
             "password": "******"
         })
     cur = conn.cursor()
     cur.execute("SELECT studyID " "FROM networks " "LIMIT 1;")
     result = cur.fetchall()
     cur.close()
     conn.close()
     conn_object.delete_tables()
     self.assertEqual(result[0][0], 'test')
Exemple #4
0
 def test_import_network_edge(self):
     """
     Tests if the import_network function reads the correct database file,
     and imports the network file, by querying the edges table.
     :return:
     """
     conn_object = BiomConnection()
     conn_object.create_tables()
     conn_object.add_summary(('test', 2, 5))
     nx.write_graphml(g, path="test.graphml")
     import_networks("test.graphml")
     os.remove("test.graphml")
     conn = psycopg2.connect(
         **{
             "host": "localhost",
             "database": "test",
             "user": "******",
             "password": "******"
         })
     cur = conn.cursor()
     cur.execute("SELECT source, target " "FROM edges " "LIMIT 2;")
     result = cur.fetchall()
     cur.close()
     conn.close()
     conn_object.delete_tables()
     self.assertCountEqual(result, [("GG_OTU_1", "GG_OTU_2"),
                                    ("GG_OTU_2", "GG_OTU_5")])
Exemple #5
0
 def test_add_observations(self):
     """
     Tests whether multiple rows are added to the counts table.
     :return:
     """
     conn_object = BiomConnection()
     conn_object.create_tables()
     conn_object.add_summary(values=('banana', 1, 2))
     conn_object.add_sample(values=('Sample1', 'banana'))
     conn_object.add_taxon(values=[('Listeria', 'banana', 'Bacteria',
                                    'Firmicutes', 'Bacilli', 'Bacillales',
                                    'Listeriaceae', 'Listeria',
                                    'monocytogenes'),
                                   ('Listeria2', 'banana', 'Bacteria',
                                    'Firmicutes', 'Bacilli', 'Bacillales',
                                    'Listeriaceae', 'Listeria',
                                    'monocytogenes')])
     conn_object.add_observation(
         values=[('banana', 'Listeria', 'Sample1',
                  0.5), ('banana', 'Listeria2', 'Sample1', 0.75)])
     conn = psycopg2.connect(
         **{
             "host": "localhost",
             "database": "test",
             "user": "******",
             "password": "******"
         })
     cur = conn.cursor()
     cur.execute("SELECT count from counts;")
     result = cur.fetchall()
     self.assertEqual(len(result), 2)
     cur.close()
     conn.close()
     conn_object.delete_tables()
Exemple #6
0
 def test_add_meta(self):
     """
     Tests whether a row is added to the meta table.
     :return:
     """
     conn_object = BiomConnection()
     conn_object.create_tables()
     conn_object.add_summary(values=('banana', 1, 2))
     conn_object.add_sample(values=('Sample1', 'banana'))
     conn_object.add_meta(values=('Sample1', 'banana', 'colour', 'yellow',
                                  None))
     conn = psycopg2.connect(
         **{
             "host": "localhost",
             "database": "test",
             "user": "******",
             "password": "******"
         })
     cur = conn.cursor()
     cur.execute("SELECT property from meta;")
     result = cur.fetchall()
     # long format table of 5 * 6 observations
     self.assertEqual(result[0][0], 'colour')
     cur.close()
     conn.close()
     conn_object.delete_tables()
Exemple #7
0
 def test_add_taxon_missingvalues(self):
     """
     Tests whether a row is added to the taxonomy table,
     even when some assignments are unknown.
     :return:
     """
     conn_object = BiomConnection()
     conn_object.create_tables()
     conn_object.add_summary(values=('banana', 1, 2))
     conn_object.add_taxon(values=('Listeria', 'banana', 'Bacteria',
                                   'Firmicutes', 'Bacilli', None, None,
                                   None, None))
     conn = psycopg2.connect(
         **{
             "host": "localhost",
             "database": "test",
             "user": "******",
             "password": "******"
         })
     cur = conn.cursor()
     cur.execute("SELECT * from taxonomy;")
     result = cur.fetchall()
     # long format table of 5 * 6 observations
     self.assertEqual(result[0],
                      ('Listeria', 'banana', 'Bacteria', 'Firmicutes',
                       'Bacilli', None, None, None, None))
     cur.close()
     conn.close()
     conn_object.delete_tables()
Exemple #8
0
 def test_add_edges(self):
     """
     Tests whether new rows are added to the edges table.
     :return:
     """
     conn_object = BiomConnection()
     conn_object.create_tables()
     conn_object.add_biom(testbiom, 'banana')
     conn_object = IoConnection()
     conn_object.add_network_node(values=("banana", "banana", 2, 3))
     conn_object.add_edge(
         values=[("banana", "GG_OTU_1", "GG_OTU_2",
                  0.3), ("banana", "GG_OTU_3", "GG_OTU_5", -0.8)])
     conn = psycopg2.connect(
         **{
             "host": "localhost",
             "database": "test",
             "user": "******",
             "password": "******"
         })
     cur = conn.cursor()
     cur.execute("SELECT * from edges;")
     result = cur.fetchall()
     cur.close()
     conn.close()
     conn_object.delete_tables()
     self.assertEqual(len(result), 2)
Exemple #9
0
    def test_agglomerate_network_phylumloop(self):
        """
        Tests whether the network agglomeration also works up to Phylum level.
        Note: need to check selfloops.

        :return:
        """
        conn_object = BiomConnection()
        conn_object.create_tables()
        conn_object.add_biom(testbiom, 'test')
        conn_object = IoConnection()
        conn_object.add_network(network=g, name='g', study='test')
        conn_object = MetaConnection()
        conn_object.agglomerate_networks(level='Phylum',
                                         weight=False,
                                         networks=['g'])
        conn = psycopg2.connect(
            **{
                "host": "localhost",
                "database": "test",
                "user": "******",
                "password": "******"
            })
        cur = conn.cursor()
        cur.execute(
            "SELECT node_num "
            "FROM networks WHERE networks.networkid = %s"
            "LIMIT 1;",
            vars=('Phylum_g', ))
        result = cur.fetchall()
        cur.close()
        conn.close()
        conn_object.delete_tables()
        self.assertEqual(result[0][0], 3)
Exemple #10
0
 def test_import_biom(self):
     """
     Tests if the import_biom function reads the correct database file,
     and imports the biom file, by querying the bioms table.
     :return:
     """
     conn_object = BiomConnection()
     conn_object.create_tables()
     write_biom_table(testbiom, fmt='hdf5', filepath="test.biom")
     import_biom("test.biom", mapping=None)
     os.remove("test.biom")
     conn = psycopg2.connect(
         **{
             "host": "localhost",
             "database": "test",
             "user": "******",
             "password": "******"
         })
     cur = conn.cursor()
     cur.execute("SELECT studyID " "FROM bioms " "LIMIT 1;")
     result = cur.fetchall()
     cur.close()
     conn.close()
     conn_object.delete_tables()
     self.assertEqual(result[0][0], 'test')
Exemple #11
0
 def test_agglomerate_networks(self):
     """
     Tests if the agglomerate_networks function agglomerates the test file
     so that only 3 nodes and 3 edges remain.
     :return:
     """
     conn_object = BiomConnection()
     conn_object.create_tables()
     conn_object.add_biom(testbiom, 'test')
     conn_object = IoConnection()
     conn_object.add_network(network=g, name='g', study='test')
     conn_object = MetaConnection()
     conn_object.agglomerate_networks(level='Family',
                                      weight=False,
                                      networks=['g'])
     conn = psycopg2.connect(
         **{
             "host": "localhost",
             "database": "test",
             "user": "******",
             "password": "******"
         })
     cur = conn.cursor()
     cur.execute(
         "SELECT node_num "
         "FROM networks WHERE networks.networkid = %s"
         "LIMIT 1;",
         vars=('Family_g', ))
     result = cur.fetchall()
     cur.close()
     conn.close()
     conn_object.delete_tables()
     self.assertEqual(result[0][0], 3)
Exemple #12
0
 def test_copy_network(self):
     """
     Tests if the network file is added
     to the database.
     :return:
     """
     conn_object = BiomConnection()
     conn_object.create_tables()
     conn_object.add_biom(testbiom, 'test')
     conn_object = IoConnection()
     conn_object.add_network(network=g, name='g', study='test')
     conn_object = MetaConnection()
     conn_object.copy_network(source_network='g', new_network='f')
     conn = psycopg2.connect(
         **{
             "host": "localhost",
             "database": "test",
             "user": "******",
             "password": "******"
         })
     cur = conn.cursor()
     cur.execute(
         "SELECT networkID "
         "FROM networks WHERE networks.networkid = %s"
         "LIMIT 1;",
         vars=('f', ))
     result = cur.fetchall()
     cur.close()
     conn.close()
     conn_object.delete_tables()
     self.assertEqual(result[0][0], 'f')
Exemple #13
0
 def test_agglomerate_pair(self):
     """
     Tests whether two edges are merged into a single edge,
     so that OTU 1 and 3 are removed.
     :return:
     """
     conn_object = BiomConnection()
     conn_object.create_tables()
     conn_object.add_biom(testbiom, 'test')
     conn_object = IoConnection()
     conn_object.add_network(network=g, name='g', study='test')
     conn_object = MetaConnection()
     conn_object.agglomerate_pair(
         (["GG_OTU_1", "GG_OTU_2"], ["GG_OTU_3", "GG_OTU_5"], 1),
         network='g',
         level='Class')
     conn = psycopg2.connect(
         **{
             "host": "localhost",
             "database": "test",
             "user": "******",
             "password": "******"
         })
     cur = conn.cursor()
     cur.execute(
         "SELECT source, target "
         "FROM edges WHERE edges.networkid = %s;",
         vars=('g', ))
     result = cur.fetchall()
     cur.close()
     conn.close()
     conn_object.delete_tables()
     removed = {"GG_OTU_1", "GG_OTU_3"}
     self.assertEqual(len(removed.intersection(result)), 0)
Exemple #14
0
 def test_import_biom_obs(self):
     """
     Tests if the import_biom function reads the correct database file,
     and imports the biom file, by queriying the counts table.
     :return:
     """
     conn_object = BiomConnection()
     conn_object.create_tables()
     write_biom_table(testbiom, fmt='hdf5', filepath="test.biom")
     import_biom("test.biom", mapping=None)
     os.remove("test.biom")
     conn = psycopg2.connect(
         **{
             "host": "localhost",
             "database": "test",
             "user": "******",
             "password": "******"
         })
     cur = conn.cursor()
     cur.execute("SELECT sampleid " "FROM counts " "LIMIT 5;")
     result = cur.fetchall()
     cur.close()
     conn.close()
     conn_object.delete_tables()
     result = [x[0] for x in result]
     self.assertCountEqual(
         result, ['Sample1', 'Sample2', 'Sample3', 'Sample4', 'Sample5'])
Exemple #15
0
 def test_create_agglom(self):
     """
     Tests whether a new agglomerated node is created with intact taxonomy.
     :return:
     """
     conn_object = BiomConnection()
     conn_object.create_tables()
     conn_object.add_biom(testbiom, 'test')
     conn_object = IoConnection()
     conn_object.add_network(network=g, name='g', study='test')
     conn_object = MetaConnection()
     uid = conn_object.create_agglom(parent="GG_OTU_1", level="Family")
     conn = psycopg2.connect(
         **{
             "host": "localhost",
             "database": "test",
             "user": "******",
             "password": "******"
         })
     cur = conn.cursor()
     cur.execute(
         "SELECT Family "
         "FROM taxonomy WHERE taxonomy.taxon = %s;",
         vars=(uid, ))
     result = cur.fetchall()
     cur.close()
     conn.close()
     conn_object.delete_tables()
     self.assertEqual(result[0][0], 'f__Enterobacteriaceae')
Exemple #16
0
 def test_start_metastats(self):
     """
     Tests if the stat_networks function reads the correct database file,
     and agglomerates the network file, by querying the networks table.
     :return:
     """
     conn_object = BiomConnection()
     conn_object.create_tables()
     conn_object.add_biom(testbiom, 'test')
     conn_object = IoConnection()
     conn_object.add_network(network=g, name='g', study='test')
     start_metastats(level='Family', weight=False, networks=['g'])
     conn = psycopg2.connect(
         **{
             "host": "localhost",
             "database": "test",
             "user": "******",
             "password": "******"
         })
     cur = conn.cursor()
     cur.execute(
         "SELECT node_num "
         "FROM networks WHERE networks.networkid = %s"
         "LIMIT 1;",
         vars=('Genus_g', ))
     result = cur.fetchall()
     cur.close()
     conn.close()
     conn_object.delete_tables()
     self.assertEqual(result[0][0], 5)
Exemple #17
0
 def test_export_network(self):
     """
     Tests whether the network can be exported from the database.
     :return:
     """
     conn_object = BiomConnection()
     conn_object.create_tables()
     conn_object.add_biom(testbiom, 'banana')
     conn_object = IoConnection()
     conn_object.add_network(g, 'banana', 'banana')
     network = conn_object.export_network(name='banana')
     conn_object.delete_tables()
     self.assertEqual(len(network.edges), 3)
Exemple #18
0
 def test_get_taxlist(self):
     """
     Tests if the matching taxa with same taxonomy at Family level are returned.
     :return:
     """
     conn_object = BiomConnection()
     conn_object.create_tables()
     conn_object.add_biom(testbiom, 'test')
     conn_object = IoConnection()
     conn_object.add_network(network=g, name='g', study='test')
     conn_object = MetaConnection()
     pair = conn_object.get_taxlist(level='Family', network='g')
     conn_object.delete_tables()
     self.assertCountEqual(pair, ['GG_OTU_3', 'GG_OTU_4', 'GG_OTU_6'])
Exemple #19
0
 def test_get_union(self):
     """
     Tests if the union of the networks is returned.
     :return:
     """
     conn_object = BiomConnection()
     conn_object.create_tables()
     conn_object.add_biom(testbiom, 'test')
     conn_object = IoConnection()
     conn_object.add_network(network=g1, name='g1', study='test')
     conn_object.add_network(network=g2, name='g2', study='test')
     conn_object = SetConnection()
     union_set = conn_object.get_union(networks=["g1", "g2"])
     conn_object.delete_tables()
     self.assertTrue(len(union_set.edges), 4)
Exemple #20
0
 def test_get_difference(self):
     """
     Tests if the difference is returned with edges that have different weights.
     :return:
     """
     conn_object = BiomConnection()
     conn_object.create_tables()
     conn_object.add_biom(testbiom, 'test')
     conn_object = IoConnection()
     conn_object.add_network(network=g1, name='g1', study='test')
     conn_object.add_network(network=g2, name='g2', study='test')
     conn_object = SetConnection()
     difference_set = conn_object.get_difference(networks=["g1", "g2"])
     conn_object.delete_tables()
     self.assertEqual(len(difference_set.edges), 3)
Exemple #21
0
 def test_get_pairlist_weight_noresults(self):
     """
     Tests if the pair is not returned when weight is set to True.
     :return:
     """
     conn_object = BiomConnection()
     conn_object.create_tables()
     conn_object.add_biom(testbiom, 'test')
     conn_object = IoConnection()
     conn_object.add_network(network=g, name='g', study='test')
     conn_object = MetaConnection()
     pair = conn_object.get_pairlist(level='Family',
                                     weight=True,
                                     network='g')
     conn_object.delete_tables()
     self.assertEqual(len(pair), 0)
Exemple #22
0
 def test_get_pairlist(self):
     """
     Tests if the pair list is returned correctly,
     with a pair being 2 edges with matching partners at the specified taxonomic level.
     :return:
     """
     conn_object = BiomConnection()
     conn_object.create_tables()
     conn_object.add_biom(testbiom, 'test')
     conn_object = IoConnection()
     conn_object.add_network(network=g, name='g', study='test')
     conn_object = MetaConnection()
     pair = conn_object.get_pairlist(level='Family',
                                     weight=False,
                                     network='g')
     conn_object.delete_tables()
     self.assertCountEqual(pair[0], ['GG_OTU_1', 'GG_OTU_4'])
Exemple #23
0
 def test_aggr_networks(self):
     """
     Tests if network names are aggregated to an edge property
     named 'source'.
     :return:
     """
     conn_object = BiomConnection()
     conn_object.create_tables()
     conn_object.add_biom(testbiom, 'test')
     conn_object = IoConnection()
     conn_object.add_network(network=g1, name='g1', study='test')
     conn_object.add_network(network=g2, name='g2', study='test')
     conn_object = SetConnection()
     difference_set = conn_object.get_difference(networks=["g1", "g2"])
     conn_object.delete_tables()
     self.assertEqual(
         difference_set.edges[('GG_OTU_1', 'GG_OTU_2')]['source'], 'g1')
Exemple #24
0
 def test_get_intersection_3(self):
     """
     Tests if the intersection returns an empty network
     when the intersection is too large.
     :return:
     """
     conn_object = BiomConnection()
     conn_object.create_tables()
     conn_object.add_biom(testbiom, 'test')
     conn_object = IoConnection()
     conn_object.add_network(network=g1, name='g1', study='test')
     conn_object.add_network(network=g2, name='g2', study='test')
     conn_object = SetConnection()
     intersection_set = conn_object.get_intersection(networks=["g1", "g2"],
                                                     number=3)
     conn_object.delete_tables()
     self.assertEqual(len(intersection_set.edges), 0)
Exemple #25
0
 def test_extract_sets(self):
     """
     Tests if the import_networks function reads the correct database file,
     and imports the network file, by querying the networks table.
     :return:
     """
     conn_object = BiomConnection()
     conn_object.create_tables()
     conn_object.add_biom(testbiom, 'test')
     conn_object = IoConnection()
     conn_object.add_network(network=g1, name='g1', study='test')
     conn_object.add_network(network=g2, name='g2', study='test')
     extract_sets(set='intersection', path=os.getcwd(), weight=False)
     file = nx.read_graphml("intersection.graphml")
     conn_object.delete_tables()
     os.remove("intersection.graphml")
     self.assertEqual(len(file.edges), 3)
Exemple #26
0
 def test_edge_error(self):
     """
     Tests if the query correctly reports an error
     when network data is not present
     :return:
     """
     conn_object = BiomConnection()
     conn_object.create_tables()
     conn_object.add_biom(testbiom, 'banana')
     conn_object = IoConnection()
     self.assertRaises(
         psycopg2.Error,
         conn_object.add_edge(values=('apple', 'Streptococcus', 'Sample1',
                                      0.5)),
     )
     # long format table of 5 * 6 observations
     conn_object.delete_tables()
Exemple #27
0
 def test_get_intersection_weight(self):
     """
     Tests if the intersection returns the correct number of edges
     when the weight parameter is changed.
     :return:
     """
     conn_object = BiomConnection()
     conn_object.create_tables()
     conn_object.add_biom(testbiom, 'test')
     conn_object = IoConnection()
     conn_object.add_network(network=g1, name='g1', study='test')
     conn_object.add_network(network=g2, name='g2', study='test')
     conn_object = SetConnection()
     intersection_set = conn_object.get_intersection(networks=["g1", "g2"],
                                                     number=2,
                                                     weight=False)
     conn_object.delete_tables()
     self.assertEqual(len(intersection_set.edges), 3)
Exemple #28
0
 def test_get_intersection(self):
     """
     Tests if the import_network function reads the correct database file,
     and imports the network file, by querying the edges table.
     :return:
     """
     conn_object = BiomConnection()
     conn_object.create_tables()
     conn_object.add_biom(testbiom, 'test')
     conn_object = IoConnection()
     conn_object.add_network(network=g1, name='g1', study='test')
     conn_object.add_network(network=g2, name='g2', study='test')
     conn_object = SetConnection()
     intersection_set = conn_object.get_intersection(networks=["g1", "g2"],
                                                     number=2)
     conn_object.delete_tables()
     self.assertCountEqual(("GG_OTU_2", "GG_OTU_5"),
                           list(intersection_set.edges)[0])
Exemple #29
0
 def test_get_pairlist_weight_esults(self):
     """
     Tests if the pair is returned when weight is set to True and edge weights set to the same. .
     :return:
     """
     conn_object = BiomConnection()
     conn_object.create_tables()
     conn_object.add_biom(testbiom, 'test')
     conn_object = IoConnection()
     f = g.copy(as_view=False)
     f.edges[("GG_OTU_4", "GG_OTU_5")]['weight'] = 1.0
     conn_object.add_network(network=f, name='g', study='test')
     conn_object = MetaConnection()
     pair = conn_object.get_pairlist(level='Family',
                                     weight=True,
                                     network='g')
     conn_object.delete_tables()
     self.assertEqual(len(pair), 3)
Exemple #30
0
 def test_aggr_weight(self):
     """
     Tests if edge weights are aggregated to an edge property
     named 'weight'.
     :return:
     """
     conn_object = BiomConnection()
     conn_object.create_tables()
     conn_object.add_biom(testbiom, 'test')
     conn_object = IoConnection()
     conn_object.add_network(network=g1, name='g1', study='test')
     conn_object.add_network(network=g2, name='g2', study='test')
     conn_object = SetConnection()
     intersection_set = conn_object.get_intersection(networks=["g1", "g2"],
                                                     number=2,
                                                     weight=False)
     conn_object.delete_tables()
     self.assertEqual(
         intersection_set.edges[('GG_OTU_1', 'GG_OTU_2')]['weight'], '1,-1')