|
| 1 | +import numpy as np |
| 2 | +import pandas as pd |
| 3 | + |
| 4 | + |
| 5 | +def read_cosine_matrix(matrix_file): |
| 6 | + matrix = np.load(matrix_file) |
| 7 | + cosine_matrix = matrix['arr_0'] |
| 8 | + return cosine_matrix |
| 9 | + |
| 10 | + |
| 11 | +def create_cluster_score_matrix(cluster_file: str, cosine_matrix: np.array, blast_file: str, taxon_file: str, output_file: str) -> None: |
| 12 | + """ |
| 13 | + Adds the corresponding cosine similarity score, blast percentage identity score and taxons for each pair of the |
| 14 | + clustered pairs. |
| 15 | + Parameters |
| 16 | + ---------- |
| 17 | + cluster_file : str |
| 18 | + TSV Filepath to clustered pairs of proteins. |
| 19 | + cosine_matrix : np.array |
| 20 | + Cosine matrix of dimensions 161354 x 161354 of the best model. |
| 21 | + blast_file : str |
| 22 | + TSV Filepath for blast percentage identity score for all Eukaryota proteins. |
| 23 | + taxon_file : str |
| 24 | + TSV Filepath for Eukaryota fucntions and taxonomy data. |
| 25 | + output_file : str |
| 26 | + Output filepath to save the resulting score matrix matrix. |
| 27 | + """ |
| 28 | + df = pd.read_csv(cluster_file, sep='\t') |
| 29 | + ref_indices = df['accession1_index'].to_numpy() |
| 30 | + asd_indices = df['accession2_index'].to_numpy() |
| 31 | + array = np.array(list(zip(ref_indices, asd_indices)), dtype=object) |
| 32 | + # adds cosine score |
| 33 | + cosine_array = np.zeros((len(ref_indices)), dtype=object) |
| 34 | + for idx, i in enumerate(array): |
| 35 | + if i[0] > i[1]: |
| 36 | + cosine_score = cosine_matrix[i[1]][i[0]] |
| 37 | + elif i[0] < i[1]: |
| 38 | + cosine_score = cosine_matrix[i[0]][i[1]] |
| 39 | + cosine_array[idx] = cosine_score |
| 40 | + df['cosine_score'] = cosine_array |
| 41 | + # adds blast percentage identity score |
| 42 | + blast = pd.read_csv(blast_file, sep='\t') |
| 43 | + blast_grouped = blast.groupby(['accession1', 'accession2'])['sequence_identity_score'].max() |
| 44 | + result = pd.merge(df, blast_grouped, on=['accession1', 'accession2'], how='left') |
| 45 | + result['sequence_identity_score'].fillna( |
| 46 | + result.groupby(['accession2', 'accession1'])['sequence_identity_score'].transform('max'), inplace=True) |
| 47 | + # adds taxon for both accessions |
| 48 | + df_euk = pd.read_csv(taxon_file, sep='\t') |
| 49 | + accession_to_taxon = dict(zip(df_euk['accession'], df_euk['taxon'])) |
| 50 | + result['taxon_acc_1'] = df['accession1'].map(accession_to_taxon) |
| 51 | + result['taxon_acc_2'] = df['accession2'].map(accession_to_taxon) |
| 52 | + result.to_csv(output_file, sep='\t') |
| 53 | + # # df = pd.read_csv("./data/output/scores/score_hybrid.tsv", sep='\t') |
| 54 | + # # df = df[df['sequence_identity_score'].isnull()] |
| 55 | + # df = pd.read_csv("./add.tsv", sep='\t') |
| 56 | + # print(len(df)) |
| 57 | + # df.to_csv("./add2.tsv", sep='\t') |
| 58 | + # result = pd.read_csv("./data/output/scores/clustered_score_matrix_hybrid.tsv", sep='\t') |
| 59 | + # new = pd.read_csv("./values2.tsv", sep='\t') |
| 60 | + # merged = pd.concat([result, new]) |
| 61 | + # merged.dropna(subset=['sequence_identity_score'], inplace=True) |
| 62 | + # merged.to_csv("./data/output/scores/clustered_score_matrix_hybrid.tsv", sep='\t') |
| 63 | + # print(len(result.loc[result['cosine_score'] < 0.9])) |
| 64 | + # merged.to_csv("./data/clustered_score_matrix_word2doc2vec.tsv", sep='\t', index=False) |
| 65 | + |
| 66 | + |
| 67 | +def create_non_clustered_score_matrix(cluster_file: str, cosine_matrix: np.array, output_file: str) -> None: |
| 68 | + """ |
| 69 | + Adds the corresponding cosine similarity score for each pair of the |
| 70 | + non-clustered pairs. |
| 71 | + Parameters |
| 72 | + ---------- |
| 73 | + cluster_file : str |
| 74 | + TSV Filepath to clustered pairs of proteins. |
| 75 | + cosine_matrix : np.array |
| 76 | + Cosine matrix of dimensions 161354 x 161354 of the best model. |
| 77 | + output_file : str |
| 78 | + Output filepath to save the resulting score matrix matrix. |
| 79 | + """ |
| 80 | + df = pd.read_csv(cluster_file, sep='\t') |
| 81 | + ref_indices = df['accession1_index'].to_numpy() |
| 82 | + asd_indices = df['accession2_index'].to_numpy() |
| 83 | + array = np.array(list(zip(ref_indices, asd_indices)), dtype=object) |
| 84 | + cosine_array = np.zeros((len(ref_indices)), dtype=object) |
| 85 | + for idx, i in enumerate(array): |
| 86 | + if i[0] > i[1]: |
| 87 | + cosine_score = cosine_matrix[i[1]][i[0]] |
| 88 | + elif i[0] < i[1]: |
| 89 | + cosine_score = cosine_matrix[i[0]][i[1]] |
| 90 | + cosine_array[idx] = cosine_score |
| 91 | + df['cosine_score'] = cosine_array |
| 92 | + df.to_csv(output_file, sep='\t') |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + # Word2doc2Vec |
| 97 | + cosine_matrix = read_cosine_matrix("./data/output/cosine/cosine_word2doc2vev_bestmodel.npz") |
| 98 | + create_cluster_score_matrix("./data/output/uniref/clustered_pairs_index.tsv", |
| 99 | + cosine_matrix, |
| 100 | + "./data/output/blast.tsv", |
| 101 | + "./data/output/functions/rev-20220525-UniProtKB-eukaryota.tsv", |
| 102 | + "./data/output/scores/clustered_score_matrix_word2doc2vec.tsv") |
| 103 | + |
| 104 | + create_non_clustered_score_matrix("./data/output/uniref/not_clustered_pairs_index.tsv", |
| 105 | + cosine_matrix, |
| 106 | + "./data/output/scores/not_clustered_score_matrix_word2doc2vec.tsv") |
| 107 | + |
| 108 | + # Hybrid-Word2doc2Vec |
| 109 | + cosine_matrix = "./data/output/cosine/cosine_hybrid_bestmodel.npz" |
| 110 | + create_cluster_score_matrix("./data/output/uniref/clustered_pairs_index.tsv", |
| 111 | + cosine_matrix, |
| 112 | + "./data/output/blast.tsv", |
| 113 | + "./data/output/functions/rev-20220525-UniProtKB-eukaryota.tsv", |
| 114 | + "./data/output/scores/clustered_score_matrix_hybrid.tsv") |
| 115 | + create_non_clustered_score_matrix("./data/output/uniref/not_clustered_pairs_index.tsv", |
| 116 | + cosine_matrix, |
| 117 | + "./data/output/scores/not_clustered_score_matrix_word2doc2vec.tsv") |
| 118 | + |
0 commit comments