Skip to content

Commit

Permalink
Merge 40f78ae into 77e5cd3
Browse files Browse the repository at this point in the history
  • Loading branch information
antgonza committed Mar 20, 2018
2 parents 77e5cd3 + 40f78ae commit f3e65be
Show file tree
Hide file tree
Showing 17 changed files with 542 additions and 7 deletions.
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,12 @@ ENV/
# Rope project settings
.ropeproject

#extra bowtie files
# Extra bowtie files
._bowtie2
.adapters

#Redis
# Shogun DB
qp_shotgun/shogun/databases/shogun/

# Redis
dump.rdb
18 changes: 15 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,17 @@ install:
- conda install --yes -c bioconda diamond=0.8 bowtie2 fastqc=0.11.5 samtools bedtools
- pip install sphinx sphinx-bootstrap-theme coveralls biopython
- pip install https://github.com/qiita-spots/qiita_client/archive/master.zip
- pip install atropos
- pip install atropos humann2
- pip install git+https://github.com/knights-lab/SHOGUN.git
- export QIITA_SERVER_CERT=$PWD/miniconda3/envs/qiita/lib/python2.7/site-packages/qiita_core/support_files/server.crt
# just making sure we have the latest flake8
- pip install -U flake8
- pip install .
- export QC_FILTER_DB_DP=$PWD/qp_shotgun/qc_filter/database/
- export QC_FILTER_DB_DP=$PWD/qp_shotgun/qc_filter/databases/
- export QC_FILTER_TEMP_DP=$PWD/qp_shotgun/qc_filter/temp/
- export QC_SHOGUN_DB_DP=$PWD/qp_shotgun/shogun/databases/
- export QC_SHOGUN_TEMP_DP=$PWD/qp_shotgun/qc_filter/temp/
- for file in $(ls $QC_SHOGUN_DB_DP); do tar xvjf $QC_SHOGUN_DB_DP$file -C $QC_SHOGUN_DB_DP; done
# installing other dependencies
- wget --quiet https://bitbucket.org/biobakery/metaphlan2/get/default.zip
- unzip -qq default.zip 'biobakery-metaphlan2*/utils/*' 'biobakery-metaphlan2*/metaphlan2.py'
Expand All @@ -48,6 +54,12 @@ install:
- wget --quiet https://github.com/biocore/oecophylla/blob/dev/test_data/test_dbs/mp2_mini.tar.gz?raw=true -O mp2_mini.tar.gz
- tar zxf mp2_mini.tar.gz
- for f in `ls mp2_mini`; do mv mp2_mini/$f $HUMANN2_DIR/db_v20/${f//mp2_mini/mpa_v20_m200}; done
- wget https://github.com/knights-lab/UTree/releases/download/v2.0c/utree-search_gg
- chmod 755 utree-search_gg
- mv utree-search_gg $CONDA_PREFIX/bin
- wget https://github.com/knights-lab/BURST/releases/download/v0.99.4a/burst_linux_DB15
- chmod 755 burst_linux_DB15
- mv burst_linux_DB15 $CONDA_PREFIX/bin/burst15
before_script:
- source activate qiita
- qiita pet webserver start &
Expand All @@ -56,7 +68,7 @@ script:
- source activate qp-shotgun
- sleep 10 # give enough time to the webserver to start
- configure_shotgun --env-script "source activate qp-shotgun" --server-cert $QIITA_SERVER_CERT
- travis_wait 40 nosetests --with-doctest --with-coverage --verbosity=3
- travis_wait 40 nosetests --with-doctest --with-coverage -vv
- flake8 qp_shotgun setup.py scripts
addons:
postgresql: "9.3"
Expand Down
2 changes: 2 additions & 0 deletions qp_shotgun/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from .humann2 import humann2_cmd
from .qc_trim import qc_trim_cmd
from .qc_filter import qc_filter_cmd
from .shogun import shogun_cmd


# Initialize the plugin
Expand All @@ -20,3 +21,4 @@
plugin.register_command(humann2_cmd)
plugin.register_command(qc_trim_cmd)
plugin.register_command(qc_filter_cmd)
plugin.register_command(shogun_cmd)
Binary file removed qp_shotgun/qc_filter/database/._bowtie2
Binary file not shown.
40 changes: 40 additions & 0 deletions qp_shotgun/shogun/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# -----------------------------------------------------------------------------
# Copyright (c) 2014--, The Qiita Development Team.
#
# Distributed under the terms of the BSD 3-clause License.
#
# The full license is in the file LICENSE, distributed with this software.
# -----------------------------------------------------------------------------

from qiita_client import QiitaCommand
from .shogun import shogun
from .utils import (generate_shogun_dflt_params, get_dbs_list)
from os.path import join
from os import environ


__all__ = ['Shogun']

# Define the shogun command
default_db = join(environ["QC_SHOGUN_DB_DP"], 'shogun')
default_db_list = get_dbs_list(environ["QC_SHOGUN_DB_DP"])
req_params = {'input': ('artifact', ['per_sample_FASTQ'])}
opt_params = {
# database
'Database': ["choice: [%s]" % default_db_list,
default_db],
# aligner
'Aligner tool': ['choice:["utree", "burst", "bowtie2"]', 'bowtie2'],
# taxonomic levels
'Taxonomy Level': ['choice:["kingdom", "phylum", "class", "order", '
'"family", "genus", "species", "strain", "all"]',
'all'],
# threads
'Number of threads': ['integer', '1'],
}
outputs = {'Functional Predictions': 'BIOM', 'Taxonomic Predictions': 'BIOM'}
dflt_param_set = generate_shogun_dflt_params()

shogun_cmd = QiitaCommand(
'Shogun', "Functional and Taxonomic Predictions", shogun,
req_params, opt_params, outputs, dflt_param_set)
Binary file added qp_shotgun/shogun/databases/shogun.tar.bz2
Binary file not shown.
189 changes: 189 additions & 0 deletions qp_shotgun/shogun/shogun.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
# -----------------------------------------------------------------------------
# Copyright (c) 2014--, The Qiita Development Team.
#
# Distributed under the terms of the BSD 3-clause License.
#
# The full license is in the file LICENSE, distributed with this software.
# -----------------------------------------------------------------------------
from os.path import join
from tempfile import TemporaryDirectory
from .utils import readfq
from qp_shotgun.utils import (make_read_pairs_per_sample)
import gzip

SHOGUN_PARAMS = {
'Database': 'database', 'Aligner tool': 'aligner',
'Taxonomic Level': 'levels', 'Number of threads': 'threads'}


def generate_fna_file(temp_path, samples):
# Combines reverse and forward seqs per sample
# Returns filepaths of new combined files
output_fp = join(temp_path, 'combined.fna')
output = open(output_fp, "a")
count = 0
for run_prefix, sample, f_fp, r_fp in samples:
with gzip.open(f_fp, 'rt') as fp:
# Loop through forward file
for header, seq, qual in readfq(fp):
output.write("%s_%d\n" % (sample, count))
output.write("%s\n" % seq)
count += 1
with gzip.open(r_fp, 'rt') as fp:
# Loop through reverse file
for header, seq, qual in readfq(fp):
output.write("%s_%d\n" % (sample, count))
output.write("%s\n" % seq)
count += 1
output.close()

return output_fp


def _format_params(parameters, func_params):
params = {}
# Loop through all of the commands alphabetically
for param in func_params:
# Find the value using long parameter names
parameter = func_params[param]
value = parameters[param]
params[parameter] = value

return params


def generate_shogun_align_commands(input_fp, temp_dir, parameters):
cmds = []
cmds.append(
'shogun align --aligner {aligner} --threads {threads} '
'--database {database} --input {input} --output {output}'.format(
aligner=parameters['aligner'],
threads=parameters['threads'],
database=parameters['database'],
input=input_fp,
output=temp_dir))

return cmds


def generate_shogun_assign_taxonomy_commands(temp_dir, parameters):
cmds = []
aln2ext = {'utree': 'tsv', 'burst': 'b6', 'bowtie2': 'sam'}
ext = aln2ext[parameters['aligner']]
output_fp = join(temp_dir, 'profile.tsv')
cmds.append(
'shogun assign_taxonomy '
'--aligner {aligner} '
'--database {database} '
'--input {input} --output {output}'.format(
aligner=parameters['aligner'],
database=parameters['database'],
input=join(temp_dir, 'alignment.%s.%s' % (parameters['aligner'],
ext)),
output=output_fp))

return cmds, output_fp


def generate_shogun_functional_commands(profile_dir, temp_dir,
parameters, sel_level):
cmds = []
cmds.append(
'shogun functional '
'--database {database} '
'--input {input} '
'--output {output} '
'--level {level}'.format(
database=parameters['database'],
input=profile_dir,
output=join(temp_dir, 'profile.functional.%s.tsv' % sel_level),
level=sel_level))

return cmds


def generate_biom_conversion_commands(input_fp, output_dir, level):
cmds = []
cmds.append(
'biom convert -i {input} '
'-o {output} '
'--table-type="OTU table" '
'--process-obs-metadata taxonomy --to-hdf5'.format(
input=input_fp,
output=join(output_dir, 'otu_table.%s.biom' % level)))

return cmds


def shogun(qclient, job_id, parameters, out_dir):
"""Run Shogun with the given parameters
Parameters
----------
qclient : tgp.qiita_client.QiitaClient
The Qiita server client
job_id : str
The job id
parameters : dict
The parameter values to run split libraries
out_dir : str
The path to the job's output directory
Returns
-------
bool, list, str
The results of the job
"""
# Step 1 get the rest of the information need to run Atropos
qclient.update_job_step(job_id, "Step 1 of 6: Collecting information")
artifact_id = parameters['input']
del parameters['input']

# Get the artifact filepath information
artifact_info = qclient.get("/qiita_db/artifacts/%s/" % artifact_id)
fps = artifact_info['files']

# Get the artifact metadata
prep_info = qclient.get('/qiita_db/prep_template/%s/'
% artifact_info['prep_information'][0])
qiime_map = prep_info['qiime-map']

# Step 2 converting to fna
qclient.update_job_step(
job_id, "Step 2 of 6: Converting to FNA for Shogun")

with TemporaryDirectory(dir=out_dir, prefix='shogun_') as temp_dir:
rs = fps['raw_reverse_seqs'] if 'raw_reverse_seqs' in fps else []
samples = make_read_pairs_per_sample(
fps['raw_forward_seqs'], rs, qiime_map)

comb_fp = generate_fna_file(temp_dir, samples)
# Combining files
parameters = _format_params(parameters, SHOGUN_PARAMS)
# Step 3 align
qclient.update_job_step(
job_id, "Step 3 of 6: Aligning FNA with Shogun")
generate_shogun_align_commands(
comb_fp, temp_dir, parameters)
# Step 4 taxonomic profile
qclient.update_job_step(
job_id, "Step 4 of 6: Taxonomic profile with Shogun")
cmd, profile_fp = generate_shogun_assign_taxonomy_commands(
temp_dir, parameters)
# Step 5 functional profile
qclient.update_job_step(
job_id, "Step 5 of 6: Functional profile with Shogun")
levels = ['genus', 'species', 'level']
for level in levels:
generate_shogun_functional_commands(
profile_fp, temp_dir, parameters, level)
# Step 6 functional profile
qclient.update_job_step(
job_id, "Step 6 of 6: Converting results to BIOM")
for level in levels:
input_fp = ('profile.functional.%s.tsv' % level)
generate_biom_conversion_commands(input_fp, out_dir, level)

ainfo = {}

return True, ainfo, ""
7 changes: 7 additions & 0 deletions qp_shotgun/shogun/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -----------------------------------------------------------------------------
# Copyright (c) 2014--, The Qiita Development Team.
#
# Distributed under the terms of the BSD 3-clause License.
#
# The full license is in the file LICENSE, distributed with this software.
# -----------------------------------------------------------------------------

0 comments on commit f3e65be

Please sign in to comment.