AnkIdentification/Supplementary_files/2-Attributing_families.py

34 lines
1.3 KiB
Python

#!/usr/bin/python3.10
# -*- coding: utf-8 -*-
"""************************
AnkIdentification - Supplementary code 2 : Attributing its family to each species.
Aurélien VALENTIN - from 06/11/2022 to 29/12/2022
************************"""
# Importation and initialisation
import json, sys
dict_classification = {}
# Definition of the progressbar function, from https://gist.github.com/ChesterChowWOV/2b35c551b339adbf459363322aac5b4b
def progressbar(it, prefix = "", size = 60, file = sys.stdout):
count = len(it)
def show(j):
x = int(size*j/count)
file.write("{}[{}{}] {}/{} {}%\r".format(prefix, ""*x, "."*(size-x), j, count, round(j / count * 100, 2)))
file.flush()
show(0)
for i, item in enumerate(it):
yield item
show(i + 1)
file.write("\n")
file.flush()
# Creation of the dictionary and the corresponding json file
with open("classification.txt", encoding="utf-8") as classification:
classification.readline()
for line in progressbar(classification.readlines(), "Computing", 40):
if line.split("\t")[4] == "SPECIES":
dict_classification[line.split("\t")[3].split(" ")[0] + " " + line.split("\t")[3].split(" ")[1]] = line.split("\t")[7]
with open("_dict_classification.json", "w") as fpo : json.dump(dict_classification, fpo)