compute_distance_mat.py 27.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
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 = "MACWPQLRLLLWKNLTFRRRQTCQLLLEVAWPLFIFLILISVRLSYPPYEQHECHFPNKAMPSAGTLPWVQGIICNANNPCFRYPTPGEAPGVVGNFNKSIVARLFSDARRLLLYSQKDTSMKDMRKVLRTLQQIKKSSSNLKLQDFLVDNETFSGFLYHNLSLPKSTVDKMLRADVILHKVFLQGYQLHLTSLCNGSKSEEMIQLGDQEVSELCGLPREKLAAAERVLRSNMDILKPILRTLNSTSPFPSKELAEATKTLLHSLGTLA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FGDKQSCLRPFTEDDAADPNDSDIDPESRETDLLSGMDGKGSYQVKGWKLTQQQFVALLWKRLLIARRSRKGFFAQIVLPAVFVCIALVFSLIVPPFGKYPSLELQPWMYNEQYTFVSNDAPEDTGTLELLNALTKDPGFGTRCMEGNPIPDTPCQAGEEEWTTAPVPQTIMDLFQNGNWTMQNPSPACQCSSDKIKKMLPVCPPGAGGLPPPQRKQNTADILQDLTGRNISDYLVKTYVQIIAKSLKNKIWVNEFRYGGFSLGVSNTQALPPSQEVNDAIKQMKKHLKLAKDSSADRFLNSLGRFMTGLDTKNNVKVWFNNKGWHAISSFLNVINNAILRANLQKGENPSHYGITAFNHPLNLTKQQLSEVALMTTSVDVLVSICVIFAMSFVPASFVVFLIQERVSKAKHLQFISGVKPVIYWLSNFVWDMCNYVVPATLVIIIFICFQQKSYVSSTNLPVLALLLLLYGWSITPLMYPASFVFKIPSTAYVVLTSVNLFIGINGSVATFVLELFTDNKLNNINDILKSVFLIFPHFCLGRGLIDMVKNQAMADALERFGENRFVSPLSWDLVGRNLFAMAVEGVVFFLITVLIQYRFFIRPRPVNAKLSPLNDEDEDVRRERQRILDGGGQNDILEIKELTKIYRRKRKPAVDRICVGIPPGECFGLLGVNGAGKSSTFKMLTGDTTVTRGDAFLNKNSILSNIHEVHQNMGYCPQFDAITELLTGREHVEFFALLRGVPEKEVGKVGEWAIRKLGLVKYGEKYAGNYSGGNKRKLSTAMALIGGPPVVFLDEPTTGMDPKARRFLWNCALSVVKEGRSVVLTSHSMEECEALCTRMAIMVNGRFRCLGSVQHLKNRFGDGYTIVVRIAGSNPDLKPVQDFFGLAFPGSVLKEKHRNMLQYQLPSSLSSLARIFSILSQSKKRLHIEDYSVSQTTLDQVFVNFAKDQSDDDHLKDLSLHKNQTVVDVAVLTSFLQDEKVKESYV"
   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)