-
Notifications
You must be signed in to change notification settings - Fork 14
/
ubiome_example.py
65 lines (40 loc) · 1.71 KB
/
ubiome_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# ubiome_example: a simple Python script showing how to use the ubiome Python library
# makes a single CSV file where each column is a unique sample
from ubiome import UbiomeSample, UbiomeDiffSample, UbiomeMultiSample
my_sample = UbiomeSample()
my_sample.load("ubiome/testdata/sample1.json")
print(my_sample) # useful for debugging
import os
def ubiome_tax_filenames (walk_dir,site="gut"):
"""
Returns a list of all files in walk_dir that are type JSON
:param walk_dir: str
:param site: str
:return: list
"""
tax_filenames = []
for root, subdirs, files in os.walk(walk_dir):
# print('--\nroot = ' + root)
# list_file_path = os.path.join(root, 'my-directory-list.txt')
# print('list_file_path = ' + list_file_path)
# with open(list_file_path) as list_file:
# for subdir in subdirs:
# print('\t- subdirectory ' + subdir)
for filename in files:
file_path = os.path.join(root, filename)
fname, file_extension = os.path.splitext(file_path)
if file_extension == ".json" : # and "gut" in fname:
# print('\t- file %s (extension: %s)' % (filename, file_extension))
# print("file:",file_path)
tax_filenames.append(file_path)
return tax_filenames
test_dir = "./ubiome/testdata/"
all_JSON_files = ubiome_tax_filenames(test_dir)
first_sample = UbiomeSample(name=all_JSON_files[0])
first_sample.load(all_JSON_files[0])
all_samples = UbiomeMultiSample(first_sample)
for sample_file in all_JSON_files[1:]:
next_sample = UbiomeSample(name=sample_file)
next_sample.load(sample_file)
all_samples.merge(next_sample)
all_samples.write("example.csv")