Skip to content

Instantly share code, notes, and snippets.

@gregcaporaso
Created March 9, 2016 01:25
Show Gist options
  • Save gregcaporaso/c2011db0c2bb17f6621b to your computer and use it in GitHub Desktop.
Save gregcaporaso/c2011db0c2bb17f6621b to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Prepare the environments with some imports"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from qiime.sdk.workflow import WorkflowTemplate\n",
"from qiime.sdk.type.primitive import Int\n",
"from feature_table import rarefy\n",
"from feature_table.artifact_types.feature_table import FeatureTable, Frequency"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create an artifact to work with."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import biom\n",
"from qiime.sdk.artifact import Artifact\n",
"biom_path = \"/Users/caporaso/code/q2d2/example-data/keyboard/q191/otu-table.tsv\"\n",
"\n",
"table = biom.load_table(biom_path)\n",
"Artifact.save(table, FeatureTable[Frequency], \"nothing...\", \"./table.qtf\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Instantiate the workflow template (note that the output is a concrete type here, so the fact that ``Signature.__call__`` isn't actually solving anything isn't a problem yet). "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"wt = WorkflowTemplate.from_function(function=rarefy, \n",
" inputs={'table': FeatureTable[Frequency], 'depth': Int}, \n",
" outputs={'rarefied_table': FeatureTable[Frequency]}, \n",
" name=\"Rarefaction\",\n",
" doc=\"Let's rarefy!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Instantiate the context. Note that it discovers its artifacts."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from qiime.q2d3.context import Q2D3Context"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"q2d3 = Q2D3Context('./')"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{UUID('3e8e39d1-f2b2-4428-b6e9-bd618093854e'): './table.qtf'}"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"q2d3.data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Call the context to generate the actual job. Here we're doing some ugly stuff to grab the UUID from ``q2d3.data``, since the UUID is different everytime we run this."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"```python\n",
">>> from qiime.sdk.artifact import Artifact\n",
">>> table = Artifact('./table.qtf').data\n",
">>> depth = 42\n",
"```\n",
"\n",
"Let's rarefy!\n",
"\n",
"```python\n",
">>> from feature_table._normalize import rarefy\n",
">>> rarefied_table = rarefy(depth=depth, table=table)\n",
"```\n",
"\n",
"```python\n",
">>> from feature_table.artifact_types.feature_table import Frequency\n",
">>> from feature_table.artifact_types.feature_table import FeatureTable\n",
">>> Artifact.save(rarefied_table, FeatureTable[Frequency], None, '/Users/caporaso/temp/q2d3-test/tmpmct3i3e_.qtf')\n",
"```\n"
]
}
],
"source": [
"print(q2d3(wt, {'table': list(q2d3.data.keys())[0]}, {'depth': 42}))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, restart the kernel (see that the cell counts start over) and confirm that the job code does actually work. "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from qiime.sdk.artifact import Artifact\n",
"table = Artifact('./table.qtf').data\n",
"depth = 42"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from feature_table._normalize import rarefy\n",
"rarefied_table = rarefy(table=table, depth=depth)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from feature_table.artifact_types.feature_table import Frequency\n",
"from feature_table.artifact_types.feature_table import FeatureTable\n",
"Artifact.save(rarefied_table, FeatureTable[Frequency], None, '/Users/caporaso/temp/q2d3-test/tmpmct3i3e_.qtf')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x tmpmct3i3e_/\r\n",
"x tmpmct3i3e_/metadata.yaml\r\n",
"x tmpmct3i3e_/README.md\r\n",
"x tmpmct3i3e_/data/\r\n",
"x tmpmct3i3e_/data/feature-table.biom\r\n"
]
}
],
"source": [
"!tar -xvf '/Users/caporaso/temp/q2d3-test/tmpmct3i3e_.qtf'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The table is rarefied! "
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Num samples: 104\r\n",
"Num observations: 340\r\n",
"Total count: 4368\r\n",
"Table density (fraction of non-zero values): 0.045\r\n",
"\r\n",
"Counts/sample summary:\r\n",
" Min: 42.0\r\n",
" Max: 42.0\r\n",
" Median: 42.000\r\n",
" Mean: 42.000\r\n",
" Std. dev.: 0.000\r\n",
" Sample Metadata Categories: None provided\r\n",
" Observation Metadata Categories: taxonomy\r\n",
"\r\n",
"Counts/sample detail:\r\n",
"M9.Pinky.R: 42.0\r\n",
"K1.Shift.L: 42.0\r\n",
"K3.G: 42.0\r\n",
"M9.Pinky.L: 42.0\r\n",
"K2.F: 42.0\r\n",
"K3.Space: 42.0\r\n",
"K3.Shift.L: 42.0\r\n",
"K2.X: 42.0\r\n",
"K3.P: 42.0\r\n",
"K2.C: 42.0\r\n",
"M9.Index.L: 42.0\r\n",
"M2.Middle.R: 42.0\r\n",
"K1.G: 42.0\r\n",
"K3.O: 42.0\r\n",
"K3.Enter: 42.0\r\n",
"K2.P: 42.0\r\n",
"M9.Thumb.L: 42.0\r\n",
"M3.Index.L: 42.0\r\n",
"K1.W: 42.0\r\n",
"K3.J: 42.0\r\n",
"K1.Y: 42.0\r\n",
"K1.V: 42.0\r\n",
"M2.Index.R: 42.0\r\n",
"M3.Ring.L: 42.0\r\n",
"K3.T: 42.0\r\n",
"M2.Pinky.L: 42.0\r\n",
"K1.R: 42.0\r\n",
"K1.K: 42.0\r\n",
"K3.K: 42.0\r\n",
"K3.F: 42.0\r\n",
"K3.Y: 42.0\r\n",
"K1.H: 42.0\r\n",
"K3.Z: 42.0\r\n",
"K1.C: 42.0\r\n",
"M3.Thumb.L: 42.0\r\n",
"K3.W: 42.0\r\n",
"M2.Thumb.R: 42.0\r\n",
"M9.Index.R: 42.0\r\n",
"M2.Index.L: 42.0\r\n",
"K3.V: 42.0\r\n",
"K3.X: 42.0\r\n",
"K3.I: 42.0\r\n",
"K2.Enter: 42.0\r\n",
"M3.Ring.R: 42.0\r\n",
"K2.O: 42.0\r\n",
"K3.U: 42.0\r\n",
"K2.A: 42.0\r\n",
"K1.P: 42.0\r\n",
"K2.Q: 42.0\r\n",
"K3.Shift.R: 42.0\r\n",
"K1.Space: 42.0\r\n",
"M9.Middle.R: 42.0\r\n",
"M3.Index.R: 42.0\r\n",
"M2.Thumb.L: 42.0\r\n",
"K1.T: 42.0\r\n",
"K1.J: 42.0\r\n",
"K2.S: 42.0\r\n",
"K1.N: 42.0\r\n",
"K1.E: 42.0\r\n",
"K2.D: 42.0\r\n",
"K1.A: 42.0\r\n",
"K3.S: 42.0\r\n",
"K3.R: 42.0\r\n",
"K2.B: 42.0\r\n",
"K3.D: 42.0\r\n",
"K2.K: 42.0\r\n",
"K3.H: 42.0\r\n",
"K3.Q: 42.0\r\n",
"K2.M: 42.0\r\n",
"M3.Pinky.L: 42.0\r\n",
"M9.Middle.L: 42.0\r\n",
"K3.C: 42.0\r\n",
"K2.W: 42.0\r\n",
"K3.M: 42.0\r\n",
"K1.B: 42.0\r\n",
"K1.L: 42.0\r\n",
"K3.L: 42.0\r\n",
"K2.Space: 42.0\r\n",
"M3.Middle.L: 42.0\r\n",
"K3.B: 42.0\r\n",
"K1.Q: 42.0\r\n",
"M3.Pinky.R: 42.0\r\n",
"K2.G: 42.0\r\n",
"M9.Thumb.R: 42.0\r\n",
"M9.Ring.R: 42.0\r\n",
"M9.Ring.L: 42.0\r\n",
"K3.E: 42.0\r\n",
"K1.Shift.R: 42.0\r\n",
"K1.Z: 42.0\r\n",
"M2.Ring.L: 42.0\r\n",
"K3.A: 42.0\r\n",
"M2.Middle.L: 42.0\r\n",
"K2.N: 42.0\r\n",
"K2.V: 42.0\r\n",
"M3.Middle.R: 42.0\r\n",
"M2.Ring.R: 42.0\r\n",
"K1.X: 42.0\r\n",
"K1.M: 42.0\r\n",
"M2.Pinky.R: 42.0\r\n",
"K2.E: 42.0\r\n",
"K3.N: 42.0\r\n",
"K2.H: 42.0\r\n",
"K2.Y: 42.0\r\n",
"M3.Thumb.R: 42.0\r\n"
]
}
],
"source": [
"!biom summarize-table -i tmpmct3i3e_/data/feature-table.biom"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
@ebolyen
Copy link

ebolyen commented Mar 9, 2016

Amazing 👏 👍 🍻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment