from __future__ import division
import olex
import olex_fs
import glob
import olx
import os
import time
import math
import olex_core
import sys
import programSettings
from subprocess import *
import htmlTools
import HttpTools
global _is_online
_is_online = False
sys.path.append(r".\src")
import History
import ExternalPrgParameters
SPD, RPD = ExternalPrgParameters.SPD, ExternalPrgParameters.RPD
from olexFunctions import OlexFunctions
OV = OlexFunctions()
import variableFunctions
import MakeMovie
import OlexVFS
haveGUI = OV.HasGUI()
if __debug__:
#gc.set_debug(gc.DEBUG_LEAK | gc.DEBUG_STATS)
#gc.set_debug(gc.DEBUG_STATS)
#gc.set_debug(gc.DEBUG_SAVEALL | gc.DEBUG_STATS)
def dump():
#a = gc.get_threshold()
dump_garbage()
#a = gc.garbage
#b = gc.collect()
OV.registerFunction(dump)
def collect(generation=None):
print gc.get_count()
if generation != None:
a = gc.collect(generation)
else:
a = gc.collect()
print "%s garbage items collected" %a
OV.registerFunction(collect)
def getObjectCount():
#a = gc.get_objects()
a = get_all_objects()
#a = []
print "Number of objects: %s" %len(a)
#print a[0]
#print a[50]
#print a[-10]
#print "\n\n"
a = []
gc.collect()
return ''
OV.registerFunction(getObjectCount)
def dump_garbage():
"""
show us what's the garbage about
"""
# force collection
print "\nGARBAGE:"
a = gc.collect()
print "%s objects collected" %a
print "\nGARBAGE OBJECTS:"
for x in gc.garbage:
s = str(x)
if len(s) > 80: s = s[:80]
print type(x),"\n ", s
if type(x).__name__ == 'function':
print x.func_code
#print x.func_name
print 'Size of garbage is: ',len(gc.garbage)
# Recursively expand slist's objects
# into olist, using seen to track
# already processed objects.
def _getr(slist, olist, seen):
for e in slist:
if id(e) in seen:
continue
seen[id(e)] = None
olist.append(e)
tl = gc.get_referents(e)
if tl:
_getr(tl, olist, seen)
# The public function.
def get_all_objects():
"""Return a list of all live Python
objects, not including the list itself."""
gcl = gc.get_objects()
olist = []
seen = {}
# Just in case:
seen[id(gcl)] = None
seen[id(olist)] = None
seen[id(seen)] = None
# _getr does the real work.
_getr(gcl, olist, seen)
return olist
# -*- Mode: Python; tab-width: 4 -*-
import types
def get_refcounts():
d = {}
sys.modules
# collect all classes
for m in sys.modules.values():
for sym in dir(m):
o = getattr (m, sym)
if type(o) is types.ClassType:
d[o] = sys.getrefcount (o)
# sort by refcount
pairs = map (lambda x: (x[1],x[0]), d.items())
pairs.sort()
pairs.reverse()
return pairs
def print_top_100():
for n, c in get_refcounts()[:100]:
print '%10d %s' % (n, c.__name__)
OV.registerFunction(print_top_100)
#if headless:
#from olexexc import *
#else:
#from olexexh import *
#GuiFunctions.registerFunctions()
class SpyVar(object):
MatchedFragments = {}
MatchedRms = []
class OlexRefinementModel(object):
restraint_types = {
'dfix':'bond',
'dang':'bond',
'flat':'planarity',
'chiv':'chirality',
'sadi':'bond_similarity',
'simu':'adp_similarity',
'delu':'rigid_bond',
'isor':'isotropic_adp',
'olex2.restraint.angle':'angle',
'olex2.restraint.dihedral':'dihedral',
'olex2.restraint.u_eq':'fixed_u_eq_adp',
'olex2.restraint.u_eq.similar':'u_eq_similarity',
}
constraint_types = {
'eadp':'adp',
'exyz':'site',
}
def __init__(self):
olex_refinement_model = OV.GetRefinementModel(True)
self._atoms = []
self._fixed_variables = {}
self.atom_ids = []
asu = olex_refinement_model['aunit']
for residue in asu['residues']:
for atom in residue['atoms']:
self._atoms.append(atom)
element_type = atom['type']
self.atom_ids.append(atom['aunit_id'])
vars = olex_refinement_model['variables']['variables']
if len(vars) > 0:
for var in vars[0]['references']:
self._fixed_variables.setdefault(var['id'], [])
self._fixed_variables[var['id']].append(var)
self._cell = olex_refinement_model['aunit']['cell']
self.exptl = olex_refinement_model['exptl']
self._afix = olex_refinement_model['afix']
self.model= olex_refinement_model
def atoms(self):
return self._atoms
def disorder_parts(self):
return [atom['part'] for atom in self._atoms]
def iterator(self):
for i, atom in enumerate(self._atoms):
name = str(atom['label'])
element_type = str(atom['type'])
xyz = atom['crd'][0]
occu = atom['occu'][0]
adp = atom.get('adp')
if adp is None:
uiso = atom.get('uiso')[0]
u = (uiso,)
else: u = adp[0]
uiso_owner = atom.get('uisoOwner')
if name[:1] != "Q":
yield name, xyz, occu, u, uiso_owner, element_type, self._fixed_variables.get(i)
def afix_iterator(self):
for afix in self._afix:
mn = afix['afix']
m, n = divmod(mn, 10)
pivot = afix['pivot']
dependent = afix['dependent']
pivot_neighbours = [
i for i in self._atoms[pivot]['neighbours']
if not i in dependent]
if len(dependent) == 0: continue
dependent_part = self._atoms[dependent[0]]['part']
#pivot_neighbours = None
bond_length = afix['d']
uiso = afix['u']
yield m, n, pivot, dependent, pivot_neighbours, bond_length
def restraints_iterator(self):
from libtbx.utils import flat_list
from cctbx import sgtbx
for shelxl_restraint in (self.restraint_types):
for restraint in self.model.get(shelxl_restraint, ()):
restraint_type = self.restraint_types.get(shelxl_restraint)
if restraint_type is None: continue
i_seqs = [i[0] for i in restraint['atoms']]
kwds = dict(i_seqs=i_seqs)
if restraint_type not in (
'adp_similarity', 'u_eq_similarity', 'rigid_bond', 'isotropic_adp', 'fixed_u_eq_adp'):
kwds['sym_ops'] = [
(sgtbx.rt_mx(flat_list(i[1][:-1]), i[1][-1]) if i[1] is not None else None)
for i in restraint['atoms']]
if restraint_type in ('angle', 'dihedral'):
esd_val = restraint['esd1']*180/math.pi
else:
esd_val = restraint['esd1']
kwds['weight'] = 1/math.pow(esd_val,2)
value = restraint['value']
if restraint_type in ('adp_similarity', 'u_eq_similarity', 'isotropic_adp', 'fixed_u_eq_adp'):
kwds['sigma'] = restraint['esd1']
if restraint_type in ('adp_similarity', 'isotropic_adp'):
kwds['sigma_terminal'] = restraint['esd2'] if restraint['esd2'] != 0 else None
elif restraint_type == 'rigid_bond':
kwds['sigma_12'] = restraint['esd1']
kwds['sigma_13'] = restraint['esd2'] if restraint['esd2'] != 0 else None
if restraint_type == 'bond':
kwds['distance_ideal'] = value
elif restraint_type in ('angle', 'dihedral'):
kwds['angle_ideal'] = value
elif restraint_type in ('fixed_u_eq_adp',):
kwds['u_eq_ideal'] = value
elif restraint_type in ('bond_similarity', 'planarity'):
kwds['weights'] = [kwds['weight']]*len(i_seqs)
if restraint_type == 'bond_similarity':
sym_ops = kwds['sym_ops']
kwds['i_seqs'] = [[i_seq for i_seq in i_seqs[i*2:(i+1)*2]]
for i in range(int(len(i_seqs)/2))]
kwds['sym_ops'] = [[sym_op for sym_op in sym_ops[i*2:(i+1)*2]]
for i in range(int(len(sym_ops)/2))]
if 'weights' in kwds:
del kwds['weight']
if restraint_type in ('bond', ):
for i in range(int(len(i_seqs)/2)):
yield restraint_type, dict(
weight=kwds['weight'],
distance_ideal=kwds['distance_ideal'],
i_seqs=kwds['i_seqs'][i*2:(i+1)*2],
sym_ops=kwds['sym_ops'][i*2:(i+1)*2])
else:
yield restraint_type, kwds
def constraints_iterator(self):
from libtbx.utils import flat_list
from cctbx import sgtbx
for shelxl_constraint in (self.constraint_types):
for constraint in self.model[shelxl_constraint]:
constraint_type = self.constraint_types.get(shelxl_constraint)
if constraint_type is None: continue
if constraint_type == "adp":
i_seqs = [i[0] for i in constraint['atoms']]
kwds = dict(i_seqs=i_seqs)
else:
kwds = {"i_seqs": constraint}
yield constraint_type, kwds
def getCell(self):
return [self._cell[param][0] for param in ('a','b','c','alpha','beta','gamma')]
def getCellErrors(self):
return [self._cell[param][1] for param in ('a','b','c','alpha','beta','gamma')]
def numberAtoms(self):
return sum(atom['occu'][0] for atom in self.atoms())
def number_non_hydrogen_atoms(self):
return sum(atom['occu'][0] for atom in self.atoms() if atom['type'] not in ('H','Q'))
def currentFormula(self):
curr_form = {}
for atom in self.atoms():
atom_type = atom['type']
atom_occu = atom['occu'][0]
curr_form.setdefault(atom_type, 0)
curr_form[atom_type] += atom_occu
return curr_form
def getExpectedPeaks(self):
cell_volume = float(olx.xf_au_GetVolume())
expected_atoms = cell_volume/15
#present_atoms = self.numberAtoms()
present_atoms = self.number_non_hydrogen_atoms()
expected_peaks = expected_atoms - present_atoms
if expected_peaks < 5: expected_peaks = 5
return int(expected_peaks)
##
def get_refine_ls_hydrogen_treatment():
afixes_present = []
afixes = {0:'refall',
1:'noref',
2:'refxyz',
3:'constr',
4:'constr',
5:'constr',
7:'constr',
8:'constr',
}
for atom in OlexRefinementModel().atoms():
if atom['type'] == 'H':
afix = atom['afix']
n = int(afix[-1])
if len(afix) > 1:
m = int(afix[:-1])
else:
m = 0
if not afixes[n] in afixes_present:
afixes_present.append(afixes[n])
if len(afixes_present) == 0:
return 'undef'
elif len(afixes_present) == 1:
return afixes_present[0]
else:
return 'mixed'
OV.registerFunction(get_refine_ls_hydrogen_treatment)
def GetAvailableRefinementProgs():
retStr = "cctbx LBFGS<-cctbx;"
retStr += "ShelXL L.S.;"
retStr += "ShelXL CGLS;"
retStr += "ShelXH L.S.;"
retStr += "ShelXH CGLS;"
if OV.IsPluginInstalled('ODAC'):
retStr+= "cctbx AutoChem<-cctbx AutoChem"
return retStr
OV.registerFunction(GetAvailableRefinementProgs)
def GetAvailableSolutionProgs():
retStr = "cctbx;"
a = olx.file_Which('XS.exe')
if a == "":
a = olx.file_Which('ShelXS.exe')
if a:
retStr += "ShelXS;"
return retStr
OV.registerFunction(GetAvailableSolutionProgs)
def OnMatchStart(argStr):
OV.write_to_olex('match.htm', "RMS (Å) Matched Fragments
")
SpyVar.MatchedFragments = {}
SpyVar.MatchedRms = []
return ""
if haveGUI:
OV.registerCallback('startmatch',OnMatchStart)
def OnMatchFound(rms, fragA, fragB):
fragA = "'%s'" %fragA.replace(",", " ")
fragB = "'%s'" %fragB.replace(",", " ")
fragL = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U']
if len(SpyVar.MatchedFragments) > 16:
return
if fragA not in SpyVar.MatchedFragments:
idA = fragL[len(SpyVar.MatchedFragments)]
SpyVar.MatchedFragments.setdefault(fragA, {'fragID':idA})
else:
idA = SpyVar.MatchedFragments[fragA].get('fragID')
if fragB not in SpyVar.MatchedFragments:
idB = fragL[len(SpyVar.MatchedFragments)]
SpyVar.MatchedFragments.setdefault(fragB, {'fragID':idB})
else:
idB = SpyVar.MatchedFragments[fragB].get('fragID')
rms = float(rms)
SpyVar.MatchedRms.append(rms)
outStr = ""
try:
outStr += olex.readImage('match.htm')
except:
pass
outStr+=''
if rms < 0.2:
outStr += '%.4f ' %(rms)
elif rms < 1:
outStr += '%.4f ' %(rms)
elif rms < 2:
outStr += '%.4f ' %(rms)
else:
outStr += '%.4f ' %(rms)
outStr+=' %s ' %(fragA, idA)
outStr+='%s ' %(fragB, idB)
outStr+="
"
OV.write_to_olex('match.htm',outStr)
if haveGUI:
OV.registerCallback('onmatch',OnMatchFound)
#def getScreenSize():
#retval = ()
#from win32api import GetSystemMetrics
#width = GetSystemMetrics (0)
#height = GetSystemMetrics (1)
#retval = (width, height)
#print retval
#olx.SetVar("screen_width", width)
#olx.SetVar("screen_height", height)
#return "OK"
#OV.registerFunction(getScreenSize)
def SetFormulaFromInput():
formula = olx.GetValue('SET_FORMULA')
if not formula:
return
f = formula.split()
Z = float(olx.xf_au_GetZ())
argStr = ""
for element in f:
try:
n = int(element[1:])*Z
el = element[:1]
except:
n = int(element[2:])*Z
el = element[:2]
argStr += "%s:%i," %(el, n)
argStr = argStr.strip(',')
argStr = "'%s'" %argStr
olx.xf_SetFormula(argStr)
return ""
if haveGUI:
OV.registerFunction(SetFormulaFromInput)
#def suvvd():
#from RunPrg import RunPrg
#a = RunPrg()
#a.save_user_parameters()
#print "The current settings have been saved for this user"
#return ""
#OV.registerFunction(suvvd)
def ChooseLabelContent(cmd):
s = ""
switches = cmd.split()
for switch in switches:
s += "-%s " %switch
olx.Labels(s)
return ""
OV.registerFunction(ChooseLabelContent)
def FindZOfHeaviestAtomInFormua():
from PeriodicTable import PeriodicTable
retVal = 0
PT = PeriodicTable()
pt = PT.PeriodicTable()
f = olx.xf_GetFormula('list')
if not f:
return retVal
f = f.split(',')
largestZ = 0
for element in f:
ele = element.split(":")[0]
Z = int(pt[ele].get('Z'))
if Z > largestZ:
largestZ = Z
retVal = largestZ
return retVal
OV.registerFunction(FindZOfHeaviestAtomInFormua)
last_formula = None
def ElementButtonStates(symbol):
if OV.GetParam('olex2.full_mode') == 'name -t=%s' %symbol:
olex.m('mode off')
else:
if olex.f('Sel()') == '':
olex.m('mode name -t=%s' %symbol)
else:
olex.m('name sel %s' %symbol)
olex.m('sel -u')
if haveGUI:
OV.registerFunction(ElementButtonStates)
def MakeElementButtonsFromFormula():
global last_formula
from PilTools import ButtonMaker
icon_size = OV.GetParam('gui.html.icon_size')
totalcount = 0
btn_dict = {}
f = olx.xf_GetFormula('list')
if not f:
return
f = f.split(',')
current_formula = OlexRefinementModel().currentFormula()
Z_prime = float(olx.xf_au_GetZprime())
Z = float(olx.xf_au_GetZ())
html_elements = []
for element in f:
symbol = element.split(':')[0]
max = float(element.split(':')[1])
max = round(max, 2)
present = round(current_formula.get(symbol,0),2)
if symbol != "H":
totalcount += max
max = max*Z_prime
bgcolour = '#ff0000'
if present < max:
bgcolour = (250,250,250)
elif present == max:
bgcolour = (210,255,210)
else:
bgcolour = (255,210,210)
command = "if strcmp(sel(),'') then 'mode name -t=%s' else 'name sel %s'>>sel -u" %(symbol, symbol)
target = OV.TranslatePhrase('change_element-target')
#command = "if strcmp(spy.GetParam(olex2.in_mode),'mode name -t=%s') then 'mode off' else %%22 if strcmp(sel(),'') then 'mode name -t=%s' else 'name sel %s'>>sel -u%%22" %(symbol, symbol, symbol)
command = 'spy.ElementButtonStates(%s)' %symbol
namelower = 'btn-element%s' %symbol
d = {}
d.setdefault('namelower', namelower)
d.setdefault('symbol', symbol)
d.setdefault('cmds', command)
d.setdefault('target', target + symbol)
d.setdefault('bgcolor', OV.GetParam('gui.html.table_firstcol_colour'))
html = '''
''' %d
#
#
<-zimg border="0" src="#image.png"> |
%%setup-title-%s%% | |||
%%setup-txt-%s%%
Swap the position of the GUI panel Skin HP Skin OD |
%s%% | " %(font_size, col, R1) except: t = "%s | " %(font_size, R1) finally: return t else: if txt: t = "%s | " %txt else: try: look = olex.f('IsVar(snum_refinement_last_R1)') if look == "true": R1 = olex.f('GetVar(snum_refinement_last_R1)') else: if olx.IsFileType('cif') == "true": R1 = olx.Cif('_refine_ls_R_factor_gt') else: R1 = olex.f('Lst(R1)') except: R1 = 0 try: R1 = float(R1) col = GetRcolour(R1) R1 = "%.2f" %(R1*100) t = r"%s%% | " %(col, R1) except: t = "%s | " %R1 #wFile = open(r"%s/displayR.htm" %olx.StrDir(), 'w') #wFile.write(t) #wFile.close() return t OV.registerFunction(GetRInfo) def GetRcolour(R1): retVal = "" try: R1 = float(R1) if R1 > 0.20: retVal=OV.GetParam('gui.red') elif R1 >0.10: retVal=OV.GetParam('gui.orange') else: retVal=OV.GetParam('gui.green') except: retVal='grey' return str(retVal) def setMainToolbarTabButtons(btn, state=""): isCif = OV.IsFileType('cif') if isCif and btn != 'report': state = 'inactive' btns = [("solve", "solution-settings"), ("refine", "refinement-settings"), ("report","report-settings")] for item in btns: if item[0] == btn: if not state: state = olx.html_GetItemState(item[1]) if state == '-1': state = "off" elif state == '0': state = "on" #OV.CopyVFSFile("cbtn-%s2%s.png" %(item[0],state),"cbtn-%s2.png" %item[0]) OV.SetImage("IMG_CBTN-%s2" %btn.upper(),"cbtn-%s2%s.png" %(item[0], state)) OV.SetImage("IMG_BTN-%s2" %btn.upper(),"btn-%s2%s.png" %(item[0], state)) OV.SetVar('gui_MainToolbarTabButtonActive',btn) elif state != 'inactive' and not isCif: OV.CopyVFSFile("cbtn-%s2off.png" %item[0],"cbtn-%s2.png" %item[0]) return "Done" if haveGUI: OV.registerFunction(setMainToolbarTabButtons) def setAllMainToolbarTabButtons(): isCif = OV.IsFileType('cif') btns = [("solve", "solution-settings"), ("refine", "refinement-settings"), ("report","report-settings")] for item in btns: btn = item[0] if isCif and btn != 'report': state = 'inactive' if olx.html_IsItem(item[1]) == 'true': olx.html_ItemState(item[1],'-1') else: state = '' #state = 'off' if not state: #if OV.IsControl(item[1]): if olx.html_IsItem(item[1]) == 'true': try: state = olx.html_GetItemState(item[1]) except RuntimeError: pass if state == '-1': state = "off" elif state == '0': state = "on" elif state == '1': state = "off" else: state = 'off' OV.CopyVFSFile("cbtn-%s%s.png" %(btn,state),"cbtn-%s.png" %btn) if state == 'on': OV.SetVar('gui_MainToolbarTabButtonActive',btn) return "Done" if haveGUI: OV.registerFunction(setAllMainToolbarTabButtons) def onCrystalColourChange(): if variableFunctions.initialisingVariables: return lustre = OV.FindValue('snum_metacif_exptl_crystal_colour_lustre') modifier = OV.FindValue('snum_metacif_exptl_crystal_colour_modifier') primary = OV.FindValue('snum_metacif_exptl_crystal_colour_primary') colour = ' '.join(item for item in (lustre,modifier,primary) if item != '?') if colour: OV.SetVar('snum_metacif_exptl_crystal_colour', colour) OV.registerFunction(onCrystalColourChange) def onRefinementProgramChange(prg_name, method=None, scope='snum'): if prg_name == 'Auto': OV.SetParam('autochem.refinement.method','Auto') return prg = RPD.programs[prg_name] if method is None or not method: method = sortDefaultMethod(prg) if method == 'Least Squares' and olx.LSM() == 'CGLS': method = 'CGLS' # work-around for bug #26 OV.SetParam("%s.refinement.method" %scope, method) onRefinementMethodChange(prg_name, method) OV.registerFunction(OV.set_refinement_program) def onRefinementMethodChange(prg_name, method): if method in RPD.programs[prg_name].methods: programSettings.doProgramSettings(prg_name, method) else: print "Please choose a valid method for the refinement program %s" %prg_name OV.registerFunction(onRefinementMethodChange) def onSolutionProgramChange(prg_name, method=None, scope='snum'): if prg_name == "Auto": OV.SetParam('autochem.solution.method','Auto') #olx.SetValue('SET_autochem_solution_METHOD', 'Auto') return prg = SPD.programs[prg_name] if method is None or not method: method = sortDefaultMethod(prg) if method == 'Direct Methods' and olx.Ins('PATT') != 'n/a': method = 'Patterson Method' # work-around for bug #48 OV.SetParam("%s.solution.method" %scope, method) onSolutionMethodChange(prg_name, method) OV.registerFunction(OV.set_solution_program) def onSolutionMethodChange(prg_name, method): if method in SPD.programs[prg_name].methods: programSettings.doProgramSettings(prg_name, method) else: print "Please choose a valid method for the solution program %s" %prg_name return OV.registerFunction(onSolutionMethodChange) def sortDefaultMethod(prg): methods = [] for method in prg: order = method.order methods.append((order, method)) methods.sort() default = methods[0][1].name return default def get_solution_programs(scope='snum'): global available_solution_programs if available_solution_programs is None: p = [] for prg in SPD: a = which_program(prg) if not a: continue p.append(prg.name) p.sort() available_solution_programs = p else: p = available_solution_programs retval = ';'.join(p) if scope != 'snum': retval = 'Auto;' + retval return retval OV.registerFunction(get_solution_programs) def get_solution_methods(prg, scope='snum'): retval = "" if prg == '?': return retval if prg == 'Auto': return 'Auto' p = [] for method in SPD.programs[prg]: p.append(method.name) p.sort() for item in p: retval += "%s;" %item if scope != 'snum': retval = 'Auto;' + retval return retval OV.registerFunction(get_solution_methods) def which_program(prg): if "olex2" in prg.name.lower(): return True if prg.name in SPD or prg.name in RPD: exec_l = prg.execs else: exec_l = ["%s.exe" %prg, "%s" %prg, "%s" %prg.lower()] for item in exec_l: a = olx.file_Which('%s' %item) if a: break if 'wingx' in a.lower(): print "%s seems to be part of a WinGX installation. These ShelX executable cannot be used with Olex" %item return False return a OV.registerFunction(which_program) def getmap(mapName): if not OV.IsFileType('cif') and mapName != 'report': return '#map_%s' %mapName else: return '' if haveGUI: OV.registerFunction(getmap) available_refinement_programs = None available_solution_programs = None def get_refinement_programs(scope='snum'): global available_refinement_programs if available_refinement_programs is None: p = [] for prg in RPD: a = which_program(prg) if not a: continue p.append(prg.name) p.sort() available_refinement_programs = p else: p = available_refinement_programs retval = ';'.join(p) if scope != 'snum': retval = 'Auto;' + retval return retval OV.registerFunction(get_refinement_programs) def get_refinement_methods(prg, scope='snum'): retval = "" if prg == '?': return retval if prg == 'Auto': return 'Auto' p = [] for method in RPD.programs[prg]: p.append(method.name) p.sort() for item in p: retval += "%s;" %item if scope != 'snum': retval = 'Auto;' + retval return retval OV.registerFunction(get_refinement_methods) def QPeaksSlide(value): val = int(value) * 5 if val >= 0: val = 100 - val else: val = -100 - val return val OV.registerFunction(QPeaksSlide) def UisoSelectSlide(value): val = int(value) * 5 if val >= 0: val = 100 - val val = val * 0.001 val = "> %.3f" %val else: val = val * 0.001 * -1 val = "< %.3f" %val # print val return val OV.registerFunction(UisoSelectSlide) def createRMSDisplay(outStr): for rms in SpyVar.MatchedRms: if rms < 0.2: outStr += '%.4f ' %(rms) elif rms < 1: outStr += '%.4f ' %(rms) elif rms < 2: outStr += '%.4f ' %(rms) else: outStr += '%.4f ' %(rms) return outStr def haveSelection(): retVal = False res = OV.olex_function('sel()') if res == "": retVal = False else: retVal = True return retVal def install_plugin(plugin, args): user_alert_uninstall_plugin = OV.FindValue('user_alert_uninstall_plugin') if not plugin: return poke = olx.IsPluginInstalled("%s" %plugin) if poke == 'false': try: olex.m("installplugin %s" %plugin) pass except: print "The plugin %s does not exist" return if user_alert_uninstall_plugin[0] == 'R': delete = user_alert_uninstall_plugin[-1] elif user_alert_uninstall_plugin == 'Y': delete = OV.Alert('Olex2', "This will delete all files and folders of plugin '%s'. Are you sure you want to continue?" %plugin, 'YNIR', "(Don't show this warning again)") else: returnspy.install_plugin if 'Y' in delete: olex.m("installplugin %s" %plugin) pass if 'R' in delete: user_alert_uninstall_plugin = 'RY' self.setVariables('alert') variableFunctions.save_user_parameters('alert') OV.registerMacro(install_plugin, "") def runSadabs(): olx.User("'%s'" %OV.FilePath()) olx.Exec("sadabs") #olx.WaitFor('process') # uncomment me!http://dimas.dur.ac.uk/olex-distro-odac/1.0-alpha/Durham-OLEX2-Demo/AutoChem%20Installer.exe OV.registerFunction(runSadabs) def getKey(key_directory=None, specific_key = None): if sys.platform[:3] != 'win': return None keyPath = "%s/Olex2u/OD/%s" %(os.environ['ALLUSERSPROFILE'], OV.GetTag()) if not key_directory: key_directory = keyPath if specific_key: g = glob.glob(r"%s/%s.%s" %(key_directory, specific_key, "priv")) for item in g: return item.split("\\")[-1:][0] import glob g = glob.glob(r"%s/*.%s" %(key_directory, "priv")) for item in g: keyname = item.split("\\")[-1:][0] return keyname.split(".")[0] def getKeys(key_directory=None): keyPath = "%s/Olex2u/OD/%s" %(os.environ['ALLUSERSPROFILE'], OV.GetTag()) kl = [] if not key_directory: key_directory = keyPath import glob g = glob.glob(r"%s/*.%s" %(key_directory, "priv")) for item in g: keyname = item.split("\\")[-1:][0] kl.append(keyname.split(".")[0]) return kl def GetCheckcifReport(outputtype='PDF'): output = OV.GetParam('user.cif.chckCif_output_format') if output: outputtype = output file_name = os.path.normpath(olx.file_ChangeExt(OV.FileFull(),'cif')) if not os.path.exists(file_name): print "\n ++ There is no cif file to check! Please add the 'ACTA' command to Shelx!" return metacif_path = '%s/%s.metacif' %(OV.StrDir(), OV.FileName()) OV.CifMerge(metacif_path) rFile = open(file_name, 'rb') cif = rFile params = { "runtype": "symmonly", "referer": "checkcif_server", "outputtype": outputtype, "file": cif } response = HttpTools.make_url_call(OV.GetParam('olex2.checkcif.url'), params) rFile.close() #outputtype = 'htm' if outputtype == "htm": wFile = open("%s_cifreport.%s" %(OV.FileName(), outputtype),'w') wFile.write(response.read()) wFile.close() elif outputtype == "PDF": rawFile = open("raw_cifreport.htm",'w') l = response.readlines() for line in l: rawFile.write(line) if "Download checkCIF report" in line: href = line.split('"')[1] response = HttpTools.make_url_call(href,"") txt = response.read() wFile = open("%s_cifreport.%s" %(OV.FileName(), outputtype.lower()),'wb') wFile.write(txt) wFile.close() rawFile.close() olx.Shell("%s_cifreport.%s" %(OV.FileName(), outputtype.lower())) OV.registerFunction(GetCheckcifReport) def GetHttpFile(f, force=False, fullURL = False): URL = "http://dimas.dur.ac.uk/" global _is_online retVal = None go_online = _is_online verbose = OV.FindValue("ac_verbose", "False") if go_online or force: try: if not fullURL: url = "%s/%s" %(URL, f.replace(" ",r"%20")) else: url = f if verbose: print "--> Getting %s" %url, response = HttpTools.make_url_call(url,"") content = response.read() if verbose: print "OK" retVal = content except Exception, err: _is_online = False retVal = None print "Olex2 can not reach the update server: %s" %err print url else: retVal = None return retVal def check_for_recent_update(): retVal = False if OV.GetParam('olex2.has_recently_updated'): return True version = OV.GetSVNVersion() try: V = OV.FindValue('last_version','0') last_version = int(V) except Exception, err: print "Alert: Reset parameter 'last_version'" last_version = 0 OV.SetVar('last_version','0') # print "Last Version: %i"%last_version if version > last_version: OV.SetParam('olex2.has_recently_updated',True) retVal = True # print "Olex2 has recently been updated" else: OV.SetParam('olex2.has_recently_updated',False) retVal = False # print "Olex2 has not been updated" OV.SetVar('last_version',str(version)) OV.StoreParameter('last_version') return retVal def check_for_crypto(): pass #if olx.IsPluginInstalled(r"Crypto").lower() == 'false': #import olex #olex.m(r"InstallPlugin Crypto") #if olx.IsPluginInstalled(r"ODAC").lower() == 'false': #import olex #olex.m(r"InstallPlugin ODAC") def get_proxy_from_usettings(): rFile = open("%s/usettings.dat" %OV.BaseDir(),'r') lines = rFile.readlines() rFile.close() proxy = None for line in lines: if line.startswith('proxy='): proxy = line.split('proxy=')[1].strip() if proxy: print "Using Proxy server %s" %proxy else: print "No Proxy server is set" return proxy def register_new_odac(username=None, pwd=None): OV.Cursor("Please wait while AutoChem will be installed") mac_address = OV.GetMacAddress()[0] computer_name = os.getenv('COMPUTERNAME') url = "http://www.olex2.org/odac/register_new" olex2_tag = OV.GetTag() if not username: print("Please provide a username and password") OV.Cursor() return if not pwd: print("Please provide a username and password") OV.Cursor() return values = {'__ac_password':pwd, '__ac_name':username, 'olex2Tag':olex2_tag, 'computerName':computer_name, 'username':username, 'context':"None", 'macAddress':mac_address, } try: f = HttpTools.make_url_call(url, values) except Exception, err: print "You may have exceeded the number of AutoChem installs." print "Please contact contact xrdapplications@agilent.com for further information." return f = f.read() if not f: print "Please provide a valid username and password, and make sure your computer is online." print "You may also have used up the number of allowable installs." return elif not f.endswith(".exe"): print "Please provide valid username and password. If this problem persitsts, please" print "contact xrdapplications@agilent.com for further information." return p = "%s/Olex2u/OD/%s" %(os.environ['ALLUSERSPROFILE'], olex2_tag) p = os.path.abspath(p) if not os.path.exists(p): os.makedirs(p) else: try: import shutil shutil.rmtree(p) os.makedirs(p) except Exception, err: print "The installer could not delete this folder: %s" %p print "Please remove all files in this folder manually and run the installer again." olex.m('exec -o explorer "%s"' %p) return cont = GetHttpFile(f, force=True, fullURL = True) if cont: name = "AutoChem Installer.exe" wFile = open("%s/%s" %(p, name),'wb') wFile.write(cont) wFile.close() ins = "%s/AutoChem Installer.exe" %p cmd = r"%s /S" %ins # print cmd Popen(cmd, shell=False, stdout=PIPE).stdout for i in xrange(10): try: os.remove(ins) break except: time.sleep(5) print "AutoChem is now installed on your computer." print "Please restart Olex2 now." OV.Cursor() OV.registerFunction(register_new_odac) def updateACF(force=False): remote_version = OV.GetParam('odac.version.remote') local_version = None version_p = r"%s/odac_version.txt" %OV.BaseDir() if os.path.exists(version_p): rFile = open(version_p) local_version = rFile.read() rFile.close() if force: print "Update is forced" elif remote_version == local_version: print "No update required" return print "Now Updating..." mac_address = OV.GetMacAddress() username, computer_name = OV.GetUserComputerName() keyname = getKey() olex2_tag = OV.GetTag() username = "updater" password = "update456R" institution = keyname.split("-")[0] type_of_key = keyname.split("-")[-1] proxy = get_proxy_from_usettings() proxies = {'http': proxy} for mac_address in OV.GetMacAddress(): url = "http://www.olex2.org/odac/update" values = {'__ac_password':password, '__ac_name':username, 'context':"None", 'institution':institution, 'olex2Tag':olex2_tag, 'typeOfKey':type_of_key, 'computerName':computer_name, 'macAddress':mac_address, } try: response = HttpTools.make_url_call(url, values) f = response.read() except: print "\n++++++++++++++++++++++++++++++++++++++++++++++" print "+ Could not reach server at www.olex2.org" print "+ --------------------------------------------" print "+ Please make sure your computer is online" print "+ and that you can reach www.olex2.org" print "++++++++++++++++++++++++++++++++++++++++++++++\n" return if f: break new = True if not f: print "Updating has failed, previous files will be used" return if new: p = "%s/Olex2u/OD/%s" %(os.environ['ALLUSERSPROFILE'], olex2_tag) cont = GetHttpFile(f, force=True, fullURL = True) try: if cont: name = "AutoChem Updater.exe" wFile = open("%s/%s" %(p, name),'wb') wFile.write(cont) wFile.close() cmd = r"%s/AutoChem Updater.exe /S" %p print cmd Popen(cmd, shell=False, stdout=PIPE).stdout print "Updated" wFile = open(version_p,'w') wFile.write(remote_version) wFile.close() except Exception, err: print "Error with updating %s" %err print "Please check your file permissions" else: import pickle try: l = pickle.load(response) p = "%s/Olex2u/OD/%s" %(os.environ['ALLUSERSPROFILE'], olex2_tag) for f in l: f = f.replace(r'/var/distro/www/', 'olex-') cont = GetHttpFile(f, force=True) name = f.split(r'/')[-1] if cont: wFile = open("%s/%s" %(p, name),'wb') wFile.write(cont) wFile.close() print "Written %s/%s" %(p, name) wFile = open(r"%s/util/pyUtil/PluginLib/odac_update.txt" %OV.BaseDir(),'w') wFile.write("False") rFile.close() cmd = r"%s/AutoChem Installer.exe /S" %p Popen(cmd, shell=False, stdout=PIPE).stdout print "Updated" except Exception, err: print "Empty response: %s" %err print repr(response) OV.registerFunction(updateACF) def GetACF(): global _is_online no_update = False print "Starting ODAC..." if no_update: _is_online = False print "Will not update ODAC Files" check_for_crypto() cont = None tag = OV.GetTag() if not tag: print "You need to update Olex2 to at least version 1.0" return if not OV.IsPluginInstalled('Headless'): olex.m("installplugin Headless") updateACF() use_new = False if use_new: debug = OV.FindValue('odac_fb', False) debug = OV.GetParam('odac.debug.debug') OV.SetVar('odac_fb', debug) debug_deep1 = OV.GetParam('odac.debug.debug_deep1') debug_deep2 = OV.GetParam('odac.debug.debug_deep2') OV.SetVar("ac_verbose", OV.GetParam('odac.debug.verbose')) else: debug = OV.FindValue('odac_fb', False) debug = [False, True][0] debug_deep1 = [False, True][0] debug_deep2 = [False, True][0] OV.SetVar("ac_verbose", [False, True][0]) keyname = getKey() if not debug: p = "%s/Olex2u/OD/%s" %(os.environ['ALLUSERSPROFILE'], tag) OV.SetParam('odac.source_dir',p) if not os.path.exists(p): os.makedirs(p) name = "entry_ac" f = "/olex-distro-odac/%s/%s/%s.py" %(tag, keyname, name) if not os.path.exists("%s/entry_ac.py" %p): cont = GetHttpFile(f, force=True) if cont: wFile = open("%s/%s.py" %(p, name),'w') wFile.write(cont) wFile.close() if not cont: print "Olex2 was not able to go online and fetch a necessary file." print "Please make sure that your computer is online and try again." return else: try: if check_for_recent_update() and not no_update: cont = GetHttpFile(f) if cont: wFile = open("%s/%s.py" %(p, name),'w') wFile.write(cont) wFile.close() except Exception, err: print "Could not update ODAC file %s: %s" %(name, err) if not cont: wFile = open("%s/%s.py" %(p, name),'r') cont = wFile.read() wFile.close() if not cont: print "Olex2 can not access a necessary file." print "Please make sure that your computer is online and try again." return try: sys.modules[name] = types.ModuleType(name) exec cont in sys.modules[name].__dict__ odac_loaded = True except Exception, err: odac_loaded = False print "ODAC failed to load correctly" else: print "Debugging Mode is on. File System Based files will be used!" OV.SetVar("ac_debug", True) OV.SetVar("ac_verbose", True) if debug_deep1: OV.SetVar("ac_debug_deep1", True) if debug_deep2: OV.SetVar("ac_debug_deep2", True) p = r"%s/util/pyUtil/PluginLib/plugin-AutoChemSRC/%s" %(olx.BaseDir(), tag) sys.path.append(p) OV.SetParam('odac.source_dir',p) try: print "Debug: Import entry_ac" import entry_ac except Exception, err: print "Failed: %s" %err odac_loaded = False if OV.GetParam('odac.is_active'): print "AutoChem loaded OK" else: print "AutoChem could not be started" OV.registerFunction(GetACF) def runODAC(cmd): if OV.GetParam('odac.is_active'): cmd = cmd.rstrip(" -") cmd += " -s" OV.Cursor('Starting ODAC') res = olex.m(cmd) else: print "ODAC has failed to initialize or is not installed" OV.registerFunction(runODAC) def settings_tree(): l = [] handlers = [olx.phil_handler, olx.gui_phil_handler] for handler in handlers: raw_l = handler.get_root_scope_names() for item in raw_l: s = handler.get_scope_by_name(item) l.append("%s (%s)\n%s\n" %(item, s.short_caption, item)) for tem in s.objects: if tem.is_scope: l.append("\t%s (%s)\n%s.%s\n" %(tem.name, tem.short_caption, item, tem.name)) for em in tem.objects: if em.is_scope: l.append("\t\t%s (%s)\n%s.%s.%s\n" %(em.name, em.short_caption, item, tem.name, em.name)) for m in em.objects: if m.is_scope: l.append("\t\t\t%s (%s)\n%s.%s.%s.%s\n" %(m.name, m.short_caption, item, tem.name, em.name, m.name)) for m1 in m.objects: if m1.is_scope: l.append("\t\t\t\t%s (%s)\n%s.%s.%s.%s.%s\n" %(m1.name, m1.short_caption, item, tem.name, em.name, m.name, m1.name)) OV.write_to_olex('settings_tree.ind', ''.join(l)) OV.registerFunction(settings_tree) def GetOptionalHyphenString(txt): txt = txt.replace ("/", "/" + u"\u200B") txt = txt.replace ("\\", "\\" + u"\u200B") txt = txt.replace ("\\", "\\" + " ") return txt OV.registerFunction(GetOptionalHyphenString) def GetTwinLaw(html=False): olex_refinement_model = OV.GetRefinementModel(False) if olex_refinement_model.has_key('twin'): c = olex_refinement_model['twin']['matrix'] curr_law = [] for row in c: for el in row: curr_law.append(el) curr_law = tuple(curr_law) else: return txt = repr(curr_law) if html: txt = "
TWIN LAW %s | |||
Flack parameter | %s |