Skip to content

Instantly share code, notes, and snippets.

@wasade
Created August 1, 2013 00:51
Show Gist options
  • Save wasade/6127603 to your computer and use it in GitHub Desktop.
Save wasade/6127603 to your computer and use it in GitHub Desktop.
this works. general purpose interface factory functions are possible, and maybe even down to a single interface factory function. all the usage example, param conv, etc, can be described in an external file. what this means is that we describe commands as objects, but the interfaces are created dynamically.
#!/usr/bin/env python
from qcli.command import CLInterface, Command, \
FilterSamplesFromOTUTable, CLOption
usage_examples = [("Abundance filtering (low coverage)","Filter samples with fewer than 150 observations from the otu table.","%prog -i otu_table.biom -o otu_table_no_low_coverage_samples.biom -n 150"),
("Abundance filtering (high coverage)","Filter samples with greater than 149 observations from the otu table.","%prog -i otu_table.biom -o otu_table_no_high_coverage_samples.biom -x 149"),
("Metadata-based filtering (positive)","Filter samples from the table, keeping samples where the value for 'Treatment' in the mapping file is 'Control'","%prog -i otu_table.biom -o otu_table_control_only.biom -m map.txt -s 'Treatment:Control'"),
("Metadata-based filtering (negative)","Filter samples from the table, keeping samples where the value for 'Treatment' in the mapping file is not 'Control'","%prog -i otu_table.biom -o otu_table_not_control.biom -m map.txt -s 'Treatment:*,!Control'"),
("List-based filtering","Filter samples where the id is listed in samples_to_keep.txt","%prog -i otu_table.biom -o otu_table_samples_to_keep.biom --sample_id_fp samples_to_keep.txt")]
param_conv = {'biom-table':{'short-name':'i',
'long-name':'input_fp',
'cl-type':'existing_filepath'},
'min-count':{'short-name':'n',
'long-name':'min_count',
'cl-type':float},
'max-count':{'short-name':'x',
'long-name':'max_count',
'cl-type':float}}
additional_opts = [CLOption(Type='biom-table',
Help='the output otu table',
Name='biom-table',
Required=True,
LongName='output_fp',
CLType='new_filepath',
ShortName='o')]
def cli_factory(cmd, usage_ex, p_conv, add_opts):
class CLIobject(CLInterface):
CommandConstructor = cmd
def _get_param_conf_info(self):
return p_conv
def _get_additional_options(self):
return add_opts
def _get_usage_examples(self):
return usage_ex
return CLIobject
CLFilterSamplesFromOTUTable = cli_factory(FilterSamplesFromOTUTable,
usage_examples,
param_conv, additional_opts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment