-
Notifications
You must be signed in to change notification settings - Fork 3
/
taxon_table_converter.py
executable file
·147 lines (108 loc) · 5.83 KB
/
taxon_table_converter.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import PySimpleGUI as sg
import pandas as pd
from pandas import DataFrame
import numpy as np
from pathlib import Path
from taxontabletools.taxontable_manipulation import add_metadata
def taxon_table_converter_ttt(read_table_xlsx, taxonomy_results_xlsx, TaXon_table_name, sheet_name, path_to_outdirs):
if sheet_name == 'APSCALE':
sheet_name = 'Taxonomy table'
## collect both input files
taxonomy_results_xlsx = Path(taxonomy_results_xlsx)
read_table_xlsx = Path(read_table_xlsx)
#ä create filename and path for output file
Output_name = TaXon_table_name + ".xlsx"
Output_file = Path(path_to_outdirs) / "TaXon_tables" / Output_name
## store the file name for later use
file_name = taxonomy_results_xlsx.name
## create dataframes for both files
taxonomy_df = pd.read_excel(taxonomy_results_xlsx, sheet_name, header=0)
if sheet_name == "BOLDigger hit":
metada_df = taxonomy_df[['ID', 'Flags']]
taxonomy_df = taxonomy_df.drop(['Flags'], axis=1)
read_table_df = pd.read_excel(read_table_xlsx, header=0)
## create a new dataframe
TaXon_table_df = taxonomy_df
# check if all OTU are correctly sorted and present in both files
if taxonomy_df["ID"].to_list() == read_table_df["ID"].to_list():
## append the sequences to the TaXon stable
TaXon_table_df["seq"] = read_table_df["Seq"]
## remove the sequence column from the read table
read_table_df.drop('Seq', axis='columns', inplace=True)
## remove the ID column from the read table
read_table_df.drop('ID', axis='columns', inplace=True)
## add samples to the dataframe
TaXon_table_df = pd.concat([TaXon_table_df, read_table_df], axis=1)
## check if species are present as "Genus" + "Epithet"
new_species_column = []
for OTU in TaXon_table_df[["Genus", "Species"]].fillna("nan").values.tolist():
if (OTU != ["nan", "nan"] and OTU[1] != 'nan'):
if OTU[0] not in OTU[1]:
new_species_column.append(OTU[0] + " " + OTU[1])
else:
new_species_column.append(OTU[1])
else:
new_species_column.append("")
## add new species column to the dataframe
TaXon_table_df["Species"] = new_species_column
## add FLAGS
if sheet_name == "BOLDigger hit":
TaXon_table_df = add_metadata(TaXon_table_df, metada_df)
## save the newly created Taxon table in TaXon format as excel file
TaXon_table_df.to_excel(Output_file, sheet_name='TaXon table', index=False)
closing_text = "Taxon table is found under:\n" + '/'.join(str(Output_file).split("/")[-4:])
sg.Popup(closing_text, title="Finished", keep_on_top=True)
from taxontabletools.create_log import ttt_log
input = taxonomy_results_xlsx.name + " + " + read_table_xlsx.name
ttt_log("taXon table converter", "processing", input, Output_file.name, "ttt", path_to_outdirs)
else:
sg.PopupError("Error: The IDs of the read table and taxonomy table do not match!")
def taxon_table_converter_qiime2(read_table_tsv, taxonomy_results_xlsx, TaXon_table_name, sheet_name, path_to_outdirs):
taxonomy_results_xlsx = Path(taxonomy_results_xlsx)
read_table_tsv = Path(read_table_tsv)
# create filename and path for output file
Output_name = TaXon_table_name + ".xlsx"
Output_file = path_to_outdirs / "TaXon_tables" / Output_name
# store the file name for later use
file_name = taxonomy_results_xlsx.name
# create datafrmes for both files
taxonomy_df = pd.read_excel(taxonomy_results_xlsx, sheet_name, header=0)
if sheet_name == "BOLDigger hit":
taxonomy_df = taxonomy_df.drop(columns=['Flags'])
read_table_df = pd.read_csv(Path(read_table_tsv), sep="\t")
# drop the first row
read_table_df = read_table_df.iloc[1:]
read_table_df = read_table_df.reset_index(drop=True)
## create a new dataframe
TaXon_table_df = taxonomy_df
# check if all OTU are correctly sorted and present in both files
if taxonomy_df["ID"].to_list() == read_table_df["id"].to_list():
## append the sequences to the TaXon stable
TaXon_table_df["seq"] = read_table_df["Sequence"].values.tolist()
## remove the sequence column from the read table
read_table_df.drop('Sequence', axis='columns', inplace=True)
## remove the ID column from the read table
read_table_df.drop('id', axis='columns', inplace=True)
## add samples to the dataframe
TaXon_table_df = pd.concat([TaXon_table_df, read_table_df], axis=1)
## check if species are present as "Genus" + "Epithet"
new_species_column = []
for OTU in TaXon_table_df[["Genus", "Species"]].fillna("nan").values.tolist():
if (OTU != ["nan", "nan"] and OTU[1] != 'nan'):
if OTU[0] not in OTU[1]:
new_species_column.append(OTU[0] + " " + OTU[1])
else:
new_species_column.append(OTU[1])
else:
new_species_column.append("")
## add new species column to the dataframe
TaXon_table_df["Species"] = new_species_column
## save the newly created Taxon table in TaXon format as excel file
TaXon_table_df.to_excel(Output_file, sheet_name='TaXon table', index=False)
closing_text = "Taxon table is found under:\n" + '/'.join(str(Output_file).split("/")[-4:])
sg.Popup(closing_text, title="Finished", keep_on_top=True)
from taxontabletools.create_log import ttt_log
input = taxonomy_results_xlsx.name + " + " + read_table_tsv.name
ttt_log("taXon table converter", "processing", input, Output_file.name, "qiime2", path_to_outdirs)
else:
sg.PopupError("Error: The IDs of the read table and taxonomy table do not match!")