-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_1_filtertable.py
95 lines (65 loc) · 2.57 KB
/
script_1_filtertable.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import os
import pandas as pd
import readline
import sys
import functions.microdir as md
import functions.microimport as mi
import functions.microfim as mf
""" This script can be used to filter your otu/taxa/esv table based on a list of samples
(metadata file including only the samples you want to analyse).
Files required and mandatory instructions:
* otu/esv/taxa table - the column name of OTU or TAXA must be '#ID'
* sample list - the first row of your sample list must be '#SampleID'
The script will ask you to set the input directory and the two files mentioned -
otu/esv/taxa table and sample list. The format of the file does not matter at this stage,
the script will ask you the type of separator.
The output file will be a filtered CSV file saved into the input directory.
"""
# set autcompletion
readline.set_completer_delims(' \t\n=')
readline.parse_and_bind("tab: complete")
# set dir
data_dir = input('Set your project directory:\n')
print(f'> You entered: {data_dir}\n\n')
data_dir = md.set_inputs_dir_rev(data_dir)
print('Project directory:', '\n\n', data_dir)
print('\n\n')
# list files of inputs dir
files = os.listdir(data_dir)
print('Here is the list of files in the project directory: \n')
print(files, '\n\n')
# set input dir and autcompletion
os.chdir(data_dir)
readline.set_completer_delims(' \t\n=')
readline.parse_and_bind("tab: complete")
# filtering options
# import sample list
metadata = input('Insert the list of samples to filter your table (use also metadata file):\n')
print(f'> You entered: {metadata}\n\n')
# sep
meta_sep = input('Declare the column separate of your metadata file:\n \
E.g. , for commas\n')
print(f'> You entered: {meta_sep}\n\n')
# import taxa/otu_table
data_table = input('Insert your data table:\n')
print(f'> You entered: {data_table}\n\n')
# sep
data_sep = input('Declare the column separate of your otu/taxa/esv table:\n \
E.g. , for commas\n')
print(f'> You entered: {data_sep}\n\n')
# IMPORT FILES
## import metadata
metadata = mi.import_metadata(metadata, data_dir, meta_sep)
print(metadata)
## import data table (otu, esv or taxa table)
data_table = mi.import_data_table(data_table, data_dir, data_sep)
print(data_table)
# FILTER DATA TABLE VIA SAMPLE METADATA
filter_table = mf.filter_data_table(metadata, data_table)
print(filter_table)
# write file
file_name = input('Insert your output file name (without extensions):\n')
print(f'> You entered: {file_name}\n\n')
# save as csv
filter_table.to_csv(os.path.join(data_dir, file_name + '.csv'), index=False)
print('File filtered and saved as ' + file_name + '.csv' + ' in inputs dir!\n\n')