def calcReferenceRms(self, reference, keepH=False, debug=False):
     ref = getCoords(getLines(reference), include_hydrogens = keepH)
     if not len(ref['coord']) == len(self.results[0]['coord']):
         print "[atoms mismatch] Warning! The reference stricture doesn't match the docked ligand!"
         print "Reference [ %d ] | Docked ligand [ %d ]" % (  len(ref['coord']), len(self.results[0]['coord']) )
         return 
     rmsdcalc = RMSDCalculator(ref['coord'])
     rmsd = [ rmsdcalc.computeRMSD(self.results[0]['coord']) ]
     if len(self.results) > 1:
         rmsd.append(rmsdcalc.computeRMSD(self.results[1]['coord']))
     if debug:
         dist_pool = []
         for px in self.poses:
             p = px['coord']
             d = []
             for i in range(len(p)):
                 x = quickdist(p[i], ref['coord'][i], sq=True)
                 d.append(x)
                 d.sort()
             dist_pool.append(d)
             #print "================="
             #print d
         return rmsd, dist_pool,self.poses
     else:
         return rmsd
    def setReceptor(self, receptor, recname=None):
        # pre-process receptor if filename
        if isinstance(receptor, str):
            if recname == None:
                r = os.path.basename(receptor)
                recname = os.path.splitext(r)[0]
            try:
                receptor = getCoords(getLines(receptor))
            except:
                print "Problem in reading the receptor: %s", receptor
                return False

        self.receptor = receptor
        self.recname = recname

        # initialize the bh_tree for the 
        # interactions calculated later
        try:
            freeBHtree(self.receptor_bht)
        except:
            pass
        self.rec_bht           = bhtreelib.BHtree( self.receptor['coord'], None, 10)
        self.rec_bht_indices   = zeros( (len(self.receptor['coord']),) ).astype('i')
        self.rec_bht_distances = zeros( (len(self.receptor['coord']),) ).astype('f') 
        pattern = opts['-s'] 
    if DEBUG: print "Pattern : |%s|" % pattern


    if '-f' in opts:
        files = [ opts['-f'] ] 
    elif '-d' in opts:
        if '-R' in opts:
            recursive = True
        files = pathToList( path = opts['-d'], recursive = recursive, pattern = pattern )
        if not files:
            print "WARNING: no VS results in the dir:", opts['-d'] 
            exit(1)
    elif '-F' in opts:
        try:
            files = getLines(opts['-F'], doStrip = True)
            print "%d files from list file %s" % (len(files), opts['-F'])
        except:
            print "ERROR opening the input list: " , opts['-F']
            exit(1)
    else:
        print "ERROR: Either file (-f), directory (-d) or list file (-F) input options must be specified"
        usage()
        exit(1)

    if '-R' in opts and ('-f' in opts or '-F' in opts):
        print "WARNING: recursive option ('-R') ignored in file/list mode."

    # interaction filters are initialized
    int_filter = { 'vdw' : [],
                   'hba': [],
     else:
         pworst = float(value)
 
 if '-s' in opts:
     pattern =  opts['-s']
     if DEBUG: print "filename pattern search>", pattern
 if '-f' in opts:
     files = [ opts['-f'] ] 
 elif '-d' in opts:
     files = pathToList( path = opts['-d'], recursive = recursive, pattern = pattern )
     if not files:
         print "No VS results in the dir:", opts['-d'] 
         exit(1)
 elif '-F' in opts:
     try:
         files = getLines(opts['-L'])
     except:
         print "ERROR opening the input list: " % opts['-L']
         exit(1)
 else:
     print "ERROR: Either file (-f), directory (-d) or list file (-F) input options must be specified"
     usage()
     exit(1)
     
 # interaction filters are initialized
 int_filter = { 'vdw' : [],
                'hba': [],
                'hbd': [],
                'hb': [],
                'pi'  : [],
                'ppi' : [],
 def getPoses(self, include_hydrogens=False):
     """
     - parser of docked poses from a Vina output
     - populate self.atomTypes[]
     - populate poses dictionary: { "text", "coord", "energy", 'source'}
                   ( for every pose the 'source' specify dlg containing it and the position)
     """
     accepted_kw = [ "ATOM", "HETATM", "ROOT", "ENDROOT", "BRANCH", "ENDBRANCH", "TORSDOF", "REMARK"  ]
     atype_list_complete = False
     inside = False
     c = 0
     lines = getLines(self.input_files)
     if "REMARK VINA RESULT" in lines[1]:
         if DEBUG: print "[found Vina result pdbqt]"
         for l in lines:
             if l.strip():
                 if "MODEL" in l: # initialize pose
                     text_pose = []
                     coord = []
                     flex_res = []
                     c += 1
                     in_res = False
                 elif "ENDMDL" in l:
                     coord = array( coord, 'f')
                     if flex_res:
                         flex_res = getCoords(flex_res) # transform coordinates and process them
                                                        # as we do with receptor atoms   
                     # flex res atoms are added to the pose here
                     self.poses.append( { "text" : text_pose, "coord" : coord, "energy" : e ,\
                             'source' : (os.path.basename(self.input_files)+":"+str(c)), "flex_res" : flex_res } ) 
                     atype_list_complete = True
                 elif "BEGIN_RES" in l:  
                     in_res = True
                     self.flexres = True # flex_res trigger
                     flex_res.append(l)
                     text_pose.append(l)
                 elif "END_RES" in l: 
                     in_res = False
                     flex_res.append(l)
                     text_pose.append(l)
                 elif l.startswith("ATOM") or l.startswith("HETATM"):
                     text_pose.append(l)
                     if not in_res:
                         atype = l.rsplit()[-1]
                         if (not atype == "HD") or include_hydrogens : # HD should be excluded from array/RMSD calculation
                             try:
                                 coord.append([float(l[30:38]),float(l[38:46]),float(l[46:54])])
                                 if not atype_list_complete: # FUTURE: to be used for the David's reclustering method
                                     self.atomTypes.append( atype ) 
                             except:
                                 print ">WARNING! error in parsing coords in file :",self.input_files
                                 self.problematic.append(self.input_files)
                                 break 
                     else:
                         flex_res.append(l)
                 elif "REMARK VINA RESULT:" in l:
                     values = l.split(":")[1]
                     values = values.split()
                     e = float(values[0])
                     lbrms = float(values[1])
                     ubrms = float(values[2])
                     self.histogram.append([e, lbrms, ubrms])
                 elif l.split(None, 1)[0] in accepted_kw:
                     text_pose.append(l)
         self.totRuns = len(self.poses)
     else:
         print ">WARNING! not a Vina output PDBQT file :",self.input_files
 def getPoses(self, include_hydrogens=False, ignoreWaters = False):
     """
     - parser of docked poses from the dlg
     - populate self.atomTypes[]
     - define self.ligName (str)
     - populate poses dictionary: { "text", "coord", "energy", 'source'}
                   ( for every pose the 'source' specify dlg containing it and the position)
     """
     #accepted_kw = [ "ATOM", "HETATM", "ROOT", "ENDROOT", "BRANCH", "ENDBRANCH", "TORSDOF", "REMARK"  ]
     accepted_kw = [ "ATOM", "HETATM", "TORSDOF"  ]
     atype_list_complete = False
     #water_found = False
     for d in self.input_files: # we expect dlgs
         inside = False
         c = 0
         for l in getLines(d)[50:]:
             if l[0:7] == "DOCKED:":
                 inside = True # used to stop parsing as soon as another keyword (i.e. "DPF> ") is found
                 l = l[8:]
                 if "MODEL" in l: # initialize pose
                     text_pose = []
                     coord = []
                     flex_res = []
                     water = []
                     c += 1
                     in_res = False
                 elif "ENDMDL" in l:
                     coord = array( coord, 'f')
                     if len(coord) == 0:
                         if DEBUG: print "[ Warning! no coordinates found in this dlg: %s ]" % (d)
                         break
                     if flex_res:
                         flex_res = getCoords(flex_res) # transform coordinates and process them
                                                             # as we do with receptor atoms   
                     # flex res atoms are added to the pose here
                     self.poses.append( { "text"         : text_pose, 
                                          "coord"        : coord, 
                                          "energy"       : e ,
                                          'source'       : (os.path.basename(d)+":"+str(c)), 
                                          "flex_res"     : flex_res,
                                          "water_bridge" : water,
                                        } ) 
                     atype_list_complete = True
                 elif "BEGIN_RES" in l:  
                     in_res = True
                     self.flexres = True # flex_res trigger
                     flex_res.append(l)
                     text_pose.append(l)
                 elif "END_RES" in l: 
                     in_res = False
                     flex_res.append(l)
                     text_pose.append(l)
                 elif l.startswith("ATOM") or l.startswith("HETATM"): # if the line is atom but *not* flexres
                     text_pose.append(l)
                     if not in_res:
                         atype = l.rsplit()[-1]
                         if (atype == 'HD' and include_hydrogens) or not ( atype in self.ignore_at):
                             try:
                                 # NOTE the flex res are not considered in the clustering
                                 coord.append([float(l[30:38]),float(l[38:46]),float(l[46:54])])
                                 if not atype_list_complete: # FUTURE: to be used for the David's reclustering method
                                     self.atomTypes.append( atype ) 
                             except:
                                 print ">WARNING! error in parsing coords in file :",d
                                 self.problematic.append(d)
                                 # the entire dlg is ignored if something bad happens
                                 # rude, but efficient on the large scale...
                                 break 
                         elif atype in self.water_types and not ignoreWaters:
                             water.append(l)
                             self.isHydrated = True
                     else:
                         flex_res.append(l)
                 elif "USER    Estimated Free Energy of Binding    =" in l:
                     e = l.split("=")[1]
                     e = float(e.split("k")[0])
                     if e == 0.:
                         if DEBUG: print "[ Warning! FEB is zero... and it shouldn't ]"
                         
                 elif l.split(None, 1)[0] in accepted_kw:
                     text_pose.append(l)
             elif l.startswith("DPF> move") and not self.ligName:
                 self.ligName = l.split("DPF> move ")[1].split(".pdbqt")[0]
             if l.startswith("DPF >") and inside: # stop reading dlg file (average lines skipped: ~30%)
                 break
         self.totRuns = len(self.poses) # TODO check this indentation?
         # Debugging printing of poses
         if self.DEBUG:
             for p in self.poses:
                 for f in p:
                     print f
                     print p[f]
                     print "==================================="