[Tutor] reg exps

Kim Branson kim.branson at csiro.au
Wed Feb 4 08:34:12 EST 2004


Hi all,

i'm trying to get my head around regexps in python.
i've now found the difference between the .match and the .search 
methods.  (i'm used to perl :)

i have a program which will spit out data like so:
% ~/Desktop/Dock4_osx/bin/scorer 1rx3.pdb dock_nrg.mol2
  PK=  6.08 Qual=  2.12 PMF= -159.15 PMF_rb= -144.15 SMoG= -161.27 
SMoG_H= -7.68 ChemScore= -23.86 Clash=  0.85 Int=  2.58 DockNRG= -24.51 
AutoDock= -20.14

so i'm working on a script which has a function (below) that checks for 
this data (the program called can spit out other data when inputs are 
bad) then grabs the matches.  so now i'm making dictionaries for each 
field, using the line number as a key. my pattern match does not return 
the first match in the list in position 0.

i.e
[' ', ' 6.08', ' 2.12', '-159.15', '-144.15', '-161.27', '-7.68', 
'-23.86', ' 0.85', ' 2.58', '-24.51', '-20.14', '\n']

so i'm grabbing the data from position 1 in the list etc, and working 
from there. Why is this, is this a default behaviour?
note the values in the output can be negative or really large, as in 
PK= -6.08 etc, or Qual= 12.12 so i use (.*) to grab the region.

Oh one more thing, if you declare a global, can i simply add dictionary 
content, or should one declare and then initialise?


def score_orientations():
     counter = 1
     global score_dict
     global qual_dict
     global pmf_dict
     global pmf_rb_dict
     global smog_dict
     global smog_h_dict
     global chemscore_dict
     global clash_dict
     global int_dict
     global docknrg_dict
     global autodock_dict
     score_dict = {}
     qual_dict = {}
     pmf_dict = {}
     pmf_rb_dict = {}
     smog_dict = {}
     smog_h_dict = {}
     chemscore_dict = {}
     clash_dict = {}
     int_dict = {}
     docknrg_dict = {}
     autodock_dict = {}
     score_lines = re.compile('PK= (.*) Qual= (.*) PMF= (.*) PMF_rb= 
(.*) SMoG= (.*) SMoG_H= (.*) ChemScore= (.*) Clash= (.*) Int= (.*) 
DockNRG= (.*) AutoDock= (.*)')
     results = os.popen2('/Users/kbranson/Desktop/Dock4_osx/bin/scorer 
%s dock_nrg.mol2' % receptor_pdb)
     results_data = results[1].readlines()
     for  line in results_data:
          if (score_lines.search(line)):
             line = re.split(score_lines, line)
             score_dict[counter] = line[1]
             qual_dict[counter] = line[2]
             pmf_dict[counter] = line[3]
             pmf_rb_dict[counter] = line[4]
             smog_dict[counter] = line[5]
             smog_h_dict[counter] = line[6]
             chemscore_dict[counter] = line[7]
             clash_dict[counter] = line[8]
             int_dict[counter] = line[9]
             docknrg_dict[counter] = line[10]
             autdock_dict[counter] = line[11]
             counter = counter + 1
     print score_dict




score_orientations()






More information about the Tutor mailing list