示例#1
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")])
示例#2
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')
示例#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')
示例#4
0
def masq(masq_args):
    """
    Main function for running masq.
    Accepts a dictionary of arguments from the argument parser
    and calls the appropriate module function.

    :param masq_args: Arguments.
    :return:
    """
    if masq_args['version']:
        info = VersionInfo('anuran')
        logger.info('Version ' + info.version_string())
        sys.exit(0)
    # unpack args
    config = masq_args['config']
    database = masq_args['config']
    networks = masq_args['networks']
    bioms = masq_args['bioms']
    username = masq_args['username']
    password = masq_args['password']
    host = masq_args['host']
    mapping = masq_args['mapping']
    sources = masq_args['sources']
    if masq_args['mapping']:
        try:
            with open(masq_args['mapping'], 'r') as file:
                contents = file.read()
                mapping = literal_eval(contents)
        except (ValueError, TypeError):
            logger.warning("Mapping file could not be imported,\n"
                           "and will be ignored. ")
    if masq_args['sources']:
        try:
            with open(masq_args['sources'], 'r') as file:
                contents = file.read()
                mapping = literal_eval(contents)
        except (ValueError, TypeError):
            logger.warning("Source file could not be imported,\n"
                           "and will be ignored. ")
    if masq_args['create']:
        logger.info('Setting up tables in PostgreSQL database. ')
        setup_database(config=config,
                       create=True,
                       host=host,
                       database=database,
                       username=username,
                       password=password)
    elif masq_args['delete']:
        setup_database(config=config,
                       create=False,
                       host=host,
                       database=database,
                       username=username,
                       password=password)
        logger.info("Deleted tables in PostgreSQL database.")
    if masq_args['bioms']:
        logger.info('Importing BIOM files... ')
        for biom in bioms:
            import_biom(location=biom,
                        mapping=mapping,
                        config=config,
                        host=host,
                        database=database,
                        username=username,
                        password=password)
    if masq_args['networks']:
        logger.info('Importing network files...')
        for network in networks:
            import_networks(location=network,
                            mapping=mapping,
                            sources=sources,
                            config=config,
                            host=host,
                            database=database,
                            username=username,
                            password=password)
    logger.info('Completed tasks! ')