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
import threads as olxth
haveGUI = OV.HasGUI()
if haveGUI:
import olex_gui
def txt():
try:
import uuid
# This creates a custom unique filename determined by the the currently loaded in url
output_file_name = "%s.txt"%uuid.uuid3(uuid.NAMESPACE_OID, '%s'%str(OV.FileFull()))
# This is passed to the altered version of text creating the custom log instead of output.txt
# Change:
# $spy.MakeHoverButton(toolbar-text, 'Text') to
# $spy.MakeHoverButton(toolbar-text, 'spy.txt\()')
# in gui/blocks/snum-info.txt to make it default
olx.Text(output_file_name)
except ImportError, err:
print "Could not initialise spy.txt() function: %s" %err
OV.registerFunction(txt)
def expand(start, finish, increment=1):
"""
expand creates returns a list based on start, finish and increment
So C1, C6 would produce:
C1 C2 C3 C4 C5 C6
"""
import string
start_atom = str(start).translate(None, string.digits).lower()
start_number = int(str(start).translate(None, string.ascii_letters))
finish_atom = str(finish).translate(None, string.digits).lower()
finish_number = int(str(finish).translate(None, string.ascii_letters))
if (finish_atom != start_atom) or (start_number == finish_number):
print "The start and end element types must be the same and the numbers different"
return
return_string = []
for x in range(start_number, finish_number+1, int(increment)):
return_string.append('%s%d'%(start_atom,x))
return ' '.join(return_string)
OV.registerFunction(expand)
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.adp_u_eq':'fixed_u_eq_adp',
'olex2.restraint.adp_u_eq_similar':'adp_u_eq_similarity',
'olex2.restraint.adp_volume_similar':'adp_volume_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, pair_sym_table=None):
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', 'adp_u_eq_similarity', 'adp_volume_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', 'adp_u_eq_similarity',
'adp_volume_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 restraint_type in ('adp_similarity', 'isotropic_adp', 'rigid_bond'):
kwds['pair_sym_table'] = pair_sym_table
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])
elif restraint_type in ('angle', ):
for i in range(int(len(i_seqs)/3)):
yield restraint_type, dict(
weight=kwds['weight'],
angle_ideal=kwds['angle_ideal'],
i_seqs=kwds['i_seqs'][i*3:(i+1)*3],
sym_ops=kwds['sym_ops'][i*3:(i+1)*3])
elif restraint_type in ('dihedral', ):
for i in range(int(len(i_seqs)/4)):
yield restraint_type, dict(
weight=kwds['weight'],
angle_ideal=kwds['angle_ideal'],
i_seqs=kwds['i_seqs'][i*4:(i+1)*4],
sym_ops=kwds['sym_ops'][i*4:(i+1)*4])
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.number_non_hydrogen_atoms()
expected_peaks = expected_atoms - present_atoms
if expected_peaks < 5: expected_peaks = 5
return int(expected_peaks)
def getExpectedPeaks_and_AtomsPresent(self):
cell_volume = float(olx.xf.au.GetVolume())
expected_atoms = cell_volume/15
present_atoms = self.number_non_hydrogen_atoms()
expected_peaks = expected_atoms - present_atoms
if expected_peaks < 5: expected_peaks = 5
return int(expected_peaks), int(present_atoms)
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 = OV.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
global last_elements_html
current_formula = OlexRefinementModel().currentFormula()
#if current_formula == last_formula:
#return last_elements_html
from PilTools import ButtonMaker
icon_size = OV.GetParam('gui.skin.icon_size')
totalcount = 0
btn_dict = {}
f = olx.xf.GetFormula('list')
if not f:
return
f = f.split(',')
Z_prime = float(olx.xf.au.GetZprime())
Z = float(olx.xf.au.GetZ())
html = ""
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
c = ""
if present < max:
bgcolour = (250,250,250)
c = 'b'
elif present == max:
bgcolour = (210,255,210)
c = 'g'
else:
bgcolour = (255,210,210)
c = 'r'
if c:
name = "btn-element%s_%s" %(symbol, c)
else:
name = "btn-element%s" %(symbol)
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', name)
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
d['namelower'] = 'Table'
html += '''
''' %d
if current_formula != last_formula:
last_formula = current_formula
OV.write_to_olex('element_buttons.htm', html, 0)
im_name='IMG_BTN-ELEMENT%s' %symbol
OV.SetImage(im_name, name)
SetAtomicVolumeInSnumPhil(totalcount)
if haveGUI:
OV.registerFunction(MakeElementButtonsFromFormula)
def SetAtomicVolumeInSnumPhil(totalcount):
cell_volume = 0
Z = 1
Z_prime = float(olx.xf.au.GetZprime())
try:
cell_volume = float(olx.xf.au.GetCellVolume())
except:
pass
try:
Z = float(olx.xf.au.GetZ())
except:
pass
if cell_volume and totalcount:
atomic_volume = (cell_volume)/(totalcount * Z)
OV.SetParam('snum.solution.current_atomic_volume','%.1f' %atomic_volume)
else:
OV.SetParam('snum.solution.current_atomic_volume',None)
def CheckBoxValue(var, def_val='false'):
if '.' in var:
value = OV.GetParam(var)
else:
value = OV.FindValue(var,def_val) # XXX this should be gotten rid of
if value in (True, 'True', 'true'):
retVal = 'checked'
else:
retVal = ''
return str(retVal)
if haveGUI:
OV.registerFunction(CheckBoxValue)
def GetHklFileList():
reflection_file_extensions = ["hkl", "hkp", "raw", 'hklf5', 'hkc']
g = OV.ListFiles(OV.FilePath(), ';'.join(reflection_file_extensions))
g.sort()
try:
a = OV.HKLSrc().replace('\\', '/')
if a[0] == "'" or a[0] == '"':
a = a[1:-1]
except:
a = ""
if not os.path.isfile(a) and not g:
print "There is no reflection file or the reflection file is not accessible"
reflection_files = ""
for item in g:
reflection_files+="%s.%s<-%s;" %(OV.FileName(item), OV.FileExt(item), item)
return '"%s"' %reflection_files
if haveGUI:
OV.registerFunction(GetHklFileList)
def tbxs_(name):
retVal = ""
txt = r'''
<-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, R1) finally: return t elif format == 'float': try: t = float(R1) except: t = 0 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: if OV.HasGUI(): retVal=OV.GetParam('gui.red') else: retVal="#b40000" elif R1 >0.10: if OV.HasGUI(): retVal=OV.GetParam('gui.orange') else: retVal="#ff8f00" else: if OV.HasGUI(): retVal=OV.GetParam('gui.green') else: retVal = "#00b400" 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' try: OV.CopyVFSFile("cbtn-%s%s.png" %(btn,state),"cbtn-%s.png" %btn) except: olex.m('skin default_new') 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.program" %scope, prg_name) 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('%s.solution.method' %scope, 'Auto') #olx.html.SetValue('SET_autochem_solution_METHOD', 'Auto') return if prg_name != 'Unknown': 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.program" %scope, prg_name) 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 == '?' or prg == 'Unknown': return retval if prg == 'Auto': OV.SetParam("%s.solution.method" %scope,'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 == '?' or prg == 'Unknown': return retval if prg == 'Auto': OV.SetParam("%s.refinement.method" %scope,'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): kl = [] 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 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 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() else: print "Could not get %s" %f return ins = "%s/AutoChem Installer.exe" %p cmd = r"%s /S" %ins # print cmd olx.Shell(ins) # Popen(cmd, shell=True, 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 settings_tree(): l = [] added = [] 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) if item not in added: l.append("%s (%s)\n%s\n" %(item, s.short_caption, item)) added.append(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 |