本文整理汇总了Python中biom.table.Table类的典型用法代码示例。如果您正苦于以下问题:Python Table类的具体用法?Python Table怎么用?Python Table使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Table类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load_BIOM
def load_BIOM(table, informat='json', v=1):
"""
load a BIOM table from BIOM format. Default format is 'json'.
"""
from biom.table import Table
import json
import sys
informats = ['json','tsv']
if not informat in informats:
print "\nPlease specify a valid BIOM input format. Currently we support: '%s'.\n" %"', '".join(informats)
else:
if v:
print "\nSpecified BIOM input format '%s' - ok!" %(informat)
if informat == 'json':
with open(table) as data_file:
data = json.load(data_file)
t = Table.from_json(data)
elif informat == 'tsv':
tsv = open(in_tsv)
func = lambda x : x
t = Table.from_tsv(tsv, obs_mapping=None, sample_mapping=None, process_func=func)
tsv.close()
return t
示例2: setUp
def setUp(self):
"""define some top-level data"""
self.otu_table_values = array([[0, 0, 9, 5, 3, 1], [1, 5, 4, 0, 3, 2], [2, 3, 1, 1, 2, 5]])
{
(0, 2): 9.0,
(0, 3): 5.0,
(0, 4): 3.0,
(0, 5): 1.0,
(1, 0): 1.0,
(1, 1): 5.0,
(1, 2): 4.0,
(1, 4): 3.0,
(1, 5): 2.0,
(2, 0): 2.0,
(2, 1): 3.0,
(2, 2): 1.0,
(2, 3): 1.0,
(2, 4): 2.0,
(2, 5): 5.0,
}
self.otu_table = Table(
self.otu_table_values,
["OTU1", "OTU2", "OTU3"],
["Sample1", "Sample2", "Sample3", "Sample4", "Sample5", "Sample6"],
[{"taxonomy": ["Bacteria"]}, {"taxonomy": ["Archaea"]}, {"taxonomy": ["Streptococcus"]}],
[None, None, None, None, None, None],
)
self.otu_table_f = Table(
self.otu_table_values,
["OTU1", "OTU2", "OTU3"],
["Sample1", "Sample2", "Sample3", "Sample4", "Sample5", "Sample6"],
[
{"taxonomy": ["1A", "1B", "1C", "Bacteria"]},
{"taxonomy": ["2A", "2B", "2C", "Archaea"]},
{"taxonomy": ["3A", "3B", "3C", "Streptococcus"]},
],
[None, None, None, None, None, None],
)
self.full_lineages = [
["1A", "1B", "1C", "Bacteria"],
["2A", "2B", "2C", "Archaea"],
["3A", "3B", "3C", "Streptococcus"],
]
self.metadata = [
[
["Sample1", "NA", "A"],
["Sample2", "NA", "B"],
["Sample3", "NA", "A"],
["Sample4", "NA", "B"],
["Sample5", "NA", "A"],
["Sample6", "NA", "B"],
],
["SampleID", "CAT1", "CAT2"],
[],
]
self.tree_text = ["('OTU3',('OTU1','OTU2'))"]
fh, self.tmp_heatmap_fpath = mkstemp(prefix="test_heatmap_", suffix=".pdf")
close(fh)
示例3: test_tree_filter_table_none
def test_tree_filter_table_none(self):
rooted_nwk = io.StringIO("(O1:4.5,(O2:4,(a:1,b:1):2):0.5);")
tree = skbio.TreeNode.read(rooted_nwk)
table = Table(np.array([[0, 1, 3], [1, 1, 2]]),
['O1', 'O2'],
['S1', 'S2', 'S3'])
actual = filter_table(table, tree)
expected = table.filter(['O1', 'O2'], axis='observation')
self.assertEqual(actual, expected)
示例4: parse_biom_table
def parse_biom_table(fp, input_is_dense=False):
try:
return Table.from_hdf5(fp)
except:
pass
if hasattr(fp, 'read'):
return Table.from_json(json.load(fp), input_is_dense=input_is_dense)
elif isinstance(fp, list):
return Table.from_json(json.loads(''.join(fp)),
input_is_dense=input_is_dense)
else:
return Table.from_json(json.loads(fp), input_is_dense=input_is_dense)
示例5: BIOM_return_clipped_taxonomy
def BIOM_return_clipped_taxonomy(taxlevel, BIOM):
"""
Returns a BIOM table for which the taxonomy has been clipped at a certain level
"""
from biom.table import Table
import numpy as np
return_OTUs = {}
levels = ['kingdom', 'phylum', 'class', 'order', 'family', 'genus', 'species', 'unassigned']
clip_level=''
to_drop=[]
if not taxlevel in levels:
raise KeyError("The taxonomic level you are trying to search: '%s', is not valid" %level)
clip_level = int(levels.index(taxlevel))+1
#check if the first OTU has 'taxonomy' metadata attached, if yes assume all others have too and resume
if not 'taxonomy' in BIOM.metadata(axis='observation')[0]:
raise KeyError('The BIOM table you are trying to screen does not have taxonomy metadata attached to it')
else:
print "Found taxonomy metadata with OTUs - ok!"
sample_ids = BIOM.ids(axis='sample')
observation_ids = BIOM.ids(axis='observation')
data_to_biom = []
sample_metadata = BIOM.metadata(axis='sample')
observation_metadata = BIOM.metadata(axis='observation')
for OTU in observation_ids:
orig=BIOM.data(OTU, axis='observation')
data_to_biom.append(orig)
data = np.asarray(data_to_biom)
for i in range(len(observation_metadata)):
if len(observation_metadata[i]['taxonomy']) > clip_level:
observation_metadata[i]['taxonomy'] = observation_metadata[i]['taxonomy'][:clip_level]
if 'unknown' in observation_metadata[i]['taxonomy'][-1]:
print "fishy: %s" %observation_metadata[i]['taxonomy']
to_drop.append(observation_ids[i])
# print observation_metadata[i]['taxonomy']
#construct adjusted table
outtable = Table(data, observation_ids, sample_ids, table_id='OTU table', sample_metadata=sample_metadata, observation_metadata=observation_metadata)
if to_drop:
outtable.filter(to_drop, invert=True, axis='observation',inplace=True)
return outtable
示例6: setUp
def setUp(self):
self.otu_table_vals = array([[1, 0, 2, 4],
[1, 2, 0, 1],
[0, 1, 1, 0],
[1, 2, 1, 0]])
self.otu_table = Table(self.otu_table_vals,
['0', '1', '2', '3'],
['s1', 's2', 's3', 's4'],
[{"taxonomy": ["Root", "Bacteria", "Actinobacteria", "Actinobacteria", "Coriobacteridae", "Coriobacteriales", "Coriobacterineae", "Coriobacteriaceae"]},
{"taxonomy": ["Root",
"Bacteria",
"Firmicutes",
"\"Clostridia\""]},
{"taxonomy": ["Root",
"Bacteria",
"Firmicutes",
"\"Clostridia\""]},
{"taxonomy": ["Root", "Bacteria"]}],
None,)
self.mapping = """#SampleID\tBarcodeSequence\tTreatment\tDescription
#Test mapping file
s1\tAAAA\tControl\tControl mouse, I.D. 354
s2\tGGGG\tControl\tControl mouse, I.D. 355
s3\tCCCC\tExp\tDisease mouse, I.D. 356
s4\tTTTT\tExp\tDisease mouse, I.D. 357""".split('\n')
示例7: BIOM_tsv_to_R_transpose
def BIOM_tsv_to_R_transpose(in_tsv, out_csv):
"""
Parse a biom table in tsv format and transpose it for input into R
"""
from biom import Table
tsv = open(in_tsv)
#in_tsv = open('COI-trim30min100-merge-c3-id97-OTU-taxonomy.kraken.tsv')
func = lambda x : x
intable = Table.from_tsv(tsv,obs_mapping=None, sample_mapping=None, process_func=func)
outtable = intable.transpose()
out=open("transposed.tsv","w")
out.write(outtable.to_tsv(header_key=None, header_value=None))
out.close()
#refine
intable = open('transposed.tsv','r')
temp = intable.next()
out=''
for line in intable:
if line.startswith('#'):
if line.strip().endswith('taxomomy'):
print "Removing taxonomy"
line = ",".join(line.strip().split("\t")[:-1]).replace('#OTU ID','Sample').replace('\t',',')+'\n'
line = line.replace('#OTU ID','Sample').replace('\t',',')
out+=line
else:
line = line.replace('\t',',')
out+=line
outtable = open(out_csv,'w')
outtable.write(out)
outtable.close()
示例8: run
def run(self, **kwargs):
json_table_str = kwargs['json_table_str']
hdf5_biom = kwargs['hdf5_table']
axis = kwargs['axis']
ids = kwargs['ids']
if axis not in self.Axes:
raise CommandError("Invalid axis '%s'. Must be either %s." % (
axis,
' or '.join(map(lambda e: "'%s'" % e, self.Axes))))
if hdf5_biom is None and json_table_str is None:
raise CommandError("Must specify an input table")
elif hdf5_biom is not None and json_table_str is not None:
raise CommandError("Can only specify one input table")
if json_table_str is not None:
idxs, new_axis_md = get_axis_indices(json_table_str, ids, axis)
new_data = direct_slice_data(json_table_str, idxs, axis)
# multiple walks over the string. bad form, but easy right now
# ...should add a yield_and_ignore parser or something.
def subset_generator():
yield "{"
yield direct_parse_key(json_table_str, "id")
yield ","
yield direct_parse_key(json_table_str, "format")
yield ","
yield direct_parse_key(json_table_str, "format_url")
yield ","
yield direct_parse_key(json_table_str, "type")
yield ","
yield direct_parse_key(json_table_str, "generated_by")
yield ","
yield direct_parse_key(json_table_str, "date")
yield ","
yield direct_parse_key(json_table_str, "matrix_type")
yield ","
yield direct_parse_key(json_table_str, "matrix_element_type")
yield ","
yield new_data
yield ","
yield new_axis_md
yield ","
if axis == "observation":
yield direct_parse_key(json_table_str, "columns")
else:
yield direct_parse_key(json_table_str, "rows")
yield "}"
format_ = 'json'
table = subset_generator()
else:
with biom_open(hdf5_biom) as f:
table = Table.from_hdf5(f, ids=ids, axis=axis)
format_ = 'hdf5'
return {'subsetted_table': (table, format_)}
示例9: filter_BIOM_by_per_sample_read_prop
def filter_BIOM_by_per_sample_read_prop(BIOM, min_prop=0.01):
"""
Filter OTU table by mininimum reads per sample
"""
import numpy as np
from biom.table import Table
print "\nFiltering at level: %s %%\n" %(min_prop*100)
# print "input table:\n"
# print BIOM
# print "\n"
sample_ids = BIOM.ids(axis='sample')
observation_ids = BIOM.ids(axis='observation')
data_to_biom = []
sample_metadata = BIOM.metadata(axis='sample')
observation_metadata = BIOM.metadata(axis='observation')
sums = BIOM.sum(axis='sample')
for OTU in observation_ids:
orig=BIOM.data(OTU, axis='observation')
for i in range(len(orig)):
if not int(orig[i]) == 0:
if not int(orig[i]) >= sums[i]*min_prop:
orig[i] = '0.0'
data_to_biom.append(orig)
data = np.asarray(data_to_biom)
#construct adjusted table
table = Table(data, observation_ids, sample_ids, table_id='OTU table', sample_metadata=sample_metadata, observation_metadata=observation_metadata)
#Filter OTUs with sum = '0'
to_exclude = []
observation_sums = table.sum(axis='observation')
for i in range(len(observation_sums)):
if int(observation_sums[i]) == 0:
to_exclude.append(observation_ids[i])
print "Removing %i OTUs for lack of support\n" %len(to_exclude)
table.filter(to_exclude, invert=True, axis='observation',inplace=True)
# print table
return table
示例10: main
def main(table_loc, otu_list, collapsed_name, output_file, classic=False):
table = load_table(table_loc)
f = open(otu_list)
otus = f.read().strip().split()
otus = set(otus) & set(table.ids(axis="observation"))
table1 = table.filter(otus, axis="observation", inplace=False)
table2 = table.filter(otus, axis="observation", invert=True, inplace=False)
sums1 = table1.sum(axis='sample')
sums2 = table2.sum(axis='sample')
new_table = Table(numpy.array([sums1,sums2]), [collapsed_name, "not_"+collapsed_name], table.ids(axis="sample"), type="otu baptable")
if classic:
# print to tab delimited biom table
open(output_file, 'w').write(new_table.to_tsv())
else:
# print biom table
new_table.to_json("predict_reactions.py", open(output_file, 'w'))
示例11: build_OTU_table_biom
def build_OTU_table_biom(OTU_table_classic, OTU_table_biom, dataset_ID):
# Builds a BIOM format OTU table from an OTU table in classic dense format (sample IDs in the first row, OTU IDs in the first column). For some reason, 'biom convert' command fails to recognize some OTU tables, and therefore the method classic2biom (above) fails.
with open(OTU_table_classic,'r') as fidin:
otu_table_data = fidin.readlines()
firstrow = otu_table_data[0].split('\t')
sample_labels = firstrow[1:]
sample_labels[len(sample_labels)-1] = sample_labels[len(sample_labels)-1].rstrip('\n')
OTU_labels = [otu_table_data[i].split('\t')[0] for i in range(1,len(otu_table_data))]
nOTUs = len(OTU_labels)
nSamples = len(sample_labels)
# Load OTU table row major order
OTU_table_data = np.zeros((nOTUs, nSamples))
for i in range(1,nOTUs+1):
OTU_table_data[i-1,:] = otu_table_data[i].split('\t')[1:]
# Write in BIOM format
t = Table(OTU_table_data, OTU_labels, sample_labels, observ_metadata=None, sample_metadata=None, table_id=dataset_ID)
with biom_open(OTU_table_biom, 'w') as f:
t.to_hdf5(f, "Generated by processing layer", compress=False)
示例12: setUp
def setUp(self):
"""define some top-level data"""
self.otu_table_values = array([[0, 0, 9, 5, 3, 1],
[1, 5, 4, 0, 3, 2],
[2, 3, 1, 1, 2, 5]])
{(0, 2): 9.0, (0, 3): 5.0, (0, 4): 3.0, (0, 5): 1.0,
(1, 0): 1.0, (1, 1): 5.0, (1, 2): 4.0, (1, 4): 3.0, (1, 5): 2.0,
(2, 0): 2.0, (2, 1): 3.0, (2, 2): 1.0, (2, 3): 1.0, (2, 4): 2.0, (2, 5): 5.0}
self.otu_table = Table(self.otu_table_values,
['OTU1', 'OTU2', 'OTU3'],
['Sample1', 'Sample2', 'Sample3',
'Sample4', 'Sample5', 'Sample6'],
[{"taxonomy": ['Bacteria']},
{"taxonomy": ['Archaea']},
{"taxonomy": ['Streptococcus']}],
[None, None, None, None, None, None])
self.otu_table_f = Table(self.otu_table_values,
['OTU1', 'OTU2', 'OTU3'],
['Sample1', 'Sample2', 'Sample3',
'Sample4', 'Sample5', 'Sample6'],
[{"taxonomy": ['1A', '1B', '1C', 'Bacteria']},
{"taxonomy":
['2A', '2B', '2C', 'Archaea']},
{"taxonomy": ['3A', '3B', '3C', 'Streptococcus']}],
[None, None, None, None, None, None])
self.full_lineages = [['1A', '1B', '1C', 'Bacteria'],
['2A', '2B', '2C', 'Archaea'],
['3A', '3B', '3C', 'Streptococcus']]
self.metadata = [[['Sample1', 'NA', 'A'],
['Sample2', 'NA', 'B'],
['Sample3', 'NA', 'A'],
['Sample4', 'NA', 'B'],
['Sample5', 'NA', 'A'],
['Sample6', 'NA', 'B']],
['SampleID', 'CAT1', 'CAT2'], []]
self.tree_text = ["('OTU3',('OTU1','OTU2'))"]
fh, self.tmp_heatmap_fpath = mkstemp(prefix='test_heatmap_',
suffix='.pdf')
close(fh)
示例13: _subset_table
def _subset_table(hdf5_biom, json_table_str, axis, ids):
if axis not in ['sample', 'observation']:
raise ValueError("Invalid axis '%s'. Must be either 'sample' or "
"'observation'." % axis)
if hdf5_biom is None and json_table_str is None:
raise ValueError("Must specify an input table")
elif hdf5_biom is not None and json_table_str is not None:
raise ValueError("Can only specify one input table")
if json_table_str is not None:
idxs, new_axis_md = get_axis_indices(json_table_str, ids, axis)
new_data = direct_slice_data(json_table_str, idxs, axis)
# multiple walks over the string. bad form, but easy right now
# ...should add a yield_and_ignore parser or something.
def subset_generator():
yield "{"
yield direct_parse_key(json_table_str, "id")
yield ","
yield direct_parse_key(json_table_str, "format")
yield ","
yield direct_parse_key(json_table_str, "format_url")
yield ","
yield direct_parse_key(json_table_str, "type")
yield ","
yield direct_parse_key(json_table_str, "generated_by")
yield ","
yield direct_parse_key(json_table_str, "date")
yield ","
yield direct_parse_key(json_table_str, "matrix_type")
yield ","
yield direct_parse_key(json_table_str, "matrix_element_type")
yield ","
yield new_data
yield ","
yield new_axis_md
yield ","
if axis == "observation":
yield direct_parse_key(json_table_str, "columns")
else:
yield direct_parse_key(json_table_str, "rows")
yield "}"
format_ = 'json'
table = subset_generator()
else:
with biom_open(hdf5_biom) as f:
table = Table.from_hdf5(f, ids=ids, axis=axis)
format_ = 'hdf5'
return table, format_
示例14: convert_table_to_biom
def convert_table_to_biom(table_f, sample_mapping, obs_mapping,
process_func, **kwargs):
"""Convert a contigency table to a biom table
sample_mapping : dict of {'sample_id':metadata} or None
obs_mapping : dict of {'obs_id':metadata} or None
process_func: a function to transform observation metadata
dtype : type of table data
"""
otu_table = Table.from_tsv(table_f, obs_mapping, sample_mapping,
process_func, **kwargs)
return otu_table.to_json(generatedby())
示例15: test_rarefy_to_files
def test_rarefy_to_files(self):
"""rarefy_to_files should write valid files
"""
maker = RarefactionMaker(self.otu_table_fp, 0, 1, 1, 1)
maker.rarefy_to_files(
self.rare_dir,
include_full=True,
include_lineages=False)
fname = os.path.join(self.rare_dir, "rarefaction_1_0.biom")
with biom_open(fname, 'U') as biom_file:
otu_table = Table.from_hdf5(biom_file)
self.assertItemsEqual(
otu_table.sample_ids,
self.otu_table.sample_ids[:2])