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 timeout/4. at
119 each iteration, and if abs(time() - _last_ping) > timeout, exits.
122 while not self._stop:
124 if self._last_ping
is not None:
125 delta = abs(self._last_ping - time.time())
134 val =
'%.0f s' % delta
136 print(
'Watchdog: last life sign %s ago; timeout is %d min(s).' % \
137 (val, self.timeout / 60.))
139 if self._last_ping
is not None and delta > self.timeout:
141 s =
'No life sign for > %d minute(s)' % (self.timeout / 60.)
143 print(s +
', exiting...')
145 if self.logfile
is not None:
147 if os.path.exists(self.logfile):
153 f = open(self.logfile, mode)
155 s +
'; host %s, %s\n' %
156 (socket.gethostname(), time.ctime()))
165 print(
'Watchdog: keeping Python interpreter alive.')
168 time.sleep(self.timeout / 4.)
173 symbols = (
'-',
'/',
'|',
'\\')
178 def update(self, s=''):
182 sys.stdout.write(
'\r%s%s' % (s, self.symbols[self.state]))
185 self.state = (self.state + 1) % len(self.symbols)
190 """implements a FIFO pipe that merges lists (see self.put)"""
192 def __init__(self, length=-1):
198 """if x is subscriptable, insert its contents at the beginning of the pipe.
199 Else insert the element itself.
200 If the pipe is full, drop the oldest element.
205 self.pipe = list(x) + self.pipe
208 self.pipe.insert(0, x)
210 if self.length > 0
and len(self.pipe) > self.length:
211 self.pipe = self.pipe[:-1]
214 """ x must be a list and will be appended to the end of the pipe, dropping
215 rightmost elements if necessary
218 self.pipe = (list(x) + self.pipe)[:self.length]
221 """returns the oldest element, without popping it out of the pipe.
222 Popping occurs in the put() method
226 def __getitem__(self, index):
227 return self.pipe.__getitem__(index)
230 return len(self.pipe)
233 return str(self.pipe)
236 return len(self.pipe) == self.length
241 class SortedQueue(Queue):
245 from numpy.oldnumeric
import array
246 from Isd.misc.mathutils
import average
248 self.queue.sort(
lambda a, b: cmp(average(a.time), average(b.time)))
250 self.times = array([average(x.time)
for x
in self.queue])
252 def _put(self, item):
254 Queue._put(self, item)
259 from numpy.oldnumeric
import power
260 from Isd.misc.mathutils
import draw_dirichlet, rescale_uniform
264 p = 1. - rescale_uniform(self.times)
267 index = draw_dirichlet(p)
269 val = self.queue[index]
271 self.queue = self.queue[:index] + self.queue[index + 1:]
279 def load_pdb(filename):
283 from Scientific.IO.PDB
import Structure
285 return Structure(os.path.expanduser(filename))
288 def copyfiles(src_path, dest_path, pattern=None, verbose=False):
290 from glob
import glob
291 from shutil
import copyfile
297 file_list = glob(os.path.join(src_path, pattern))
300 copyfile(f, os.path.join(dest_path, os.path.basename(f)))
309 f = open(filename,
'w')
312 except IOError
as error:
314 if os.path.isdir(filename):
323 """read sequence of ONE chain, 1-letter or 3-letter, returns dict of
324 no:3-letter code. Fails on unknown amino acids.
327 filename = os.path.abspath(filename)
330 except IOError
as msg:
331 raise IOError(
'Could not open sequence file "%s".' % filename)
332 seq = f.read().upper()
334 if seq.startswith(
'>'):
335 print(
"Detected FASTA 1-letter sequence")
338 seq =
''.join(seq[pos + 1:].split())
339 names = [code[i]
for i
in seq]
340 numbers = list(range(first_residue_number, first_residue_number + len(seq)))
341 return dict(list(zip(numbers, names)))
345 if not x
in list(code.values()):
346 print(
'Warning: unknown 3-letter code: %s' % x)
347 numbers = list(range(first_residue_number, first_residue_number + len(l)))
348 return dict(list(zip(numbers, l)))
354 "checks whether residue codes a and b are the same, doing necessary conversions"
359 print(
'Warning: unknown 1-letter code: %s' % a)
364 print(
'Warning: unknown 1-letter code: %s' % b)
368 print(
'Unknown residue code %s' % a)
371 print(
'Unknown residue code %s' % b)
374 print(
'Residues %s and %s are not the same' % (a, b))
380 def my_glob(x, do_touch=False):
382 from glob
import glob
388 path, name = os.path.split(x)
396 def Dump(this, filename, gzip=0, mode='w', bin=1):
398 Dump(this, filename, gzip = 0)
399 Supports also '~' or '~user'.
404 import cPickle
as pickle
408 filename = os.path.expanduser(filename)
410 if not mode
in [
'w',
'a']:
411 raise ValueError(
"mode has to be 'w' (write) or 'a' (append)")
415 f = gzip.GzipFile(filename, mode)
417 f = open(filename, mode)
419 pickle.dump(this, f, bin)
424 def Load(filename, gzip=0, force=0):
426 Load(filename, gzip=0, force=0)
428 force: returns all objects that could be unpickled. Useful
429 when unpickling of sequential objects fails at some point.
434 filename = os.path.expanduser(filename)
439 f = gzip.GzipFile(filename)
443 f = open(filename,
'rb')
453 object = pickle.load(f)
467 print(
'Could not load chunk %d. Stopped.' % n)
472 object = pickle.load(f)
479 def get_pdb(pdb_entry, dest='.', verbose_level=0):
482 from tempfile
import mktemp
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 l += _make_block(block, length, tol)
624 def _make_block(s, length, tol):
627 l = [(w,
' ')
for w
in l]
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...
def check_residue
checks whether residue codes a and b are the same, doing necessary conversions