Skip to content

Commit

Permalink
Supporting rarefied tables (#11)
Browse files Browse the repository at this point in the history
* Supporting rarefied tables

* Fixing travis

* fixing travis (take 2)

* Addressing @ElDeveloper's comment
  • Loading branch information
josenavas authored and ElDeveloper committed Aug 22, 2017
1 parent 1927c3f commit 1ab2b96
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 45 deletions.
12 changes: 6 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@ before_install:
# Update conda itself
- conda update --yes conda
install:
- travis_retry conda create --yes -n qiita_env python=2.7 pip nose flake8
- travis_retry conda create --yes -n qiita python=2.7 pip nose flake8
pyzmq networkx pyparsing natsort mock future libgfortran
'pandas>=0.18' 'scipy>0.13.0' 'numpy>=1.7' 'h5py>=2.3.1'
- source activate qiita_env
- source activate qiita
- pip install sphinx sphinx-bootstrap-theme coveralls ipython[all]==2.4.1
- pip install https://github.com/biocore/qiita/archive/analysis-refactor.zip --process-dependency-links
- export MOI_CONFIG_FP=$HOME/miniconda3/envs/qiita_env/lib/python2.7/site-packages/qiita_core/support_files/config_test.cfg
- pip install https://github.com/biocore/qiita/archive/master.zip --process-dependency-links
- export MOI_CONFIG_FP=$HOME/miniconda3/envs/qiita/lib/python2.7/site-packages/qiita_core/support_files/config_test.cfg
- ipython profile create qiita-general --parallel
- qiita-env start_cluster qiita-general
- qiita-env make --no-load-ontologies
- export QIITA_SERVER_CERT=$HOME/miniconda3/envs/qiita_env/lib/python2.7/site-packages/qiita_core/support_files/server.crt
- export QIITA_SERVER_CERT=$HOME/miniconda3/envs/qiita/lib/python2.7/site-packages/qiita_core/support_files/server.crt
- source deactivate
- travis_retry conda create --yes -n env_name python=$PYTHON_VERSION pip nose flake8 coverage numpy pandas 'h5py>=2.3.1' matplotlib seaborn
- source activate env_name
- pip install https://github.com/qiita-spots/qiita_client/archive/master.zip
- travis_retry pip install . --process-dependency-links
- configure_biom --env-script "source activate env_name" --server-cert $QIITA_SERVER_CERT
before_script:
- source activate qiita_env
- source activate qiita
- qiita plugins update
- qiita pet webserver --no-build-docs start &
script:
Expand Down
98 changes: 60 additions & 38 deletions qtp_biom/summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,20 @@
import seaborn as sns


def generate_html_summary(qclient, job_id, parameters, out_dir):
"""Generates the HTML summary of a BIOM artifact
def _generate_html(biom):
"""Generate the HTML summary for the given biom table
Parameters
----------
qclient : qiita_client.QiitaClient
The Qiita server client
job_id : str
The job id
parameters : dict
The parameter values to validate and create the artifact
out_dir : str
The path to the job's output directory
biom : biom.Table
The biom table to generate the summary from
Returns
-------
bool, None, str
Whether the job is successful
Ignored
The error message, if not successful
str
A string with the HTML summary page
"""
# Step 1: gather file information from qiita using REST api
artifact_id = parameters['input_data']
qclient_url = "/qiita_db/artifacts/%s/" % artifact_id
artifact_info = qclient.get(qclient_url)

# if we get to this point of the code we are sure that this is a biom file
# and that it only has one element
fp = artifact_info['files']['biom'][0]

# Step 2: generate HTML summary
# Modified from https://goo.gl/cUVHgB
biom = load_table(fp)
num_features, num_samples = biom.shape

sample_counts = []
Expand All @@ -64,15 +45,6 @@ def generate_html_summary(qclient, job_id, parameters, out_dir):
'Median count': np.median(sample_counts),
}

ax = sns.distplot(sample_counts)
ax.set_xlabel("Number of sequences per sample")
ax.set_ylabel("Frequency")
plot = ax.get_figure()
sc_plot = StringIO()
plot.savefig(sc_plot, format='png')
sc_plot.seek(0)

uri = 'data:image/png;base64,' + quote(b64encode(sc_plot.buf))
artifact_information = [
"<b>Number of samples:</b> %d<br/>" % num_samples,
"<b>Number of features:</b> %d<br/>" % num_features,
Expand All @@ -84,13 +56,63 @@ def generate_html_summary(qclient, job_id, parameters, out_dir):
sample_count_summary['Median count']),
("<b>Mean count:</b> %d<br/>" %
sample_count_summary['Mean count']),
'<br/><hr/><br/>',
'<img src = "%s"/>' % uri
]
'<br/><hr/><br/>']

if sample_count_summary['Minimum count'] == sample_count_summary[
'Maximum count']:
artifact_information.append(
"All the samples in your BIOM table have %d sequences, "
"no plot will be shown below."
% sample_count_summary['Minimum count'])
else:
ax = sns.distplot(sample_counts)
ax.set_xlabel("Number of sequences per sample")
ax.set_ylabel("Frequency")
plot = ax.get_figure()
sc_plot = StringIO()
plot.savefig(sc_plot, format='png')
sc_plot.seek(0)
uri = 'data:image/png;base64,' + quote(b64encode(sc_plot.buf))
artifact_information.append('<img src = "%s"/>' % uri)

return '\n'.join(artifact_information)


def generate_html_summary(qclient, job_id, parameters, out_dir):
"""Generates the HTML summary of a BIOM artifact
Parameters
----------
qclient : qiita_client.QiitaClient
The Qiita server client
job_id : str
The job id
parameters : dict
The parameter values to validate and create the artifact
out_dir : str
The path to the job's output directory
Returns
-------
bool, None, str
Whether the job is successful
Ignored
The error message, if not successful
"""
# Step 1: gather file information from qiita using REST api
artifact_id = parameters['input_data']
qclient_url = "/qiita_db/artifacts/%s/" % artifact_id
artifact_info = qclient.get(qclient_url)

# if we get to this point of the code we are sure that this is a biom file
# and that it only has one element
fp = artifact_info['files']['biom'][0]

# Step 2: generate HTML summary
biom = load_table(fp)
of_fp = join(out_dir, "%s.html" % basename(fp))
with open(of_fp, 'w') as of:
of.write('\n'.join(artifact_information))
of.write(_generate_html(biom))

# Step 3: add the new file to the artifact using REST api
success = True
Expand Down
22 changes: 21 additions & 1 deletion qtp_biom/tests/test_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
from shutil import rmtree
from json import dumps

from biom import Table
import numpy as np
from qiita_client.testing import PluginTestCase

from qtp_biom.summary import generate_html_summary
from qtp_biom.summary import generate_html_summary, _generate_html


class SummaryTestsWith(PluginTestCase):
Expand Down Expand Up @@ -60,6 +62,13 @@ def test_generate_html_summary(self):
html = html_f.read()
self.assertRegexpMatches(html, '\n'.join(EXP_HTML_REGEXP))

def test_generate_html_summary_rarefied(self):
# Create a new biom table
data = np.asarray([[0, 2, 4], [2, 2, 2], [4, 2, 0]])
table = Table(data, ['O1', 'O2', 'O3'], ['S1', 'S2', 'S3'])
obs = _generate_html(table)
self.assertEqual(obs, '\n'.join(EXP_HTML_RAREFIED))


EXP_HTML_REGEXP = [
'<b>Number of samples:</b> 7<br/>',
Expand All @@ -71,6 +80,17 @@ def test_generate_html_summary(self):
'<br/><hr/><br/>',
'<img src = "data:image/png;base64,.*"/>']

EXP_HTML_RAREFIED = [
'<b>Number of samples:</b> 3<br/>',
'<b>Number of features:</b> 3<br/>',
'<b>Minimum count:</b> 6<br/>',
'<b>Maximum count:</b> 6<br/>',
'<b>Median count:</b> 6<br/>',
'<b>Mean count:</b> 6<br/>',
'<br/><hr/><br/>',
'All the samples in your BIOM table have 6 sequences, '
'no plot will be shown below.']


if __name__ == '__main__':
main()

0 comments on commit 1ab2b96

Please sign in to comment.