Skip to content

Commit

Permalink
Merge pull request #144 from squirrelo/add-demo-files-with-dowload-to…
Browse files Browse the repository at this point in the history
…-the-database

Add demo files with dowload to the database
  • Loading branch information
antgonza committed Jun 20, 2014
2 parents 19f2957 + ae9737b commit 2a4e108
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion qiita_db/environment_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,23 @@
#
# The full license is in the file LICENSE, distributed with this software.
# -----------------------------------------------------------------------------

from tempfile import mkdtemp
from tarfile import open as taropen
from gzip import open as gzopen
from os import remove
from os.path import abspath, dirname, join
from shutil import rmtree, move
from functools import partial
try:
from urllib import urlretrieve
except ImportError: # python3
from urllib.request import urlretrieve

from psycopg2 import connect
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT

from qiita_db.util import get_db_files_base_dir

get_support_file = partial(join, join(dirname(abspath(__file__)),
'support_files'))

Expand Down Expand Up @@ -165,6 +176,37 @@ def make_demo_environment(base_data_dir, base_work_dir, user, password, host):
cur.close()
conn.close()

# download files from thebeast
url = ("ftp://thebeast.colorado.edu/pub/QIIME_DB_Public_Studies/study_1001"
"_split_library_seqs_and_mapping.tgz")
outdir = mkdtemp()
basedir = join(outdir, "study_1001_split_library_seqs_and_mapping/")
try:
urlretrieve(url, join(outdir, "study_1001.tar.gz"))
except:
raise IOError("Error: DOWNLOAD FAILED")
rmtree(outdir)

# untar the files
with taropen(join(outdir, "study_1001.tar.gz")) as tar:
tar.extractall(outdir)
# un-gzip sequence file
with gzopen(join(basedir, "study_1001_split_library_seqs.fna.gz")) as gz:
with open(join(basedir, "seqs.fna"), 'w') as fout:
fout.write(gz.read())

# copy the preprocessed and procesed data to the study
dbbase = get_db_files_base_dir()
remove(join(dbbase, "processed_data/study_1001_closed_reference_otu"
"_table.biom"))
remove(join(dbbase, "preprocessed_data/seqs.fna"))
move(join(basedir, "study_1001_closed_reference_otu_table.biom"),
join(dbbase, "processed_data"))
move(join(basedir, "seqs.fna"), join(dbbase, "preprocessed_data"))

# clean up after ourselves
rmtree(outdir)


def drop_demo_environment(user, password, host):
r"""Drops the demo environment.
Expand All @@ -178,6 +220,7 @@ def drop_demo_environment(user, password, host):
host : str
The host where the postgres server is running
"""
base = get_db_files_base_dir()
# Connect to the postgres server
conn = connect(user=user, host=host, password=password)
# Set the isolation level to AUTOCOMMIT so we can execute a
Expand All @@ -190,6 +233,13 @@ def drop_demo_environment(user, password, host):
cur.close()
conn.close()

# wipe the overwriiten test files so empty as on repo
with open(join(base, "preprocessed_data/seqs.fna"), 'w') as fout:
fout.write("\n")
with open(join(base, "processed_data/study_1001_closed_reference_otu_"
"table.biom"), 'w') as fout:
fout.write("\n")


def make_production_environment():
"""TODO: Not implemented"""
Expand Down

0 comments on commit 2a4e108

Please sign in to comment.