Python-Ref > Cheminformatics > Molecular fingerprints > How fingerprints work
 
 

<-^^->

How fingerprints work

We demonstrate the workings of fingerprints by developing our own naive implementation.
Expand/Shrink
  1   from oasa import smiles
  2   from oasa import periodic_table as pt
  3   import operator
  4   
  5   def atom_to_num( a):
  6     return pt.periodic_table[a.symbol]['ord']
  7   
  8   def get_fp( mol):
  9     frags = []
 10     for atom1 in mol.atoms:
 11       for edge2, atom2 in atom1.get_neighbor_edge_pairs():
 12         frags.append( (atom_to_num(atom1),edge2.order,atom_to_num(atom2)))
 13     fps2 = set( [sum( map( operator.mul, frag, [3,5,3])) % 256 for frag in frags])
 14     frags = []
 15     for atom1 in mol.atoms:
 16       for edge2, atom2 in atom1.get_neighbor_edge_pairs():
 17         for edge3, atom3 in atom2.get_neighbor_edge_pairs():
 18           if atom1 is not atom3:
 19             frags.append( (atom_to_num(atom1),edge2.order,atom_to_num(atom2),edge3.order,atom_to_num(atom3)))
 20     fps3 = set( [sum( map( operator.mul, frag, [3,5,7,5,3])) % 256 for frag in frags])
 21     return fps2 | fps3
 22   
 23   mol = smiles.text_to_mol( "O=CCC=CCO")
 24   fp1= get_fp( mol)
 25   print fp1
 26   
 27   mol = smiles.text_to_mol( "O=CC")
 28   fp2 = get_fp( mol)
 29   print fp2
 30   
 31   print fp1 & fp2 == fp2
 32   
 33     
 34       
stdout:
set([99, 41, 46, 47, 52, 88, 93, 94])
set([41, 99, 52])
True
Doba běhu: 131.6 ms