Esempio n. 1
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)
Esempio n. 2
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)
Esempio n. 3
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)
Esempio n. 4
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')
Esempio n. 5
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)
Esempio n. 6
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')
Esempio n. 7
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)
Esempio n. 8
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)
Esempio n. 9
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)
Esempio n. 10
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'])
Esempio n. 11
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)
Esempio n. 12
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)
Esempio n. 13
0
 def test_IoConnection(self):
     """
     Tests if the IoConnection can be successfully initialized,
     with the correct config parameters.
     :return:
     """
     test_config = {
         'host': 'localhost',
         'database': 'test',
         'user': '******',
         'password': '******'
     }
     conn = IoConnection()
     read_config = conn.config
     self.assertTrue(test_config == read_config)
Esempio n. 14
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)
Esempio n. 15
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')
Esempio n. 16
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()
Esempio n. 17
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)
Esempio n. 18
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'])
Esempio n. 19
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)
Esempio n. 20
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])
Esempio n. 21
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)
Esempio n. 22
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)
Esempio n. 23
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')
Esempio n. 24
0
 def test_convert_network(self):
     """
     Tests whether the set result is converted to a Networkx graph with 1 edge.
     :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')
     set_query = "SELECT string_agg(networkID::varchar, ',') AS networks, " \
                 "source, target, SIGN(weight), COUNT(*) " \
                 "FROM edges " \
                 "WHERE networkID IN " + str(('g1', 'g2')) + \
                 " GROUP BY source, target, SIGN(weight) " \
                 "HAVING COUNT(*) > " + str(1)
     set_result = conn_object.query(set_query, fetch=True)
     graph = _convert_network(set_result)
     conn_object.delete_tables()
     self.assertEqual(len(graph.edges), 1)
Esempio n. 25
0
 def test_add_network_node(self):
     """
     Tests whether a row is added to the networks table.
     :return:
     """
     conn_object = BiomConnection()
     conn_object.create_tables()
     conn_object.add_biom(testbiom, 'potato')
     conn_object = IoConnection()
     conn_object.add_network_node(values=('potato', 'potato', 5, 20))
     conn = psycopg2.connect(
         **{
             "host": "localhost",
             "database": "test",
             "user": "******",
             "password": "******"
         })
     cur = conn.cursor()
     cur.execute("SELECT * from networks;")
     result = cur.fetchall()
     cur.close()
     conn.close()
     conn_object.delete_tables()
     self.assertEqual(result[0], ('potato', 'potato', 5, 20))