Ejemplo n.º 1
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)
Ejemplo n.º 2
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')
Ejemplo 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)
Ejemplo n.º 4
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)
Ejemplo n.º 5
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)
Ejemplo n.º 6
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')
Ejemplo 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)
Ejemplo n.º 8
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)
Ejemplo 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)
Ejemplo 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'])
Ejemplo 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)
Ejemplo 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)
Ejemplo n.º 13
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)
Ejemplo n.º 14
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)
Ejemplo n.º 15
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'])
Ejemplo n.º 16
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)
Ejemplo n.º 17
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()
Ejemplo n.º 18
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')
Ejemplo n.º 19
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])
Ejemplo n.º 20
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)
Ejemplo 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)
Ejemplo n.º 22
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')
Ejemplo n.º 23
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)
Ejemplo n.º 24
0
 def test_add_biom(self):
     """
     Tests if the BIOM file is imported by
     querying the count data.
     :return:
     """
     conn_object = BiomConnection()
     conn_object.create_tables()
     conn_object.add_biom(testbiom, 'banana')
     conn = psycopg2.connect(
         **{
             "host": "localhost",
             "database": "test",
             "user": "******",
             "password": "******"
         })
     cur = conn.cursor()
     cur.execute("SELECT * from counts;")
     result = cur.fetchall()
     # long format table of 5 * 6 observations
     self.assertEqual(len(result), 30)
     cur.close()
     conn.close()
     conn_object.delete_tables()
Ejemplo 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))