Ejemplo n.º 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')
Ejemplo n.º 2
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')
Ejemplo n.º 3
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")])
Ejemplo n.º 4
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()
Ejemplo n.º 5
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()
Ejemplo n.º 6
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()
Ejemplo n.º 7
0
 def test_observation_error(self):
     """
     Tests if the query correctly reports an error
     when a taxon is not present in the taxonomy 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'))
     self.assertRaises(
         psycopg2.Error,
         conn_object.add_observation(values=('banana', 'Streptococcus',
                                             'Sample1', 0.5)),
     )
     # long format table of 5 * 6 observations
     conn_object.delete_tables()
Ejemplo n.º 8
0
 def test_add_summary(self):
     """
     Tests whether a row is added to the bioms table.
     :return:
     """
     conn_object = BiomConnection()
     conn_object.create_tables()
     conn_object.add_summary(values=('potato', 5, 20))
     conn = psycopg2.connect(
         **{
             "host": "localhost",
             "database": "test",
             "user": "******",
             "password": "******"
         })
     cur = conn.cursor()
     cur.execute("SELECT * from bioms;")
     result = cur.fetchall()
     # long format table of 5 * 6 observations
     self.assertEqual(result[0], ('potato', 5, 20))
     cur.close()
     conn.close()
     conn_object.delete_tables()