more refactoring

This commit is contained in:
manudiv16 2020-05-22 01:01:17 +02:00
commit 957bdb26ca
5 changed files with 41 additions and 109 deletions

View file

@ -1,67 +0,0 @@
graph "Hierarchy Sars-Cov-2" {
node [shape=plaintext]
"Malaysia,Thailand" -- Malaysia
"Malaysia,Thailand" -- Thailand
"Hong Kong,Viet Nam" -- "Hong Kong"
"Hong Kong,Viet Nam" -- "Viet Nam"
"South Africa,Greece" -- "South Africa"
"South Africa,Greece" -- Greece
"South Africa,Greece,Japan" -- "South Africa,Greece"
"South Africa,Greece,Japan" -- Japan
"South Africa,Greece,Japan,Brazil" -- "South Africa,Greece,Japan"
"South Africa,Greece,Japan,Brazil" -- Brazil
"Spain,Colombia" -- Spain
"Spain,Colombia" -- Colombia
"Hong Kong,Viet Nam,Sweden" -- "Hong Kong,Viet Nam"
"Hong Kong,Viet Nam,Sweden" -- Sweden
"Hong Kong,Viet Nam,Sweden,South Korea" -- "Hong Kong,Viet Nam,Sweden"
"Hong Kong,Viet Nam,Sweden,South Korea" -- "South Korea"
"Israel,Taiwan" -- Israel
"Israel,Taiwan" -- Taiwan
"Hong Kong,Viet Nam,Sweden,South Korea,South Africa,Greece,Japan,Brazil" -- "Hong Kong,Viet Nam,Sweden,South Korea"
"Hong Kong,Viet Nam,Sweden,South Korea,South Africa,Greece,Japan,Brazil" -- "South Africa,Greece,Japan,Brazil"
"Hong Kong,Viet Nam,Sweden,South Korea,South Africa,Greece,Japan,Brazil,Israel,Taiwan" -- "Hong Kong,Viet Nam,Sweden,South Korea,South Africa,Greece,Japan,Brazil"
"Hong Kong,Viet Nam,Sweden,South Korea,South Africa,Greece,Japan,Brazil,Israel,Taiwan" -- "Israel,Taiwan"
"Hong Kong,Viet Nam,Sweden,South Korea,South Africa,Greece,Japan,Brazil,Israel,Taiwan,Peru" -- "Hong Kong,Viet Nam,Sweden,South Korea,South Africa,Greece,Japan,Brazil,Israel,Taiwan"
"Hong Kong,Viet Nam,Sweden,South Korea,South Africa,Greece,Japan,Brazil,Israel,Taiwan,Peru" -- Peru
"Hong Kong,Viet Nam,Sweden,South Korea,South Africa,Greece,Japan,Brazil,Israel,Taiwan,Peru,Unknown" -- "Hong Kong,Viet Nam,Sweden,South Korea,South Africa,Greece,Japan,Brazil,Israel,Taiwan,Peru"
"Hong Kong,Viet Nam,Sweden,South Korea,South Africa,Greece,Japan,Brazil,Israel,Taiwan,Peru,Unknown" -- Unknown
"Hong Kong,Viet Nam,Sweden,South Korea,South Africa,Greece,Japan,Brazil,Israel,Taiwan,Peru,Unknown,India" -- "Hong Kong,Viet Nam,Sweden,South Korea,South Africa,Greece,Japan,Brazil,Israel,Taiwan,Peru,Unknown"
"Hong Kong,Viet Nam,Sweden,South Korea,South Africa,Greece,Japan,Brazil,Israel,Taiwan,Peru,Unknown,India" -- India
"China,Nepal" -- China
"China,Nepal" -- Nepal
"China,Nepal,Turkey" -- "China,Nepal"
"China,Nepal,Turkey" -- Turkey
"China,Nepal,Turkey,Finland" -- "China,Nepal,Turkey"
"China,Nepal,Turkey,Finland" -- Finland
"Hong Kong,Viet Nam,Sweden,South Korea,South Africa,Greece,Japan,Brazil,Israel,Taiwan,Peru,Unknown,India,USA" -- "Hong Kong,Viet Nam,Sweden,South Korea,South Africa,Greece,Japan,Brazil,Israel,Taiwan,Peru,Unknown,India"
"Hong Kong,Viet Nam,Sweden,South Korea,South Africa,Greece,Japan,Brazil,Israel,Taiwan,Peru,Unknown,India,USA" -- USA
"Hong Kong,Viet Nam,Sweden,South Korea,South Africa,Greece,Japan,Brazil,Israel,Taiwan,Peru,Unknown,India,USA,Pakistan" -- "Hong Kong,Viet Nam,Sweden,South Korea,South Africa,Greece,Japan,Brazil,Israel,Taiwan,Peru,Unknown,India,USA"
"Hong Kong,Viet Nam,Sweden,South Korea,South Africa,Greece,Japan,Brazil,Israel,Taiwan,Peru,Unknown,India,USA,Pakistan" -- Pakistan
"Hong Kong,Viet Nam,Sweden,South Korea,South Africa,Greece,Japan,Brazil,Israel,Taiwan,Peru,Unknown,India,USA,Pakistan,China,Nepal,Turkey,Finland" -- "Hong Kong,Viet Nam,Sweden,South Korea,South Africa,Greece,Japan,Brazil,Israel,Taiwan,Peru,Unknown,India,USA,Pakistan"
"Hong Kong,Viet Nam,Sweden,South Korea,South Africa,Greece,Japan,Brazil,Israel,Taiwan,Peru,Unknown,India,USA,Pakistan,China,Nepal,Turkey,Finland" -- "China,Nepal,Turkey,Finland"
"Hong Kong,Viet Nam,Sweden,South Korea,South Africa,Greece,Japan,Brazil,Israel,Taiwan,Peru,Unknown,India,USA,Pakistan,China,Nepal,Turkey,Finland,Spain,Colombia" -- "Hong Kong,Viet Nam,Sweden,South Korea,South Africa,Greece,Japan,Brazil,Israel,Taiwan,Peru,Unknown,India,USA,Pakistan,China,Nepal,Turkey,Finland"
"Hong Kong,Viet Nam,Sweden,South Korea,South Africa,Greece,Japan,Brazil,Israel,Taiwan,Peru,Unknown,India,USA,Pakistan,China,Nepal,Turkey,Finland,Spain,Colombia" -- "Spain,Colombia"
"Nigeria,Australia" -- Nigeria
"Nigeria,Australia" -- Australia
"Malaysia,Thailand,Italy" -- "Malaysia,Thailand"
"Malaysia,Thailand,Italy" -- Italy
"Malaysia,Thailand,Italy,Iran" -- "Malaysia,Thailand,Italy"
"Malaysia,Thailand,Italy,Iran" -- Iran
"Malaysia,Thailand,Italy,Iran,Philippines" -- "Malaysia,Thailand,Italy,Iran"
"Malaysia,Thailand,Italy,Iran,Philippines" -- Philippines
"Hong Kong,Viet Nam,Sweden,South Korea,South Africa,Greece,Japan,Brazil,Israel,Taiwan,Peru,Unknown,India,USA,Pakistan,China,Nepal,Turkey,Finland,Spain,Colombia,France" -- "Hong Kong,Viet Nam,Sweden,South Korea,South Africa,Greece,Japan,Brazil,Israel,Taiwan,Peru,Unknown,India,USA,Pakistan,China,Nepal,Turkey,Finland,Spain,Colombia"
"Hong Kong,Viet Nam,Sweden,South Korea,South Africa,Greece,Japan,Brazil,Israel,Taiwan,Peru,Unknown,India,USA,Pakistan,China,Nepal,Turkey,Finland,Spain,Colombia,France" -- France
"Nigeria,Australia,Tunisia" -- "Nigeria,Australia"
"Nigeria,Australia,Tunisia" -- Tunisia
"Nigeria,Australia,Tunisia,Malaysia,Thailand,Italy,Iran,Philippines" -- "Nigeria,Australia,Tunisia"
"Nigeria,Australia,Tunisia,Malaysia,Thailand,Italy,Iran,Philippines" -- "Malaysia,Thailand,Italy,Iran,Philippines"
"Nigeria,Australia,Tunisia,Malaysia,Thailand,Italy,Iran,Philippines,Iraq" -- "Nigeria,Australia,Tunisia,Malaysia,Thailand,Italy,Iran,Philippines"
"Nigeria,Australia,Tunisia,Malaysia,Thailand,Italy,Iran,Philippines,Iraq" -- Iraq
"Nigeria,Australia,Tunisia,Malaysia,Thailand,Italy,Iran,Philippines,Iraq,Belgium" -- "Nigeria,Australia,Tunisia,Malaysia,Thailand,Italy,Iran,Philippines,Iraq"
"Nigeria,Australia,Tunisia,Malaysia,Thailand,Italy,Iran,Philippines,Iraq,Belgium" -- Belgium
"Nigeria,Australia,Tunisia,Malaysia,Thailand,Italy,Iran,Philippines,Iraq,Belgium,Serbia" -- "Nigeria,Australia,Tunisia,Malaysia,Thailand,Italy,Iran,Philippines,Iraq,Belgium"
"Nigeria,Australia,Tunisia,Malaysia,Thailand,Italy,Iran,Philippines,Iraq,Belgium,Serbia" -- Serbia
"Nigeria,Australia,Tunisia,Malaysia,Thailand,Italy,Iran,Philippines,Iraq,Belgium,Serbia,Hong Kong,Viet Nam,Sweden,South Korea,South Africa,Greece,Japan,Brazil,Israel,Taiwan,Peru,Unknown,India,USA,Pakistan,China,Nepal,Turkey,Finland,Spain,Colombia,France" -- "Nigeria,Australia,Tunisia,Malaysia,Thailand,Italy,Iran,Philippines,Iraq,Belgium,Serbia"
"Nigeria,Australia,Tunisia,Malaysia,Thailand,Italy,Iran,Philippines,Iraq,Belgium,Serbia,Hong Kong,Viet Nam,Sweden,South Korea,South Africa,Greece,Japan,Brazil,Israel,Taiwan,Peru,Unknown,India,USA,Pakistan,China,Nepal,Turkey,Finland,Spain,Colombia,France" -- "Hong Kong,Viet Nam,Sweden,South Korea,South Africa,Greece,Japan,Brazil,Israel,Taiwan,Peru,Unknown,India,USA,Pakistan,China,Nepal,Turkey,Finland,Spain,Colombia,France"
}

