-
Notifications
You must be signed in to change notification settings - Fork 1
/
filter_possible_contaminants.py
48 lines (38 loc) · 1.58 KB
/
filter_possible_contaminants.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
import pandas as pd
import sys
def filter_contaminants(input_file, output_file):
# Load the data
data = pd.read_csv(input_file)
# Extract the date from 'Sample_id'
data["Date"] = data["Sample_id"].apply(lambda x: x.split("_")[0])
# Identify numeric columns (those that can be converted to float)
numeric_cols = []
for col in data.columns:
if col not in ["Sample_id", "Date"]:
try:
data[col] = pd.to_numeric(data[col], errors="raise")
numeric_cols.append(col)
except ValueError:
print(
f"Column {col} cannot be converted to numeric and will be ignored."
)
# Group by 'Date' and sum up the counts for each taxon
taxa_by_date = data.groupby("Date")[numeric_cols].sum()
# Identify taxa that appear exclusively on a specific date and are absent on others
exclusive_taxa = (taxa_by_date > 0).sum(axis=0) == 1 # Taxa found on only one date
taxa_to_remove = exclusive_taxa[
exclusive_taxa
].index.tolist() # List of taxa to remove
# Filter out the identified contaminants
filtered_data = data.drop(columns=taxa_to_remove + ["Date"])
# Save the filtered data
filtered_data.to_csv(output_file, index=False)
if __name__ == "__main__":
if len(sys.argv) != 3:
print(
"Usage: python filter_possible_contaminants.py {PATH_TO_INPUT_FILE} {PATH_TO_FILTERED_FILE}"
)
else:
input_path = sys.argv[1]
output_path = sys.argv[2]
filter_contaminants(input_path, output_path)