Python Forum

Full Version: compare element of a dictionary
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone

Here is an excerpt from my file I'm working on:

HS3ST1 GPC5 0 0 0 0 96 900 387 939
HS3ST1 GPC1 0 0 0 61 96 900 470 948 ---> EXP
HS3ST1 GPC4 0 0 0 0 96 900 462 947
SEMA3F PLXNA4 0 0 0 0 188 600 433 799
SEMA3F PLXNA3 0 0 0 62 188 600 900 965 ---> EXP
SEMA3F PLXNA1 0 0 0 111 188 600 904 968 ---> EXP
SEMA3F NRP1 0 0 0 0 478 800 839 981
SEMA3F PLXNA2 0 0 0 88 188 600 431 809 ---> EXP
CFTR PSMC2 0 0 0 62 59 900 50 905 ---> EXP
CFTR PSMD11 0 0 0 62 103 900 41 908 ---> EXP
CFTR DVL2 0 0 0 62 52 900 514 951 ---> EXP
CFTR CBL 0 0 0 0 379 900 207 946
CFTR PSMD4 0 0 0 0 51 900 87 905
CFTR EGFR 0 0 0 0 51 900 383 936


I have the following script:

from collections import defaultdict
Dprot1 = defaultdict(set)
Dprot2 = defaultdict(set)
Dinter = defaultdict(dict)
with open("C:/Users/lveillat/Desktop/Données stage/Données/resultats_matrice_avec_scores_sans_localisation.txt","r") as f1:
	for ligne in f1:
		lp = ligne.rstrip('\n').split(" ") 
		if lp[-1] == "EXP": 
			prot1 = lp[0] #prot1=First prot
			prot2 = lp[1] #prot2=Second prot
			if prot1 not in Dprot1: #if the protein is not in the dico then we add it (no duplicates)
				Dprot1[prot1].add(prot1) 
			if prot2 not in Dprot2: #same thing
				Dprot2[prot2].add(prot2)
			Dinter[prot1][prot2]=1 #If interaction between 2 prot on mark 1
	print (Dinter)
f1.close()

for i in Dprot1: #We go through all the elements of Dprot1
	for j in Dprot2: #We go through all the elements of Dprot2
		if Dinter[i][j]: #if i = prot1 and j = prot2
			print ("1")
		else:
			print("0")
Prot1 = different names of proteins, for example:
KDGHZ
DFGF
SDFET
SDGFT
SDDZ
BJJU
DAA
INS
ect...

Prot2 = Same
KQV
SHYZ
CKZ
YSC5E
2FQT
SGY2
ect…

Dinter[prot1][prot2]=1 = In key take a protein and value all the proteins with which it can interact, for example :

Output:
F5': {'SPTBN2': 1, 'ARFGAP1': 1, 'ANK2': 1, 'COPZ2': 1, 'KIF15': 1, 'KIF2B': 1, 'KIF25': 1, 'KDELR2': 1, 'RAB1B': 1, 'TMED10': 1, 'KIF3B': 1, 'GBF1': 1, 'CAPZB': 1, 'KIF3C': 1, 'COPA': 1, 'YKT6': 1, 'COPB1': 1, 'KIF20A': 1, 'RAB1A': 1, 'ACTR1A': 1, 'CAPZA2': 1, 'CAPZA1': 1, 'SPTBN5': 1, 'USO1': 1, 'CENPE': 1, 'KIFC1': 1, 'KIF9': 1, 'ARF1': 1, 'KIF6': 1, 'KIF18B': 1, 'KIF26A': 1, 'KIF5A': 1, 'KIF20B': 1, 'ANK1': 1, 'KIF18A': 1, 'KIF19': 1, 'KIF23': 1, 'BET1': 1, 'KDELR1': 1, 'ANK3': 1, 'SPTBN1': 1, 'KIF5B': 1, 'COPB2': 1, 'KIF11': 1, 'KIF2C': 1, 'ARCN1': 1, 'DYNLL2': 1, 'COPG2': 1, 'KIFC2': 1, 'COPG1': 1, 'KIF26B': 1, 'DYNLL1': 1, 'ARFGAP2': 1, 'KIF2A': 1, 'KIF22': 1, 'KIF12': 1, 'SEC22B': 1, 'ARFGAP3': 1, 'KDELR3': 1, 'KIF3A': 1, 'COPZ1': 1, 'COPE': 1, 'DCTN1': 1}, 'FKBP4': {'HSF1': 1, 'PPP5C': 1, 'ACTR1A': 1, 'HSPA1A': 1, 'PPID': 1, 'HSPA2': 1, 'PTGES3': 1, 'HSP90AA1': 1, 'HSP90AB1': 1, 'STIP1': 1, 'HSPA8': 1, 'EP300': 1, 'FOXA1': 1, 'HSPA1L': 1, 'ESR2': 1, 'MAPK3': 1, 'HSPA1B': 1, 'DCTN1': 1}
I am stuck at the end, it would be necessary that my algorithm by testing all possible pairs of proteins tells me 1 if there is interaction between the two proteins or 0 if there is none.

To do this, in the last lines of my algorithm, I have to compare the pair i, j with the pairs of proteins interacting with each other in my Dinter dictionary.

unfortunately it does not work with my algorithm, can you help me?

Thank you
Please don't use i, l, or O as single digit variable names as they can look like numbers. To find keys common to both dictionaries:

for key in Dprot1: #We go through all the elements of Dprot1
    if key in Dprot2:
        print("intersection found")
You can also use 2 sets instead of dictionaries, and set.intersection