Binary file not shown.

View file

@ -3,9 +3,9 @@ import signal
from os.path import join
from sys import argv
from utils.tree import HierarchyTree
from utils.csv_table import CsvTable
from utils.fasta_map import FastaMap
from utils.tree import HierarchyTree
signal.signal(signal.SIGTSTP, signal.SIG_IGN)
@ -25,18 +25,9 @@ def main():
print("\nBuilding hierarchy...")
table_hierarchy = fasta_map.build_hierarchy()
show_tree(labels, table_hierarchy)
print("Done!")
def show_tree(labels, table_hierarchy):
tree = HierarchyTree(labels)
while len(table_hierarchy) > 1:
closest_pair = FastaMap.find_closest_pair(table_hierarchy)
tree.add_relation(closest_pair)
new_relation = FastaMap.build_relation(closest_pair, table_hierarchy)
table_hierarchy = FastaMap.refactor_table(closest_pair, new_relation, table_hierarchy)
tree.show()
tree.show_tree(table_hierarchy)
print("Done!")
if __name__ == '__main__':

View file

@ -88,36 +88,6 @@ class FastaMap:
f"Comparisons performed in {time.time() - start_time:.3f} seconds!")
return comparisons
@staticmethod
def build_relation(pair, table):
relation = dict()
for elem in pair:
for key, value in table[elem].items():
if key not in pair:
relation.setdefault(key, []).append(value)
relation = {key: min(relation[key]) for key in relation}
return relation
@staticmethod
def refactor_table(pair, relation, table):
new_table = dict()
new_table[pair] = relation
for id1, value in table.items():
if id1 not in pair:
new_table[id1] = {id2: distance for id2, distance in value.items()
if id2 not in pair}
new_table[id1][pair] = relation[id1]
return new_table
@staticmethod
def find_closest_pair(table):
closest_pairs = list()
for key, value in table.items():
closest_id, distance = min(value.items(), key=lambda x: x[-1])
closest_pairs.append((key, closest_id, distance))
sample1, sample2, _ = min(closest_pairs, key=lambda x: x[-1])
return sample1, sample2
@staticmethod
def _to_dict(comparisons):
sample_compare = dict()

