本文整理汇总了Python中biom.Table类的典型用法代码示例。如果您正苦于以下问题:Python Table类的具体用法?Python Table怎么用?Python Table使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Table类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_validate_prefix
def test_validate_prefix(self):
httpretty.register_uri(
httpretty.POST,
"https://test_server.com/qiita_db/jobs/job-id/step/")
httpretty.register_uri(
httpretty.GET,
"https://test_server.com/qiita_db/prep_template/1/data",
body='{"data": {"1.S1": {"orig_name": "S1"}, "1.S2": '
'{"orig_name": "S2"}, "1.S3": {"orig_name": "S3"}}}')
fd, biom_fp = mkstemp(suffix=".biom")
close(fd)
data = np.asarray([[0, 0, 1], [1, 3, 42]])
table = Table(data, ['O1', 'O2'], ['S1', 'S2', 'S3'])
with biom_open(biom_fp, 'w') as f:
table.to_hdf5(f, "Test")
self._clean_up_files.append(biom_fp)
self.parameters['files'] = '{"BIOM": ["%s"]}' % biom_fp
obs_success, obs_ainfo, obs_error = validate(
self.qclient, 'job-id', self.parameters, self.out_dir)
exp_biom_fp = join(self.out_dir, basename(biom_fp))
self._clean_up_files.append(exp_biom_fp)
self.assertTrue(obs_success)
self.assertEqual(obs_ainfo, [[None, 'BIOM', [exp_biom_fp, 'biom']]])
self.assertEqual(obs_error, "")
obs_t = load_table(exp_biom_fp)
self.assertItemsEqual(obs_t.ids(), ["1.S1", "1.S2", "1.S3"])
示例2: beta
def beta(table: biom.Table, metric: str,
pseudocount: int=1, n_jobs: int=1)-> skbio.DistanceMatrix:
if not (metric in non_phylogenetic_metrics()):
raise ValueError("Unknown metric: %s" % metric)
counts = table.matrix_data.toarray().T
def aitchison(x, y, **kwds):
return euclidean(clr(x), clr(y))
if metric == 'aitchison':
counts += pseudocount
metric = aitchison
if table.is_empty():
raise ValueError("The provided table object is empty")
sample_ids = table.ids(axis='sample')
return skbio.diversity.beta_diversity(
metric=metric,
counts=counts,
ids=sample_ids,
validate=True,
pairwise_func=sklearn.metrics.pairwise_distances,
n_jobs=n_jobs
)
示例3: main
def main(table_in, table_out, pathways, to_classic):
# setup
table = load_table(table_in)
pathway_dict = get_pathway2kos()
# get set of kos from pathways
pathways_kos = set()
for pathway in pathways:
pathways_kos = pathways_kos | pathway_dict[pathway.strip()[-5:]]
# get selected kos
kos_to_keep = set(table.ids('observation')) & \
pathways_kos
if len(kos_to_keep) == 0:
raise EmptySetERROR('Intersection created empty set')
obs_ids = np.array(list(kos_to_keep))
data = np.empty([len(obs_ids), len(table.ids('sample'))])
for i, obs in enumerate(obs_ids):
data[i] = table.data(obs, 'observation')
# output
new_table = Table(data, obs_ids, table.ids('sample'), type="OTU table")
if to_classic:
# print to tab delimited biom table
f = open(table_out, 'w')
f.write(new_table.to_tsv())
else:
# print json biom table
new_table.to_json("filter_KOs_by_pathway.py", open(table_out, 'w'))
示例4: group
def group(table: biom.Table, axis: str,
metadata: qiime2.CategoricalMetadataColumn, mode: str) -> biom.Table:
if table.is_empty():
raise ValueError("Cannot group an empty table.")
if axis == 'feature':
biom_axis = 'observation'
else:
biom_axis = axis
metadata = _munge_metadata_column(metadata, table.ids(axis=biom_axis),
axis)
grouped_table = table.collapse(
lambda axis_id, _: metadata.get_value(axis_id),
collapse_f=_mode_lookup[mode],
axis=biom_axis,
norm=False,
include_collapsed_metadata=False)
# Reorder axis by first unique appearance of each group value in metadata
# (makes it stable for identity mappings and easier to test)
# TODO use CategoricalMetadataColumn API for retrieving categories/groups,
# when the API exists.
series = metadata.to_series()
return grouped_table.sort_order(series.unique(), axis=biom_axis)
示例5: test_execute_job_error
def test_execute_job_error(self):
# Create a prep template
prep_info = {'SKB8.640193': {'col': 'val1'},
'SKD8.640184': {'col': 'val2'}}
data = {'prep_info': dumps(prep_info),
'study': 1,
'data_type': '16S'}
template = self.qclient.post(
'/apitest/prep_template/', data=data)['prep']
# Create a new validate job
fd, biom_fp = mkstemp(suffix=".biom")
close(fd)
data = np.random.randint(100, size=(2, 2))
table = Table(data, ['O1', 'O2'], ['S1', 'S2'])
with biom_open(biom_fp, 'w') as f:
table.to_hdf5(f, "Test")
data = {'command': dumps(['BIOM type', '2.1.4', 'Validate']),
'parameters': dumps(
{'files': dumps({'biom': [biom_fp]}),
'template': template,
'artifact_type': 'BIOM'}),
'artifact_type': 'BIOM',
'status': 'queued'}
job_id = self.qclient.post(
'/apitest/processing_job/', data=data)['job']
plugin("https://localhost:21174", job_id, self.out_dir)
obs = self._wait_job(job_id)
self.assertEqual(obs, 'error')
示例6: beta_phylogenetic
def beta_phylogenetic(table: biom.Table, phylogeny: skbio.TreeNode,
metric: str, n_jobs: int=1)-> skbio.DistanceMatrix:
if metric not in phylogenetic_metrics():
raise ValueError("Unknown phylogenetic metric: %s" % metric)
if table.is_empty():
raise ValueError("The provided table object is empty")
if n_jobs != 1 and metric == 'weighted_unifrac':
raise ValueError("Weighted UniFrac is not parallelizable")
counts = table.matrix_data.toarray().astype(int).T
sample_ids = table.ids(axis='sample')
feature_ids = table.ids(axis='observation')
try:
results = skbio.diversity.beta_diversity(
metric=metric,
counts=counts,
ids=sample_ids,
otu_ids=feature_ids,
tree=phylogeny,
pairwise_func=sklearn.metrics.pairwise_distances,
n_jobs=n_jobs
)
except skbio.tree.MissingNodeError as e:
message = str(e).replace('otu_ids', 'feature_ids')
message = message.replace('tree', 'phylogeny')
raise skbio.tree.MissingNodeError(message)
return results
示例7: merge
def merge(table1: biom.Table, table2: biom.Table) -> biom.Table:
table1_sids = set(table1.ids(axis='sample'))
table2_sids = set(table2.ids(axis='sample'))
if len(table1_sids & table2_sids) > 0:
raise ValueError('Some samples are present in both tables: %s' %
', '.join(table1_sids & table2_sids))
return table1.merge(table2)
示例8: filter_table
def filter_table(table: biom.Table, tree: skbio.TreeNode) -> biom.Table:
""" Filter table to remove feature ids that are not tip ids in tree
"""
tip_ids = set([t.name for t in tree.tips()])
feature_ids = set(table.ids(axis='observation'))
# ids_to_keep can only include ids that are in table
ids_to_keep = tip_ids & feature_ids
table.filter(ids_to_keep, axis='observation', inplace=True)
return table
示例9: rarefy
def rarefy(table: biom.Table, sampling_depth: int) -> biom.Table:
table = table.subsample(sampling_depth, axis='sample', by_id=False)
if table.is_empty():
raise ValueError('The rarefied table contains no samples or features. '
'Verify your table is valid and that you provided a '
'shallow enough sampling depth.')
return table
示例10: alpha
def alpha(table: biom.Table, metric: str) -> pd.Series:
if metric not in non_phylogenetic_metrics():
raise ValueError("Unknown metric: %s" % metric)
if table.is_empty():
raise ValueError("The provided table object is empty")
counts = table.matrix_data.toarray().astype(int).T
sample_ids = table.ids(axis='sample')
result = skbio.diversity.alpha_diversity(metric=metric, counts=counts,
ids=sample_ids)
result.name = metric
return result
示例11: _1
def _1(data: biom.Table) -> BIOMV100Format:
data = _drop_axis_metadata(data)
ff = BIOMV100Format()
with ff.open() as fh:
fh.write(data.to_json(generated_by=_get_generated_by()))
return ff
示例12: beta
def beta(table: biom.Table, metric: str, n_jobs: int=1)-> skbio.DistanceMatrix:
if metric not in non_phylogenetic_metrics():
raise ValueError("Unknown metric: %s" % metric)
if table.is_empty():
raise ValueError("The provided table object is empty")
counts = table.matrix_data.toarray().astype(int).T
sample_ids = table.ids(axis='sample')
return skbio.diversity.beta_diversity(
metric=metric,
counts=counts,
ids=sample_ids,
pairwise_func=sklearn.metrics.pairwise_distances,
n_jobs=n_jobs
)
示例13: rename_deblur_biom
def rename_deblur_biom(biom, name_stub='deblur', metadata_name='deblurred_seq'):
seqs = biom.ids(axis='observation')
seqnames = ['{0}{1}'.format(name_stub, x) for x in range(len(seqs))]
seq_metadata = {seqname: {metadata_name: seq} for seq, seqname in zip(seqs, seqnames)}
renamed_biom = Table(biom.matrix_data,
seqnames,
biom.ids(axis='sample'),
biom.metadata(axis='observation'),
biom.metadata(axis='sample'),
table_id = biom.table_id + ' renamed')
renamed_biom.add_metadata(seq_metadata, axis='observation')
return(renamed_biom)
示例14: test_collapse_full
def test_collapse_full(self):
obs = collapse_full(table)
exp = Table(array([[0.00769230769231], [0.0282051282051],
[0.0487179487179], [0.0692307692308],
[0.0897435897436], [0.110256410256],
[0.130769230769], [0.151282051282],
[0.171794871795], [0.192307692308]]),
observ_ids, ['average'],
observation_metadata=observ_metadata)
for r in range(10):
assert_almost_equal(obs[r, 0], exp[r, 0])
self.assertEqual(obs.ids(), exp.ids())
self.assertItemsEqual(obs.ids('observation'), exp.ids('observation'))
obs_meta = []
for _, _, m in obs.iter(axis='observation'):
obs_meta.append(m)
self.assertItemsEqual(obs_meta, observ_metadata)
示例15: test_biom_match
def test_biom_match(self):
table = Table(
np.array([[0, 0, 1, 1],
[2, 3, 4, 4],
[5, 5, 3, 3]]).T,
['a', 'b', 'c', 'd'],
['s2', 's3', 's4'])
md = pd.DataFrame(
{
'x1': [1, 3, 2],
'x2': [1, 1, 0]
},
columns=['s1', 's2', 's3']
).T
exp_table = Table(
np.array(
[
[0, 0, 1, 1],
[2, 3, 4, 4]
]).T,
['a', 'b', 'c', 'd'],
['s2', 's3'])
exp_md = pd.DataFrame(
{
'x1': [3, 2],
'x2': [1, 0]
},
columns=['s2', 's3']
).T
res_table, res_md = match(table, md)
exp_df = pd.DataFrame(exp_table.to_dataframe())
res_df = pd.DataFrame(res_table.to_dataframe())
exp_df = exp_df.reindex_axis(sorted(exp_df.columns), axis=1)
res_df = res_df.reindex_axis(sorted(res_df.columns), axis=1)
pdt.assert_frame_equal(exp_df, res_df)
exp_md = exp_md.reindex_axis(sorted(exp_md.index), axis=0)
res_md = res_md.reindex_axis(sorted(res_md.index), axis=0)
pdt.assert_frame_equal(res_md, exp_md)