Alignment mask API raise error while the command line version works

Hi all,

I tried the following code to generate masked sequence for phylogenetic diversity analysis.

import qiime2
from qiime2.plugins.alignment.methods import mafft, mask

rep_seqs_path = './pilot_qza/gutxy_pilot_seqs.qza'

# feat_table = qiime2.Artifact.load(feat_table_path)
seqs = qiime2.Artifact.load(rep_seqs_path)
aligned_seqs = mafft(seqs, n_threads=-1)
aligned_seqs.alignment.save('./aligned_seqs_pilot.qza')
print(aligned_seqs.alignment.type)
# FeatureData[AlignedSequence]

The code ran successfully, but I got error calling the mask API

mask(aligned_seqs)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-fdf4104de9a9> in <module>()
----> 1 mask(aligned_seqs)

<decorator-gen-358> in mask(alignment, max_gap_frequency, min_conservation)

~/anaconda3/envs/qiime2-2017.10/lib/python3.5/site-packages/qiime2/sdk/action.py in bound_callable(*args, **kwargs)
    194 
    195                 # Type management
--> 196                 self.signature.check_types(**user_input)
    197                 output_types = self.signature.solve_output(**user_input)
    198                 callable_args = {}

~/anaconda3/envs/qiime2-2017.10/lib/python3.5/site-packages/qiime2/core/type/signature.py in check_types(self, **kwargs)
    297                         kwargs[name] is None):
    298                     raise TypeError("Argument to parameter %r is not a "
--> 299                                     "subtype of %r." % (name, spec.qiime_type))
    300 
    301     def solve_output(self, **input_types):

TypeError: Argument to parameter 'alignment' is not a subtype of FeatureData[AlignedSequence].

For debugging, I tried to use mask with command line

qiime alignment mask --i-alignment aligned_seqs_pilot.qza --output-dir ./tmp_mask

Saved FeatureData[AlignedSequence] to: ./tmp_mask/masked_alignment.qza

Using exact the same data, and it was successful.
I am not quite sure whether I used the API wrong or the document missed some preprocessing of the sequence to be masked?

Any information will be appreciated.

By the way, is there any way to trace how the command line translated to APIs? I guess it will be helpful for debugging (for a Python developer’s perspective).

Spencer

Hi @spencerimp!

When you call an action via the Artifact API, you always get back a Results object which is basically a named-tuple with a pretty repr. This is handy when actions return multiple outputs, but can be a bit tedious when they only have a single output.

You can write something like this to get the artifact:

mask(aligned_seqs[0])
- or -
mask(aligned_seqs.alignment)
- or - 
aligned_seqs, = mafft(seqs, n_threads=-1)  # comma unpacks a tuple of size 1

Since it looks like you are running this in IPython in some way, I would definitely recommend just repr-ing most of the QIIME 2 objects. They should pretty much always give you something to start with.

To clarify, are you asking how the CLI uses the framework?
(This would be an awesome topic for Developer Discussion)

Or are you asking if there is something that logs the stack-trace even when successful?
(This happens automatically on failure)

1 Like

Hi @ebolyen,

Thanks for pointing out that Artifact API always returns a Result object.

I want to share my experience:

I sometimes unpack them to get the value we want, sometimes forget to do that and just follow the document.

For example,

https://docs.qiime2.org/2017.10/plugins/available/alignment/mafft/
It says

Returns
-------
alignment : FeatureData[AlignedSequence]
    The aligned sequences.

So I thought this API would unpack the Result and only return an object of FeatureData[AlignedSequence], but I was wrong.

I will always try to the unpack return named tuple.
For my second question, I meant I want to only how the cli uses the framework, and will ask this in the Developer Discussion once I can squeeze some time. :slight_smile:

Thanks again for the help!
Spencer

1 Like

Thanks @spencerimp! We have an outstanding issue specifically related to this documentation issue — the proposed change in that issue should help make this more clear in the future! Thank for writing!! :t_rex:

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.