View file

@ -22,3 +22,41 @@ class HierarchyTree:
if self.__labels is None:
return value
return ','.join(map(lambda x: self.__labels[x.strip()], value.split(',')))
@staticmethod
def find_closest_pair(table):
closest_pairs = list()
for key, value in table.items():
closest_id, distance = min(value.items(), key=lambda x: x[-1])
closest_pairs.append((key, closest_id, distance))
sample1, sample2, _ = min(closest_pairs, key=lambda x: x[-1])
return sample1, sample2
@staticmethod
def build_relation(pair, table):
relation = dict()
for elem in pair:
for key, value in table[elem].items():
if key not in pair:
relation.setdefault(key, []).append(value)
relation = {key: min(relation[key]) for key in relation}
return relation
@staticmethod
def refactor_table(pair, relation, table):
new_table = dict()
new_table[pair] = relation
for id1, value in table.items():
if id1 not in pair:
new_table[id1] = {id2: distance for id2, distance in value.items()
if id2 not in pair}
new_table[id1][pair] = relation[id1]
return new_table
def show_tree(self, table_hierarchy):
while len(table_hierarchy) > 1:
closest_pair = HierarchyTree.find_closest_pair(table_hierarchy)
self.add_relation(closest_pair)
new_relation = HierarchyTree.build_relation(closest_pair, table_hierarchy)
table_hierarchy = HierarchyTree.refactor_table(closest_pair, new_relation, table_hierarchy)
self.show()