-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLichen_Data_Parser.py
More file actions
257 lines (218 loc) · 10.8 KB
/
Lichen_Data_Parser.py
File metadata and controls
257 lines (218 loc) · 10.8 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# lichen_file = "testfile3.txt"
def unique_finder(filename):
unique_species = []
unique_choice = input("Which would you like to find unique instances of? Type F for families, G for genera, S for species: ")
if unique_choice.lower() == "s":
family_col = input("What column is the taxonomic family in? ")
family_col = int(family_col) - 1
genus_col = input("What column is the taxonomic genus in? ")
genus_col = int(genus_col) - 1
species_col = input("What column is the taxonomic species in? ")
species_col = int(species_col) - 1
with open(filename) as lichens:
#header = lichens.readline()
for line in lichens:
lines = line.rstrip().split("\t")
if f"{lines[family_col].lower()} {lines[genus_col].lower()} {lines[species_col].lower()}" not in unique_species:
unique_species.append(f"{lines[family_col].lower()} {lines[genus_col].lower()} {lines[species_col].lower()}")
elif unique_choice.lower() == 'f':
family_col = input("What column is the taxonomic family in? ")
family_col = int(family_col) - 1
with open(filename) as lichens:
#header = lichens.readline()
for line in lichens:
lines = line.rstrip().split("\t")
if f"{lines[family_col].lower()}" not in unique_species:
unique_species.append(f"{lines[family_col].lower()}")
elif unique_choice.lower() == 'g':
family_col = input("What column is the taxonomic family in? ")
family_col = int(family_col) - 1
genus_col = input("What column is the taxonomic genus in? ")
genus_col = int(genus_col) - 1
with open(filename) as lichens:
#header = lichens.readline()
for line in lichens:
lines = line.rstrip().split("\t")
if f"{lines[family_col].lower()} {lines[genus_col].lower()}" not in unique_species:
unique_species.append(f"{lines[family_col].lower()} {lines[genus_col].lower()}")
print(len(unique_species))
def finder_98(filename):
heading_list = []
above_98_d = {}
line_count = 0
blast_id_column = int(input("What column is the BLAST Percent ID in? ")) - 1
family_col = int(input("What column is the family assignment in? ")) - 1
genus_col = int(input("What column is the genus assignment in? ")) - 1
species_col = int(input("What column is the species assignment in? ")) - 1
with open(filename) as lichens:
header = lichens.readline()
header = header.rstrip().split("\t")
for heading in header:
above_98_d[heading] = []
heading_list.append(heading)
for line in lichens:
lines = line.rstrip().split("\t")
line_count += 1
if lines[blast_id_column] == '':
continue
elif lines[blast_id_column] == "multi-identity":
continue
elif float(lines[blast_id_column]) >= 98.0:
for i, item in enumerate(lines):
if item != "0":
if f"{lines[family_col]} {lines[genus_col]} {lines[species_col]}" not in above_98_d[heading_list[i]]:
above_98_d[heading_list[i]].append(f"{lines[family_col]} {lines[genus_col]} {lines[species_col]}")
above_98 = {}
for head, value, in above_98_d.items():
above_98[head] = len(value)
print(line_count)
return above_98
def finder_985(filename):
heading_list = []
above_985_d = {}
blast_id_column = int(input("What column is the BLAST Percent ID in? ")) - 1
family_col = int(input("What column is the family assignment in? ")) - 1
genus_col = int(input("What column is the genus assignment in? ")) - 1
species_col = int(input("What column is the species assignment in? ")) - 1
with open(filename) as lichens:
header = lichens.readline()
header = header.rstrip().split("\t")
for heading in header:
above_985_d[heading] = []
heading_list.append(heading)
for line in lichens:
lines = line.rstrip().split("\t")
if lines[blast_id_column] == '':
continue
elif lines[blast_id_column] == "multi-identity":
continue
elif float(lines[blast_id_column]) >= 98.5:
for i, item in enumerate(lines):
if item != "0":
if f"{lines[family_col]} {lines[genus_col]} {lines[species_col]}" not in above_985_d[heading_list[i]]:
above_985_d[heading_list[i]].append(f"{lines[family_col]} {lines[genus_col]} {lines[species_col]}")
above_985 = {}
for head, value, in above_985_d.items():
above_985[head] = len(value)
return above_985
def finder_regional(filename):
heading_list = []
regional_d = {}
# species_list = []
library_col = int(input("What column is the assessment of the regional database in? ")) - 1
family_col = int(input("What column is the family assignment in? ")) - 1
genus_col = int(input("What column is the genus assignment in? ")) - 1
species_col = int(input("What column is the species assignment in? ")) - 1
with open(filename) as regional:
header = regional.readline()
header = header.rstrip().split("\t")
for heading in header:
regional_d[heading] = []
heading_list.append(heading)
for line in regional:
lines = line.rstrip().split("\t")
if lines[library_col] == "yes":
for i, item in enumerate(lines):
if item != "0":
if f"{lines[family_col]} {lines[genus_col]} {lines[species_col]}" not in regional_d[heading_list[i]]:
regional_d[heading_list[i]].append(f"{lines[family_col]} {lines[genus_col]} {lines[species_col]}")
else:
continue
regional_lengths = {}
for head, value, in regional_d.items():
regional_lengths[head] = len(value)
return regional_lengths
def family_number_finder_98(filename):
family_d = {}
family_col = int(input("What column is the family assignment in? ")) - 1
blast_id_column = int(input("What column is the BLAST ID in? ")) - 1
with open(filename) as families:
header = families.readline()
for line in families:
lines = line.rstrip().split("\t")
if lines[blast_id_column] == '':
continue
elif lines[blast_id_column] == "multi-identity":
continue
elif float(lines[blast_id_column]) >= 98.0:
if lines[family_col].lower() not in family_d:
family_d[lines[family_col].lower()] = []
family_d[lines[family_col].lower()].append(f"{lines[family_col+1]} {lines[family_col+2]}")
else:
if f"{lines[family_col+1]} {lines[family_col+2]}" not in family_d[lines[family_col].lower()]:
family_d[lines[family_col].lower()].append(f"{lines[family_col+1]} {lines[family_col+2]}")
else:
continue
family_lengths = {}
for head, value, in family_d.items():
family_lengths[head] = len(value)
return family_lengths
def family_number_finder_985(filename):
family_d = {}
family_col = int(input("What column is the family assignment in? ")) - 1
blast_id_column = int(input("What column is the BLAST ID in? ")) - 1
with open(filename) as families:
header = families.readline()
for line in families:
lines = line.rstrip().split("\t")
if lines[blast_id_column] == '':
continue
elif lines[blast_id_column] == "multi-identity":
continue
elif float(lines[blast_id_column]) >= 98.5:
if lines[family_col].lower() not in family_d:
family_d[lines[family_col].lower()] = []
family_d[lines[family_col].lower()].append(f"{lines[family_col+1]} {lines[family_col+2]}")
else:
if f"{lines[family_col+1]} {lines[family_col+2]}" not in family_d[lines[family_col].lower()]:
family_d[lines[family_col].lower()].append(f"{lines[family_col+1]} {lines[family_col+2]}")
else:
continue
family_lengths = {}
for head, value, in family_d.items():
family_lengths[head] = len(value)
return family_lengths
def regional_family_finder(filename):
family_d = {}
family_col = int(input("What column is the family assignment in? ")) - 1
id_col = int(input("What column is the database assessment in? ")) - 1
with open(filename) as families:
header = families.readline()
for line in families:
lines = line.rstrip().split("\t")
if lines[id_col] == "yes":
if lines[family_col].lower() not in family_d:
family_d[lines[family_col].lower()] = []
family_d[lines[family_col].lower()].append(f"{lines[family_col + 1]} {lines[family_col + 2]}")
else:
if f"{lines[family_col + 1]} {lines[family_col + 2]}" not in family_d[lines[family_col].lower()]:
family_d[lines[family_col].lower()].append(f"{lines[family_col + 1]} {lines[family_col + 2]}")
else:
continue
family_lengths = {}
for head, value, in family_d.items():
family_lengths[head] = len(value)
return family_lengths
def main_function():
file_is = input("Please input filename: ")
start = True
while start:
user_input = input("98 for 98 analysis, 98.5 for 98.5 analysis, R for Regional, \nU for Unique, F98 for Family "
"Finder 98, F98.5 for Family Finder 98.5, \nFR for Regional Family Finder, or X for exit: ")
if user_input == "98":
print(finder_98(file_is))
elif user_input == "98.5":
print(finder_985(file_is))
elif user_input.lower() == "u":
print(unique_finder(file_is))
elif user_input.lower() == "r":
print(finder_regional(file_is))
elif user_input == "F98":
print(family_number_finder_98(file_is))
elif user_input == "F98.5":
print(family_number_finder_985(file_is))
elif user_input.lower() == "fr":
print(regional_family_finder(file_is))
elif user_input.lower() == "x":
start = False
main_function()