-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
218 lines (191 loc) · 7.07 KB
/
functions.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
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
from utils import NounParser,VerbParser,AdjectiveNumeralParser
import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from lingpy import *
from Bio import Phylo
from Bio.Phylo.TreeConstruction import DistanceMatrix, DistanceTreeConstructor
#Instantiating parser objects
noun_parser=NounParser()
verb_parser=VerbParser()
adj_num_parser=AdjectiveNumeralParser()
def prior_forms(row):
"""
puts all forms that have been parsed into a single column
"""
if pd.isna(row["POS"]):
return None
elif row["POS"] in ["noun", "numeral", "adjective"]:
form = str(row["SINGULAR"]) if isinstance(row["SINGULAR"], str) else ""
return form
elif row["POS"] == "verb":
verb = row["FORM"]
if pd.isna(verb):
return None
else:
return verb
else:
form = str(row["SINGULAR"]) if isinstance(row["SINGULAR"], str) else ""
return form
def parsing_data(row):
"""
puts all forms that have been parsed into a single column
"""
if pd.isna(row["POS"]):
return None
elif row["POS"]=="noun":
noun=row["BEFORE_PARSE"]
if pd.isna(noun):
return None
else:
noun=(noun_parser.identified_suffixes(
noun_parser.hyphen_space(
noun_parser.nasalized_stops(
noun_parser.cvcv_segmentation(
noun_parser.parse_off_final_nasals(
noun_parser.existing_parses(
adj_num_parser.y_suffixes(noun.strip("()/_"))))))))) if (noun.endswith("y") or (noun.endswith("ⁿ") and noun[-2]=="y")) else \
(noun_parser.identified_suffixes(
noun_parser.hyphen_space(
noun_parser.nasalized_stops(
noun_parser.cvcv_segmentation(
noun_parser.parse_off_final_nasals(
noun_parser.existing_parses(noun.strip("()/_"))))))))
return noun
elif row["POS"] == "numeral" or row["POS"] == "adjective":
form = row["BEFORE_PARSE"]
if pd.isna(form):
return None
else:
form=adj_num_parser.miscellaneous(
adj_num_parser.switch_hyphen_position(
adj_num_parser.replace_hyphens_keep_last(
adj_num_parser.y_suffixes(
adj_num_parser.isolating_suffixes(
adj_num_parser.existing_parses(form.strip("()/_")))))))
return form
elif row["POS"] == "verb":
verb = row["BEFORE_PARSE"]
if pd.isna(verb):
return None
else:
verb=verb_parser.post_editing_short_strings(
verb_parser.segment_cvcs(
verb_parser.existing_parses(verb.strip(")(_"))))
return verb
else:
form = str(row["BEFORE_PARSE"]) if isinstance(row["BEFORE_PARSE"], str) else ""
return form
def remove_spaces(word):
if word is None:
return None
new_word=""
for letter in word:
if letter==" ":
new_word += ""
else:
new_word += letter
return new_word
def lexstatExperiment(input_file, coverage_num, lexstat_output):
"""
Takes an input file and outputs a lexstat cluster
input_file: any_name
coverage_num: coverage number
lexstat_output:any_name
"""
wl = Wordlist(input_file)
retain = []
for language, coverage in wl.coverage().items():
if coverage > coverage_num:
retain.append(language)
new_wl = {0: [c for c in wl.columns]}
for idx, language in wl.iter_rows("doculect"):
if language in retain:
new_wl[idx] = wl[idx]
new_wl = Wordlist(new_wl)
lex = LexStat(new_wl)
lex.get_scorer(runs=10000)
lex.cluster(method='lexstat', threshold=0.55, ref='cogid')
return lex.output('tsv', filename=lexstat_output)
def alignmentExperiment(input_name, output_name, output_type="html"):
"""
returns a nexus file of alignments.
Arguments:
input_name= a tsv lexstat cluster
output_name= name to be given to nexus file
"""
lex=LexStat(input_name)
alm = Alignments(lex, ref='cogid')
alm.align()
return alm.output(output_type, filename=output_name)
def getHeatmap(distance, taxa, title, heatmap_name):
"""
returns a heatmap. It accepts a distance matrix and a list of languages
Args:
distance=distance matrix
taxa=languages
title=title of heatmap
heatmap_name=name to be used to save heatmap
"""
fig=plt.figure(figsize=(12, 8)) # Adjust the width and height as needed
sns.heatmap(pd.DataFrame(distance, taxa, columns=taxa), annot=True)
plt.title(title)
plt.show()
return fig.savefig(heatmap_name)
def getdistanceandtaxa(lexstat_cluster):
"returns distances and languages that must be assigned a variable. Accepts a lextstat clustered tsv file"
lex=LexStat(lexstat_cluster)
alm=Alignments(lex)
alm.align()
distance=alm.get_distances()
taxa=alm.taxa
return distance, taxa
def treeConstructor(distance, language, tree_name,title):
"""
returns a phylogenetic tree. It accepts a distance matrix and a list of languages
Args:
distance=distance matrix
language=languages
tree_name=name to be used to save heatmap
title=title of heatmap
"""
df = pd.DataFrame(distance, language, columns=language)
# Convert the DataFrame to a lower triangular matrix format
def to_lower_triangle(matrix):
lower_triangle = []
for i in range(len(matrix)):
row = []
for j in range(i+1):
row.append(matrix[i][j])
lower_triangle.append(row)
return lower_triangle
matrix = to_lower_triangle(df.values)
labels = df.index.tolist()
# Create a DistanceMatrix object
distance_matrix = DistanceMatrix(names=labels, matrix=matrix)
# Construct the tree using UPGMA (you can also use 'nj' for Neighbor-Joining)
constructor = DistanceTreeConstructor()
tree = constructor.upgma(distance_matrix)
# Draw the tree using matplotlib and save to a PNG file
fig = plt.figure(figsize=(10, 7))
ax = fig.add_subplot(1, 1, 1)
plt.title(title)
Phylo.draw(tree, do_show=False, axes=ax)
plt.show()
# Save the figure as a PNG file
return fig.savefig(tree_name)
def getHeatmap(distance, taxa, title, heatmap_name):
"""
returns a heatmap. It accepts a distance matrix and a list of languages
Args:
distance=distance matrix
taxa=languages
title=title of heatmap
heatmap_name=name to be used to save heatmap
"""
fig=plt.figure(figsize=(12, 8)) # Adjust the width and height as needed
sns.heatmap(pd.DataFrame(distance, taxa, columns=taxa), annot=True)
plt.title(title)
plt.show()
return fig.savefig(heatmap_name)