import pandas as pd import Levenshtein import time import numpy as np from Levenshtein import distance import re from minineedle import needle, smith, core from Bio.Blast.Applications import NcbiblastpCommandline from io import StringIO from Bio.Blast import NCBIXML from Bio.Seq import Seq from Bio.SeqRecord import SeqRecord from Bio import SeqIO import swalign import multiprocessing as mp globi=0 import nw_wrapper import nw_wrapper_matrix import math import blosum as bl def substitute_or_remove_prot_id(data,archSubs,sub_rem): """ Substitute or remove protein IDs based on a substitution file. Parameters: - data: DataFrame containing protein data - sub_rem: 's' to substitute, 'r' to remove - archSubs: Substituion file Returns: - Updated DataFrame after substitution or removal """ with open(archSubs) as prottosubs: index=prottosubs.readline() acept=index.split() listtosubs={} for i in range(0,len(acept)): listtosubs[acept[i]]=[] while line := prottosubs.readline(): newline=line.split() #print(len(newline)) for i in range(0,len(newline)): listtosubs[list(listtosubs.keys())[i]].append(newline[i].strip()) resub=1 if re.search("Primary",list(listtosubs.keys())[0]): resub=0 #print(data) #data2=data.copy() if(sub_rem == "s"): data["protein_id"].replace(list(listtosubs.values())[(resub+1)%2], list(listtosubs.values())[resub]) #datacp=data.copy() #print(pd.concat([data2,datacp]).drop_duplicates()) else: global globi datas= data[data["protein_id"].isin(list(listtosubs.values())[(resub+1)%2])==True] data = data[data["protein_id"].isin(list(listtosubs.values())[(resub+1)%2])==False] datas.to_csv('resultados/proteinasDescartadas_'+ str(globi) +'.csv', index=False) globi=globi+1 return data def readData(archivoEntrada, enfermedad): """ Read protein data from an Excel file, filter based on disease, and return protein sequences. Parameters: - archivoEntrada: Excel file containing protein data - enfermedad: Disease ID for filtering Returns: - Protein sequences DataFrame """ data = pd.read_excel(archivoEntrada) sequences = data["protein_sequence"] return sequences def calculate_matrix_similarity(data, similarity_function, output_filename): """ Calculate similarity matrix pairwise between each data pair of sequences using multiprocessing . Parameters: - data: Protein sequences DataFrame - similarity_function: Function to calculate similarity - output_filename: Filename to save the similarity matrix """ num_points = len(data) similarity_matrix = [[0] * num_points for _ in range(num_points)] with mp.Pool(processes=20) as pool: sim_matrix = pool.starmap(similarity_function, [(data[i], data[j]) for i in range(num_points) for j in range(num_points)]) similarity = [] for idx in range(0, len(sim_matrix) // num_points): similarity.append([sim_matrix[idx * num_points: (idx + 1) * num_points]]) datf = pd.DataFrame(np.asmatrix(np.array(similarity))) datf.to_csv(output_filename, index=False, header=False) def remplazar_sequence_for_ID(output,archivoEntrada,archSubs): """ Changes the protein sequences in output to their corresponding id. Parameters: - ouput: Pandas Dataframe with the sequences to be changed - archivoEntrada : Input file with the equivalences of sequence-id - archSubs: Input file with each protein that has to be changed back to its primary entry Returns: - Returns dataframe passed as input with the sequences changed to their respective id """ df_b = pd.read_excel(archivoEntrada) df_b= substitute_or_remove_prot_id(df_b,archSubs,"s") proteinas_dict = dict(df_b[['protein_sequence', 'protein_id']].values) for i in range(len(output)): protein_sequence = output[i] if protein_sequence in proteinas_dict: output[i] = proteinas_dict[protein_sequence] return output def smith_waterman_similarity(pattern1,pattern2): """ Wrapper for Smith-Waterman algorithm using default values. Parameters: - pattern1: Protein sequence 1 - pattern2: Protein sequence 2 Returns: - Smith-Waterman alignment score divided by the maximum lenght between the two sequences, making the score be between the interval [0,1] """ return smith.SmithWaterman(pattern1,pattern2).get_score()/max(len(pattern1), len(pattern2)) def levenshtein_similarity(pattern1, pattern2): """ Calculate Levenshtein similarity between two sequences. Parameters: - pattern1: Protein sequence 1 - pattern2: Protein sequence 2 Returns: - Levenshtein similarity score divided by the maximum lenght between the two sequences, making the score be between the interval [0,1] """ return Levenshtein.distance(pattern1, pattern2) / max(len(pattern1), len(pattern2)) def needleman_wunsch_similarity(pattern1, pattern2): """ Wrapper for Needleman-Wunsch algorithm using default values. Parameters: - pattern1: Protein sequence 1 - pattern2: Protein sequence 2 Returns: - Needleman-Wunsch alignment score normalized between [-1,1] """ global dat #print(needle.NeedlemanWunsch(pattern1 , pattern2).get_score()/max(len(pattern1), len(pattern2))) return needle.NeedlemanWunsch(pattern1 , pattern2).get_score()/max(len(pattern1), len(pattern2)) def to_raw(string): return "{0}".format(string) def blast_similarity(pattern1,pattern2): """ Run BLAST to calculate similarity between two protein sequences. Parameters: - pattern1: Protein sequence 1 - pattern2: Protein sequence 2 Returns: - BLAST alignment score """ seq1 = SeqRecord(Seq(pattern1), id="seq1") seq2 = SeqRecord(Seq(pattern2), id="seq2") assert pattern1 assert pattern2 SeqIO.write(seq1, "seq1.fasta", "fasta") SeqIO.write(seq2, "seq2.fasta", "fasta") SeqIO.write(seq1, "seqx.fasta", "fasta") SeqIO.write(seq1, "seqy.fasta", "fasta") output = NcbiblastpCommandline(query="seq1.fasta", subject="seq2.fasta", outfmt=5)()[0] #print(output) blast_result_record = NCBIXML.read(StringIO(output)) result=0 with open("seq1.fasta", 'w') as target: target.truncate() with open("seq2.fasta", 'w') as target: target.truncate() for alignment in blast_result_record.alignments: for hsp in alignment.hsps: result=result+hsp.score #print(blast_result_record) return float(result)/max(len(pattern1), len(pattern2)) def nwmodScore(sec1,sec2,dic,match,mismatch,gap): """ Wrapper for Needleman-Wunsch algorithm. Parameters: - sec1: Protein sequence 1 - sec2: Protein sequence 2 - dic: Substitution dictionary of the letters that belong to the same group - match: match value - mismatch: mismatch value - gap: gap value Returns: - Needleman-Wunsch alignment score """ #print(sec2) nw_instance=nw_wrapper.NW(sec1,sec2,dic,match,mismatch,gap) #print(str(int(nw_instance.get_score())/max(len(sec1), len(sec2)))) return int(nw_instance.get_score())/(match*max(len(sec1), len(sec2))) def nwmodScoreMt(sec1,sec2,dic,match,mismatch,gap): """ Wrapper for Needleman-Wunsch algorithm. Parameters: - sec1: Protein sequence 1 - sec2: Protein sequence 2 - dic: Substitution dictionary of the letters that belong to the same group - match: match value - mismatch: mismatch value - gap: gap value Returns: - Needleman-Wunsch alignment score """ print(sec2) nw_instance=nw_wrapper_matrix.NWM(sec1,sec2,dic,match,mismatch,gap) print(str(int(nw_instance.get_score())/max(len(sec1), len(sec2)))) len_sec1=0 for i in sec1: len_sec1+=dic[i][i] len_sec2=0 for i in sec2: len_sec2+=dic[i][i] return int(nw_instance.get_score())/(max(len_sec1, len_sec2)) def generate_nwmod(data,dic,dd,sal): """ Matrix generator using Needleman-Wunsch algorithm to compute pairwise score between each pair of proteins with custom matches. Parameters: - data: Protein sequences - dic: Substitution dictionary of the letters that belong to the same group - dd: match, mismatch and gap values dictionary - sal: Extension added to the output file Returns: - Needleman-Wunsch alignment score matrix normalized between [max(gap,mismatch)/match,1] """ num_points=len(data) match=dd["match"] mismatch=dd["mismatch"] gap=dd["gap"] sim_data = [[0 for _ in range(len(data))] for _ in range(len(data))] similarity_matrix = [[0] * num_points for _ in range(num_points)] first=True for i in range(num_points): sim_matrix=[nwmodScore(data[i],data[j],dic,dd["match"],dd["mismatch"],dd["gap"]) for j in range(num_points)] similarity = [] #for i in range(0,num_points): # for j in range(i,num_points): # sim_data[i][j]=sim_matrix.pop(0) # sim_data[j][i]=sim_data[i][j] if(first): datf=pd.DataFrame(np.asmatrix(np.array(sim_matrix))) datf.to_csv('resultados/matrizNW'+sal+'.csv', index=False,header=False) first=False else: datf=pd.DataFrame(np.asmatrix(np.array(sim_matrix))) datf.to_csv('resultados/matrizNW'+sal+'.csv', index=False,header=False, mode="a") return def generate_nwmodpremade(data,dic,dd,sal): """ Matrix generator using Needleman-Wunsch algorithm to compute pairwise score between each pair of proteins with custom matches. Parameters: - data: Protein sequences - dic: Substitution dictionary of the letters that belong to the same group - dd: match, mismatch and gap values dictionary - sal: Extension added to the output file Returns: - Needleman-Wunsch alignment score matrix normalized between [max(gap,mismatch)/match,1] """ num_points=len(data) match=dd["match"] mismatch=dd["mismatch"] gap=dd["gap"] sim_data = [[0 for _ in range(len(data))] for _ in range(len(data))] similarity_matrix = [[0] * num_points for _ in range(num_points)] first=True for i in range(num_points): sim_matrix=[nwmodScoreMt(data[i],data[j],dic,dd["match"],dd["mismatch"],dd["gap"]) for j in range(num_points)] similarity = [] #for i in range(0,num_points): # for j in range(i,num_points): # sim_data[i][j]=sim_matrix.pop(0) # sim_data[j][i]=sim_data[i][j] if(first): datf=pd.DataFrame(np.asmatrix(np.array(sim_matrix))) datf.to_csv('resultados/matrizNW'+sal+'.csv', index=False,header=False) first=False else: datf=pd.DataFrame(np.asmatrix(np.array(sim_matrix))) datf.to_csv('resultados/matrizNW'+sal+'.csv', index=False,header=False, mode="a") return def swap_dict(d): """ Swap keys and values in a dictionary. Parameters: - d: Input dictionary Returns: - Dictionary with swapped keys and values """ new_dict = {} for key, values in d.items(): for value in values: if value not in new_dict: if(len(value)== 1): new_dict[value[0]] = [] else: new_dict[value] = [] if(len(value)== 1): new_dict[value[0]].append(key) else: new_dict[value].append(key) return new_dict def read_aminoacidos(afile): """ Read amino acid data from a file and create dictionaries. Parameters: - afile: Amino acid data file Returns: - Dictionaries with amino acid data """ cla = {} with open(afile, 'r') as op: lines = op.readlines() for line in lines: oo = line.replace('\n', '').split('\t') key = oo.pop(0) cla[key] = oo return swap_dict(cla), cla def get_matrix(data, similarity_function, output_filename): """ Creates a square matrix of with entry using the similarity function specified. Parameters: - data: Protein sequences data file - similarity_function: similarity function to apply - output_filename: Name of the file in which the output matriz will be written """ calculate_matrix_similarity(data, similarity_function, output_filename) def get_clases(clas): """ Calculate the substitutability between aminoacids based on their common clases. Parameters: - clas: Dictionary containing amino acid clases and the aminoacid that belong to that clases Returns: Dictionary of dictionaries with the grade of match that have 2 aminoacids depending on their clases """ clases={} for k,v in clas.items(): for k2,v2 in clas.items(): if(k not in clases.keys()): clases[k]={} if(k2 not in clases[k].keys()): clases[k][k2]=float(len(set(v) & set(v2))/len(set(v) | set(v2))) return clases if __name__=="__main__": inputFile="Data/data_lung_cancer_treatment.xlsx" data=readData(inputFile,"") similarity_functions = [levenshtein_similarity] output_filenames = ["levenshtein_similarity.csv"] for sim_func, output_filename in zip(similarity_functions, output_filenames): get_matrix(data, sim_func, output_filename) afile="Data/aminoacidos_mod.txt" sal="mod1" a = "MACWPQLRLLLWKNLTFRRRQTCQLLLEVAWPLFIFLILISVRLSYPPYEQHECHFPNKAMP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GSVLKEKHRNMLQYQLPSSLSSLARIFSILSQSKKRLHIEDYSVSQTTLDQVFVNFAKDQSDDDHLKDLSLHKNQTVVDVAVLTSFLQDEKVKESYV" c = {'A': {'A': 1, 'C': 1}, 'C': {'A': 1, 'C': 1}} dd={"match":3,"mismatch":0,"gap":-1} nw_instance = nw_wrapper.NW(a, b, c, dd["match"], dd["mismatch"], dd["gap"]) score = nw_instance.get_score() print(f"Alignment Score: {score/max(len(a),len(b))}") di=[] do={} clas,_=read_aminoacidos(afile) clases=get_clases(clas) generate_nwmod(data,clases,dd,sal) matrix = bl.BLOSUM(62) dd={"match":1,"mismatch":-4,"gap":0} sal="blosum62" generate_nwmodpremade(data,matrix,dd,sal) """ output=data.to_list() output=remplazar_sequence_for_ID(data,inputFile,"nombres_sust.txt") similarity_matrix=pd.read_csv('resultados/matrizLevenshtein.csv',header=None,index_col=False) #similarity_matrix=similarity_matrix/2 #similarity_matrix=similarity_matrix.abs() #similarity_matrix.to_numpy() sim_mat_40=similarity_matrix.copy() sim_mat_20=similarity_matrix.copy() #sim_mat_10=similarity_matrix.copy() data_40=pd.read_csv('resultados/Metrica_Coincidencia.csv',names=['proteina1','proteina2','%Coincidencia'],index_col=False) #data_40=data_40.drop([0]) #data_20=pd.read_csv('resultados/Metrica_Coincidencia_20.csv',names=['proteina1','proteina2','%Coincidencia'],index_col=False) #data_20=data_20.drop([0]) #data_10=pd.read_csv('resultados/Metrica_Coincidencia_10.csv',names=['proteina1','proteina2','%Coincidencia'],index_col=False) #data_10=data_10.drop([0]) #new_sim=np.copy(similarity_matrix) #print(output) #new_sim_mean=np.copy(similarity_matrix) for i,ks in data_40.iterrows(): sim_mat_40[output.index(ks['proteina1'])][output.index(ks['proteina2'])]+=float(ks['%Coincidencia'])*0.3/70 sim_mat_20[output.index(ks['proteina1'])][output.index(ks['proteina2'])]+=float(ks['%Coincidencia']) #for i,kks in data_20.iterrows(): # sim_mat_20[output.index(kks['proteina1'])][output.index(kks['proteina2'])]+=float(kks['%Coincidencia'])*0.3 #for i,ksk in data_10.iterrows(): # sim_mat_10[output.index(ksk['proteina1'])][output.index(ksk['proteina2'])]+=float(ksk['%Coincidencia'])*0.3 dfx=pd.DataFrame(sim_mat_20) dfx=df/2 dfx=df-1 dfx.abs() dfx.to_csv("resultados/matrizLevenshteinFS_Mean.csv",header=False,index=False) dfx=pd.DataFrame(sim_mat_40) dfx=dfx*0.7 dfx=dfx-1 dfx.abs() dfx.to_csv("resultados/matrizLevenshteinFS_70.csv",header=False,index=False) similarity_matrix=pd.read_csv('resultados/matrizNeedleWunch.csv',header=None,index_col=False)-1 similarity_matrix=similarity_matrix/2 similarity_matrix=similarity_matrix.abs() #similarity_matrix.to_numpy() sim_mat_40=similarity_matrix.copy() sim_mat_20=similarity_matrix.copy() #sim_mat_10=similarity_matrix.copy() data_40=pd.read_csv('resultados/Metrica_Coincidencia.csv',names=['proteina1','proteina2','%Coincidencia'],index_col=False) #data_40=data_40.drop([0]) #data_20=pd.read_csv('resultados/Metrica_Coincidencia_20.csv',names=['proteina1','proteina2','%Coincidencia'],index_col=False) #data_20=data_20.drop([0]) #data_10=pd.read_csv('resultados/Metrica_Coincidencia_10.csv',names=['proteina1','proteina2','%Coincidencia'],index_col=False) #data_10=data_10.drop([0]) #new_sim=np.copy(similarity_matrix) #print(output) #new_sim_mean=np.copy(similarity_matrix) indexes=[] for i,ks in data_40.iterrows(): indexes.append(output.index(ks['proteina1']),output.index(ks['proteina2'])) sim_mat_40[output.index(ks['proteina1'])][output.index(ks['proteina2'])]+=float(ks['%Coincidencia'])*0.3/70 sim_mat_20[output.index(ks['proteina1'])][output.index(ks['proteina2'])]+=float(ks['%Coincidencia'])/100 print(indexes) #for i,kks in data_20.iterrows(): # sim_mat_20[output.index(kks['proteina1'])][output.index(kks['proteina2'])]+=float(kks['%Coincidencia'])*0.3 #for i,ksk in data_10.iterrows(): # sim_mat_10[output.index(ksk['proteina1'])][output.index(ksk['proteina2'])]+=float(ksk['%Coincidencia'])*0.3 dfx=pd.DataFrame(sim_mat_20) dfx=df/2 dfx=df-1 dfx.abs() dfx.to_csv("resultados/matrizNeedleWunchFS_Mean.csv",header=False,index=False) dfx=pd.DataFrame(sim_mat_40) dfx=dfx*0.7 dfx=dfx-1 dfx.abs() """ # dfx.to_csv("resultados/mmatrizNeedleWunchFS_70.csv",header=False,index=False) """ dfx=pd.DataFrame(sim_mat_10) dfx=df/1.3 dfx=df-1 dfx.abs() dfx.to_csv("resultados/matrizLevenshteinFS_10.csv",header=False,index=False) s1 = pd.merge(data_40, data_20, how='inner', on=['proteina1','proteina2']) s2= pd.merge(s1,data_10, how='inner', on=['proteina1','proteina2']) ss=s1[(~(s1['proteina1'].isin(s2['proteina1']))& ~(s1['proteina2'].isin(s2['proteina2'])))] s3 = pd.merge(data_20, data_10, how='inner', on=['proteina1','proteina2']) print(s3['proteina2'].isin(s2['proteina2'])) s4=s3[~(s3['proteina1'].isin(s2['proteina1']))&~(s3['proteina2'].isin(s2['proteina2']))] s5 = pd.merge(data_40, data_10, how='inner', on=['proteina1','proteina2']) s6=s5.loc[~(s5['proteina1'].isin(s2['proteina1']))&(s5['proteina2'].isin(s2['proteina2']))] data_401=data_40[~(data_40['proteina1'].isin(data_20['proteina1']))& ~(data_40['proteina2'].isin(data_20['proteina2']))] data_402=data_40[~(data_40['proteina1'].isin(data_10['proteina1']))& ~(data_40['proteina2'].isin(data_10['proteina2']))] data_40X=data_402[~(data_402['proteina1'].isin(data_20['proteina1']))& ~(data_402['proteina2'].isin(data_20['proteina2']))] data_201=data_20[~(data_20['proteina1'].isin(data_40['proteina1']))&(data_20['proteina2'].isin(data_40['proteina2']))] data_202=data_20[~(data_20['proteina1'].isin(data_10['proteina1']))&(data_20['proteina2'].isin(data_10['proteina2']))] data_20X=data_202[~(data_202['proteina1'].isin(data_40['proteina1']))&(data_202['proteina2'].isin(data_40['proteina2']))] data_101=data_10[~(data_10['proteina1'].isin(data_40['proteina1']))&(data_10['proteina2'].isin(data_40['proteina2']))] data_102=data_10[~(data_10['proteina1'].isin(data_20['proteina1']))&(data_10['proteina2'].isin(data_20['proteina2']))] data_10X=data_102[~(data_102['proteina1'].isin(data_40['proteina1']))&(data_102['proteina2'].isin(data_40['proteina2']))] #print(s3) print(data_40X) print(data_20X) print(data_10X) #print(data_402) for i in range(0,similarity_matrix.shape[0]): for j in range(0,similarity_matrix.shape[1]): cross=0 cross_over=0 dd_10_check=False dd_20_check=False dd_40_check=False if ((data_40['proteina1']==output[i]) & (data_40['proteina2']==output[j])).any() or ((data_40['proteina1']==output[j]) & (data_40['proteina2']==output[i])).any(): dd_40_check=True if ((data_40['proteina1']==output[i]) & (data_40['proteina2']==output[j])).any(): dd_40=float(data_40[(data_40['proteina1']==output[i]) & (data_40['proteina2']==output[j])]['%Coincidencia'].to_list()[0])/100 else: dd_40=float(data_40[(data_40['proteina1']==output[j]) & (data_40['proteina2']==output[i])]['%Coincidencia'].to_list()[0])/100 if ((data_20['proteina1']==output[i]) & (data_20['proteina2']==output[j])).any() or ((data_20['proteina1']==output[j]) & (data_20['proteina2']==output[i])).any(): dd_20_check=True if ((data_20['proteina1']==output[i]) & (data_20['proteina2']==output[j])).any(): dd_20=float(data_20[(data_20['proteina1']==output[i]) & (data_20['proteina2']==output[j])]['%Coincidencia'].to_list()[0])/100 else: dd_20=float(data_20[(data_20['proteina1']==output[j]) & (data_20['proteina2']==output[i])]['%Coincidencia'].to_list()[0])/100 if ((data_10['proteina1']==output[i]) & (data_10['proteina2']==output[j])).any() or ((data_10['proteina1']==output[j]) & (data_10['proteina2']==output[i])).any(): dd_10_check=True if ((data_10['proteina1']==output[i]) & (data_10['proteina2']==output[j])).any(): dd_10=float(data_10[(data_10['proteina1']==output[i]) & (data_10['proteina2']==output[j])]['%Coincidencia'].to_list()[0])/100 else: dd_10=float(data_10[(data_10['proteina1']==output[j]) & (data_10['proteina2']==output[i])]['%Coincidencia'].to_list()[0])/100 if dd_10_check and dd_40_check and dd_20_check: #print(dd_40) #print(dd_20) #print(dd_10) cross=(dd_40-dd_20)-(dd_20-dd_10) cross_over=(dd_40+dd_20+dd_10)/len([dd_20,dd_10,dd_40]) elif dd_20_check and dd_40_check: cross=(dd_40-dd_20) cross_over=(dd_40+dd_20)/len([dd_20,dd_40]) elif dd_10_check and dd_20_check: cross=(dd_20-dd_10) cross_over=(dd_20+dd_10)/len([dd_20,dd_10]) elif dd_40_check and dd_10_check: cross=(dd_40-dd_10) cross_over=(dd_40+dd_10)/len([dd_10,dd_40]) elif dd_40_check: cross=-dd_40 cross_over=dd_40 elif dd_20_check: cross=-dd_20 cross_over=dd_20 elif dd_10_check: cross=-dd_10 cross_over=dd_10 if(cross!=0): print(cross) if(cross==0): cross=-1 new_sim[i][j]+=0.3*cross new_sim_mean[i][j]+=0.3*cross_over df=pd.DataFrame(new_sim) df=df-1 df.abs() df=df/1.3 df.to_csv("resultados/matrizLevenshteinF.csv",header=False,index=False) df2=pd.DataFrame(new_sim_mean) df2=df/1.3 df2=df-1 df2.abs() """ #df2.to_csv("resultados/matrizLevenshteinFMean.csv",header=False,index=False)