compara-deep-learning/finalize_dataset.py

107 lines
3.3 KiB
Python
Raw Permalink Normal View History

2019-07-24 04:54:02 -07:00
import numpy as np
2019-07-24 03:27:54 -07:00
import pickle
import os
import sys
from tree_data import create_tree_data
from process_negative import read_database_txt
from select_data import read_db_homology
2019-07-24 04:54:02 -07:00
def read_data_homology(dirname, nfname):
lf = os.listdir(dirname)
if len(lf) == 0:
2019-07-24 03:27:54 -07:00
print("No Files in the Directory!!!!!!!")
sys.exit(1)
2019-07-24 04:54:02 -07:00
a_h = []
d_h = []
2019-07-24 03:27:54 -07:00
for x in lf:
2019-07-24 04:54:02 -07:00
df, n = read_db_homology(dirname, x)
n = n.split()[0]
2019-07-24 03:27:54 -07:00
try:
2019-07-24 04:54:02 -07:00
indexes = np.load(
"processed/synteny_matrices/" +
n +
"_indexes.npy")
except BaseException:
print("Incomplete data for:", n)
df = df.loc[indexes]
2019-07-24 03:27:54 -07:00
a_h.append(df)
d_h.append(n)
2019-07-24 04:54:02 -07:00
# read the negative dataset
df = read_database_txt(nfname)
indexes = np.load(
"processed/synteny_matrices/" +
nfname.split(".")[0] +
"_indexes.npy")
df = df.loc[indexes]
2019-07-24 03:27:54 -07:00
a_h.append(df)
d_h.append(nfname.split(".")[0])
2019-07-24 04:54:02 -07:00
return a_h, d_h
2019-07-24 03:27:54 -07:00
2019-07-24 04:54:02 -07:00
def prepare_features(a_h, d_h, sptree, label):
rows = []
smg_name = "_synteny_matrices_global.npy"
sml_name = "_synteny_matrices_local.npy"
smi_name = "_indexes.npy"
dir_name = "processed/synteny_matrices/"
2019-07-24 03:27:54 -07:00
for i in range(len(a_h)):
2019-07-24 04:54:02 -07:00
df = a_h[i]
n = d_h[i]
2019-07-24 03:27:54 -07:00
try:
2019-07-24 04:54:02 -07:00
smg = np.load(dir_name + n + smg_name)
sml = np.load(dir_name + n + sml_name)
indexes = np.load(dir_name + n + smi_name)
except BaseException:
print("Incomplete data for:", n)
2019-07-24 03:27:54 -07:00
continue
2019-07-24 04:54:02 -07:00
df = df.loc[indexes]
2019-07-24 03:27:54 -07:00
2019-07-24 04:54:02 -07:00
branch_length_species, \
branch_length_homology_species, \
distance, dist_p_s, dist_p_hs = create_tree_data(
sptree, df)
assert(len(branch_length_species) == len(df))
assert(len(sml) == len(distance))
2019-07-24 03:27:54 -07:00
for i in range(len(df)):
2019-07-24 04:54:02 -07:00
index = indexes[i]
row = df.loc[index]
r = {}
r["species"] = row["species"]
r["homology_species"] = row["homology_species"]
r["gene_stable_id"] = row["gene_stable_id"]
r["homology_gene_stable_id"] = row["homology_gene_stable_id"]
r["label"] = label[row["homology_type"]]
r["global_alignment_matrix"] = smg[i]
r["local_alignment_matrix"] = sml[i]
r["index_homology_dataset"] = index
r["bls"] = branch_length_species[i]
r["blhs"] = branch_length_homology_species[i]
r["dis"] = distance[i]
r["dps"] = dist_p_s[i]
r["dphs"] = dist_p_hs[i]
2019-07-24 03:27:54 -07:00
rows.append(r)
return rows
2019-07-24 04:54:02 -07:00
2019-07-24 03:27:54 -07:00
def main():
2019-07-24 04:54:02 -07:00
arg = sys.argv
nfname = arg[-1]
a_h, d_h = read_data_homology("data_homology", nfname)
labels = dict(ortholog_one2one=1,
other_paralog=0,
non_homolog=2,
ortholog_one2many=1,
ortholog_many2many=1,
within_species_paralog=0,
gene_split=4)
rows = prepare_features(a_h, d_h, "species_tree.tree", labels)
with open("dataset", "wb") as file:
pickle.dump(rows, file)
2019-07-24 03:27:54 -07:00
print("Dataset_Finalized")
2019-07-24 04:54:02 -07:00
if __name__ == "__main__":
2019-07-24 03:27:54 -07:00
main()