1 """@namespace IMP.isd.utils
2 Miscellaneous utilities.
5 from __future__
import print_function
33 from queue
import Queue
35 from Queue
import Queue
36 from threading
import Thread
65 return sum(x) / float(len(x))
68 def atexit_register(*args):
70 atexit.register(*args)
73 def atexit_unregister(func):
75 exit_funcs = [x[0]
for x
in atexit._exithandlers]
78 i = exit_funcs.index(func)
82 atexit._exithandlers.pop(i)
85 class WatchDog(Thread):
87 def __init__(self, timeout, debug=False, logfile=None):
94 self.timeout = timeout * 60.
96 self._last_ping =
None
99 if logfile
is not None:
100 logfile = os.path.expanduser(logfile)
102 self.logfile = logfile
110 "set the _last_ping variable of the WatchDog instance"
113 print(
'Watchdog: set(%s) called.' % str(x))
118 """run the Watchdog thread, which sits in a loop sleeping for
119 timeout/4. at each iteration, and
120 if abs(time() - _last_ping) > timeout, exits.
123 while not self._stop:
125 if self._last_ping
is not None:
126 delta = abs(self._last_ping - time.time())
135 val =
'%.0f s' % delta
137 print(
'Watchdog: last life sign %s ago; timeout is %d min(s).'
138 % (val, self.timeout / 60.))
140 if self._last_ping
is not None and delta > self.timeout:
142 s =
'No life sign for > %d minute(s)' % (self.timeout / 60.)
144 print(s +
', exiting...')
146 if self.logfile
is not None:
148 if os.path.exists(self.logfile):
154 f = open(self.logfile, mode)
156 s +
'; host %s, %s\n' %
157 (socket.gethostname(), time.ctime()))
166 print(
'Watchdog: keeping Python interpreter alive.')
169 time.sleep(self.timeout / 4.)
174 symbols = (
'-',
'/',
'|',
'\')
179 def update(self, s=''):
180 sys.stdout.write(
'\r%s%s' % (s, self.symbols[self.state]))
183 self.state = (self.state + 1) % len(self.symbols)
188 """implements a FIFO pipe that merges lists (see self.put)"""
190 def __init__(self, length=-1):
196 """If x is subscriptable, insert its contents at the beginning of
197 the pipe. Else insert the element itself.
198 If the pipe is full, drop the oldest element.
203 self.pipe = list(x) + self.pipe
206 self.pipe.insert(0, x)
208 if self.length > 0
and len(self.pipe) > self.length:
209 self.pipe = self.pipe[:-1]
212 """x must be a list and will be appended to the end of the pipe,
213 dropping rightmost elements if necessary
216 self.pipe = (list(x) + self.pipe)[:self.length]
219 """returns the oldest element, without popping it out of the pipe.
220 Popping occurs in the put() method
224 def __getitem__(self, index):
225 return self.pipe.__getitem__(index)
228 return len(self.pipe)
231 return str(self.pipe)
234 return len(self.pipe) == self.length
239 class SortedQueue(Queue):
243 from numpy.oldnumeric
import array
244 from Isd.misc.mathutils
import average
246 self.queue.sort(
lambda a, b: cmp(average(a.time), average(b.time)))
248 self.times = array([average(x.time)
for x
in self.queue])
250 def _put(self, item):
252 Queue._put(self, item)
257 from numpy.oldnumeric
import power
258 from Isd.misc.mathutils
import draw_dirichlet, rescale_uniform
262 p = 1. - rescale_uniform(self.times)
265 index = draw_dirichlet(p)
267 val = self.queue[index]
269 self.queue = self.queue[:index] + self.queue[index + 1:]
277 def load_pdb(filename):
281 from Scientific.IO.PDB
import Structure
283 return Structure(os.path.expanduser(filename))
286 def copyfiles(src_path, dest_path, pattern=None, verbose=False):
288 from glob
import glob
289 from shutil
import copyfile
295 file_list = glob(os.path.join(src_path, pattern))
298 copyfile(f, os.path.join(dest_path, os.path.basename(f)))
307 f = open(filename,
'w')
310 except IOError
as error:
312 if os.path.isdir(filename):
321 """read sequence of ONE chain, 1-letter or 3-letter, returns dict of
322 no:3-letter code. Fails on unknown amino acids.
325 filename = os.path.abspath(filename)
329 raise IOError(
'Could not open sequence file "%s".' % filename)
330 seq = f.read().upper()
332 if seq.startswith(
'>'):
333 print(
"Detected FASTA 1-letter sequence")
336 seq =
''.join(seq[pos + 1:].split())
337 names = [code[i]
for i
in seq]
338 numbers = list(range(first_residue_number,
339 first_residue_number + len(seq)))
340 return dict(list(zip(numbers, names)))
344 if x
not in code.values():
345 print(
'Warning: unknown 3-letter code: %s' % x)
346 numbers = list(range(first_residue_number,
347 first_residue_number + len(spl)))
348 return dict(list(zip(numbers, spl)))
354 """checks whether residue codes a and b are the same, doing necessary
360 print(
'Warning: unknown 1-letter code: %s' % a)
365 print(
'Warning: unknown 1-letter code: %s' % b)
369 print(
'Unknown residue code %s' % a)
372 print(
'Unknown residue code %s' % b)
375 print(
'Residues %s and %s are not the same' % (a, b))
381 def my_glob(x, do_touch=False):
383 from glob
import glob
389 path, name = os.path.split(x)
397 def Dump(this, filename, gzip=0, mode='w', bin=1):
399 Dump(this, filename, gzip = 0)
400 Supports also '~' or '~user'.
405 import cPickle
as pickle
409 filename = os.path.expanduser(filename)
411 if mode
not in [
'w',
'a']:
412 raise ValueError(
"mode has to be 'w' (write) or 'a' (append)")
416 f = gzip.GzipFile(filename, mode)
418 f = open(filename, mode)
420 pickle.dump(this, f, bin)
425 def Load(filename, gzip=0, force=0):
427 Load(filename, gzip=0, force=0)
429 force: returns all objects that could be unpickled. Useful
430 when unpickling of sequential objects fails at some point.
435 filename = os.path.expanduser(filename)
440 f = gzip.GzipFile(filename)
444 f = open(filename,
'rb')
454 object = pickle.load(f)
468 print(
'Could not load chunk %d. Stopped.' % n)
473 object = pickle.load(f)
480 def get_pdb(pdb_entry, dest='.', verbose_level=0):
485 url =
'ftp.ebi.ac.uk'
486 path =
'pub/databases/rcsb/pdb-remediated/data/structures/all/pdb'
487 filename_template =
'pdb%s.ent.gz'
489 dest = os.path.expanduser(dest)
491 ftp = ftplib.FTP(url)
493 ftp.set_debuglevel(verbose_level)
497 filename = os.path.join(dest,
'%s.pdb.gz' % pdb_entry)
499 f = open(filename,
'wb')
502 ftp.retrbinary(
'RETR %s' % filename_template % pdb_entry.lower(),
509 except ftplib.error_perm:
510 raise IOError(
'File %s not found on server' % filename)
512 os.system(
'gunzip -f %s' % filename)
515 def compile_index_list(chain, atom_names, residue_index_list=None):
517 if residue_index_list
is None:
518 residue_index_list = list(range(len(chain)))
528 for res_index
in residue_index_list:
530 if atom_names
is None:
531 names = sorted(chain[res_index].keys())
535 if n
in chain[res_index]:
536 index = chain[res_index][n].index
537 index_list.append(index)
541 return index_list, index_map
544 def get_coordinates(universe, E, indices=None, atom_names=(
'CA',),
545 residue_index_list=
None, atom_index_list=
None):
547 from numpy.oldnumeric
import array, take
550 indices = list(range(len(E)))
552 chain = universe.get_polymer()
554 if atom_index_list
is None:
555 atom_index_list, index_map = compile_index_list(chain, atom_names,
562 chain.set_torsions(E.torsion_angles[i], 1)
564 X = array(take(universe.X, atom_index_list))
573 maps angles into interval [-pi,pi]
576 from numpy.oldnumeric
import fmod, greater, logical_not
579 from numpy.oldnumeric
import pi
as period
581 mask = greater(angles, 0.)
583 return mask * (fmod(angles + period, 2 * period) - period) + \
584 logical_not(mask) * (fmod(angles - period, 2 * period) + period)
587 def remove_from_dict(d, items):
594 def myrange(a, b, n):
596 from numpy.oldnumeric
import arange
598 step = (b - a) / (n - 1)
600 x = arange(a, b + step, step)
605 def indent(lines, prefix):
607 tag =
' ' * len(str(prefix))
609 lines[0] = prefix + lines[0]
610 lines = [lines[0]] + list(map(
lambda s, t=tag: t + s, lines[1:]))
612 return '\n'.join(lines)
615 def make_block(s, length=80, tol=10):
616 blocks = s.split(
'\n')
619 spl += _make_block(block, length, tol)
624 def _make_block(s, length, tol):
627 spl = [(w,
' ')
for w
in spl]
632 g = [w +
'/' for w
in g]
633 g[-1] = g[-1][:-1] +
' '
640 for i
in range(len(words)):
643 if len(line + word) <= length:
647 if length - len(line) > tol:
648 m = length - len(line)
652 if len(line) > 1
and line[0] ==
' ' and \
660 if len(line) > 1
and line[0] ==
' ' and \
669 def _save_dump(x, filename, err_msg=None, delay=10, show_io_err=True,
670 gzip=
False, bin=
True):
673 Dump(x, filename, gzip=gzip, bin=bin)
675 except IOError
as msg:
680 print(
'IOError: %s' % str(msg))
684 print(
'%s. %s' % (str(msg), err_msg))
692 time.sleep(60. * delay)
695 Dump(x, filename, gzip=gzip, bin=bin)
702 def save_dump(x, filename, err_msg=None, delay=10, show_io_err=True,
703 gzip=
False, mode=
'w', bin=
True):
708 path, _filename = os.path.split(filename)
710 temp_path, temp_filename = os.path.split(tempfile.mktemp())
711 temp_filename = os.path.join(path, temp_filename)
713 _save_dump(x, temp_filename, err_msg, delay, show_io_err,
719 os.rename(temp_filename, filename)
722 os.unlink(temp_filename)
723 Dump(x, filename, mode=
'a', gzip=gzip, bin=bin)
726 raise Exception(
'Mode "%s" invalid.' % mode)
def map_angles
maps angles into interval [-pi,pi]
def put
If x is subscriptable, insert its contents at the beginning of the pipe.
implements a FIFO pipe that merges lists (see self.put)
def get
returns the oldest element, without popping it out of the pipe.
def read_sequence_file
read sequence of ONE chain, 1-letter or 3-letter, returns dict of no:3-letter code.
def append
x must be a list and will be appended to the end of the pipe, dropping rightmost elements if necessar...
The general base class for IMP exceptions.
def check_residue
checks whether residue codes a and b are the same, doing necessary conversions