Prepare the environments with some imports
from qiime.sdk.workflow import WorkflowTemplate
from qiime.sdk.type.primitive import Int
from feature_table import rarefy
from feature_table.artifact_types.feature_table import FeatureTable, Frequency
Create an artifact to work with.
import biom
from qiime.sdk.artifact import Artifact
biom_path = "/Users/caporaso/code/q2d2/example-data/keyboard/q191/otu-table.tsv"
table = biom.load_table(biom_path)
Artifact.save(table, FeatureTable[Frequency], "nothing...", "./table.qtf")
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).
wt = WorkflowTemplate.from_function(function=rarefy,
inputs={'table': FeatureTable[Frequency], 'depth': Int},
outputs={'rarefied_table': FeatureTable[Frequency]},
name="Rarefaction",
doc="Let's rarefy!")
Instantiate the context. Note that it discovers its artifacts.
from qiime.q2d3.context import Q2D3Context
q2d3 = Q2D3Context('./')
q2d3.data
{UUID('3e8e39d1-f2b2-4428-b6e9-bd618093854e'): './table.qtf'}
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.
print(q2d3(wt, {'table': list(q2d3.data.keys())[0]}, {'depth': 42}))
```python >>> from qiime.sdk.artifact import Artifact >>> table = Artifact('./table.qtf').data >>> depth = 42 ``` Let's rarefy! ```python >>> from feature_table._normalize import rarefy >>> rarefied_table = rarefy(depth=depth, table=table) ``` ```python >>> from feature_table.artifact_types.feature_table import Frequency >>> from feature_table.artifact_types.feature_table import FeatureTable >>> Artifact.save(rarefied_table, FeatureTable[Frequency], None, '/Users/caporaso/temp/q2d3-test/tmpmct3i3e_.qtf') ```
Now, restart the kernel (see that the cell counts start over) and confirm that the job code does actually work.
from qiime.sdk.artifact import Artifact
table = Artifact('./table.qtf').data
depth = 42
from feature_table._normalize import rarefy
rarefied_table = rarefy(table=table, depth=depth)
from feature_table.artifact_types.feature_table import Frequency
from feature_table.artifact_types.feature_table import FeatureTable
Artifact.save(rarefied_table, FeatureTable[Frequency], None, '/Users/caporaso/temp/q2d3-test/tmpmct3i3e_.qtf')
!tar -xvf '/Users/caporaso/temp/q2d3-test/tmpmct3i3e_.qtf'
x tmpmct3i3e_/ x tmpmct3i3e_/metadata.yaml x tmpmct3i3e_/README.md x tmpmct3i3e_/data/ x tmpmct3i3e_/data/feature-table.biom
The table is rarefied!
!biom summarize-table -i tmpmct3i3e_/data/feature-table.biom
Num samples: 104 Num observations: 340 Total count: 4368 Table density (fraction of non-zero values): 0.045 Counts/sample summary: Min: 42.0 Max: 42.0 Median: 42.000 Mean: 42.000 Std. dev.: 0.000 Sample Metadata Categories: None provided Observation Metadata Categories: taxonomy Counts/sample detail: M9.Pinky.R: 42.0 K1.Shift.L: 42.0 K3.G: 42.0 M9.Pinky.L: 42.0 K2.F: 42.0 K3.Space: 42.0 K3.Shift.L: 42.0 K2.X: 42.0 K3.P: 42.0 K2.C: 42.0 M9.Index.L: 42.0 M2.Middle.R: 42.0 K1.G: 42.0 K3.O: 42.0 K3.Enter: 42.0 K2.P: 42.0 M9.Thumb.L: 42.0 M3.Index.L: 42.0 K1.W: 42.0 K3.J: 42.0 K1.Y: 42.0 K1.V: 42.0 M2.Index.R: 42.0 M3.Ring.L: 42.0 K3.T: 42.0 M2.Pinky.L: 42.0 K1.R: 42.0 K1.K: 42.0 K3.K: 42.0 K3.F: 42.0 K3.Y: 42.0 K1.H: 42.0 K3.Z: 42.0 K1.C: 42.0 M3.Thumb.L: 42.0 K3.W: 42.0 M2.Thumb.R: 42.0 M9.Index.R: 42.0 M2.Index.L: 42.0 K3.V: 42.0 K3.X: 42.0 K3.I: 42.0 K2.Enter: 42.0 M3.Ring.R: 42.0 K2.O: 42.0 K3.U: 42.0 K2.A: 42.0 K1.P: 42.0 K2.Q: 42.0 K3.Shift.R: 42.0 K1.Space: 42.0 M9.Middle.R: 42.0 M3.Index.R: 42.0 M2.Thumb.L: 42.0 K1.T: 42.0 K1.J: 42.0 K2.S: 42.0 K1.N: 42.0 K1.E: 42.0 K2.D: 42.0 K1.A: 42.0 K3.S: 42.0 K3.R: 42.0 K2.B: 42.0 K3.D: 42.0 K2.K: 42.0 K3.H: 42.0 K3.Q: 42.0 K2.M: 42.0 M3.Pinky.L: 42.0 M9.Middle.L: 42.0 K3.C: 42.0 K2.W: 42.0 K3.M: 42.0 K1.B: 42.0 K1.L: 42.0 K3.L: 42.0 K2.Space: 42.0 M3.Middle.L: 42.0 K3.B: 42.0 K1.Q: 42.0 M3.Pinky.R: 42.0 K2.G: 42.0 M9.Thumb.R: 42.0 M9.Ring.R: 42.0 M9.Ring.L: 42.0 K3.E: 42.0 K1.Shift.R: 42.0 K1.Z: 42.0 M2.Ring.L: 42.0 K3.A: 42.0 M2.Middle.L: 42.0 K2.N: 42.0 K2.V: 42.0 M3.Middle.R: 42.0 M2.Ring.R: 42.0 K1.X: 42.0 K1.M: 42.0 M2.Pinky.R: 42.0 K2.E: 42.0 K3.N: 42.0 K2.H: 42.0 K2.Y: 42.0 M3.Thumb.R: 42.0