1 """@namespace IMP.isd.utils
2 Miscellaneous utilities.
31 from Queue
import Queue
32 from threading
import Thread
61 return sum(x) / float(len(x))
64 def atexit_register(*args):
66 atexit.register(*args)
69 def atexit_unregister(func):
71 exit_funcs = [x[0]
for x
in atexit._exithandlers]
74 i = exit_funcs.index(func)
78 atexit._exithandlers.pop(i)
81 class WatchDog(Thread):
83 def __init__(self, timeout, debug=False, logfile=None):
90 self.timeout = timeout * 60.
92 self._last_ping =
None
95 if logfile
is not None:
96 logfile = os.path.expanduser(logfile)
98 self.logfile = logfile
106 "set the _last_ping variable of the WatchDog instance"
109 print 'Watchdog: set(%s) called.' % str(x)
114 """run the Watchdog thread, which sits in a loop sleeping for timeout/4. at
115 each iteration, and if abs(time() - _last_ping) > timeout, exits.
118 while not self._stop:
120 if self._last_ping
is not None:
121 delta = abs(self._last_ping - time.time())
130 val =
'%.0f s' % delta
132 print 'Watchdog: last life sign %s ago; timeout is %d min(s).' % \
133 (val, self.timeout / 60.)
135 if self._last_ping
is not None and delta > self.timeout:
137 s =
'No life sign for > %d minute(s)' % (self.timeout / 60.)
139 print s +
', exiting...'
141 if self.logfile
is not None:
143 if os.path.exists(self.logfile):
149 f = open(self.logfile, mode)
151 s +
'; host %s, %s\n' %
152 (socket.gethostname(), time.ctime()))
161 print 'Watchdog: keeping Python interpreter alive.'
164 time.sleep(self.timeout / 4.)
169 symbols = (
'-',
'/',
'|',
'\\')
174 def update(self, s=''):
178 sys.stdout.write(
'\r%s%s' % (s, self.symbols[self.state]))
181 self.state = (self.state + 1) % len(self.symbols)
186 """implements a FIFO pipe that merges lists (see self.put)"""
188 def __init__(self, length=-1):
194 """if x is subscriptable, insert its contents at the beginning of the pipe.
195 Else insert the element itself.
196 If the pipe is full, drop the oldest element.
201 self.pipe = list(x) + self.pipe
204 self.pipe.insert(0, x)
206 if self.length > 0
and len(self.pipe) > self.length:
207 self.pipe = self.pipe[:-1]
210 """ x must be a list and will be appended to the end of the pipe, dropping
211 rightmost elements if necessary
214 self.pipe = (list(x) + self.pipe)[:self.length]
217 """returns the oldest element, without popping it out of the pipe.
218 Popping occurs in the put() method
222 def __getitem__(self, index):
223 return self.pipe.__getitem__(index)
226 return len(self.pipe)
229 return str(self.pipe)
232 return len(self.pipe) == self.length
237 class SortedQueue(Queue):
241 from numpy.oldnumeric
import array
242 from Isd.misc.mathutils
import average
244 self.queue.sort(
lambda a, b: cmp(average(a.time), average(b.time)))
246 self.times = array([average(x.time)
for x
in self.queue])
248 def _put(self, item):
250 Queue._put(self, item)
255 from numpy.oldnumeric
import power
256 from Isd.misc.mathutils
import draw_dirichlet, rescale_uniform
260 p = 1. - rescale_uniform(self.times)
263 index = draw_dirichlet(p)
265 val = self.queue[index]
267 self.queue = self.queue[:index] + self.queue[index + 1:]
275 def load_pdb(filename):
279 from Scientific.IO.PDB
import Structure
281 return Structure(os.path.expanduser(filename))
284 def copyfiles(src_path, dest_path, pattern=None, verbose=False):
286 from glob
import glob
287 from shutil
import copyfile
293 file_list = glob(os.path.join(src_path, pattern))
296 copyfile(f, os.path.join(dest_path, os.path.basename(f)))
305 f = open(filename,
'w')
308 except IOError
as error:
310 if os.path.isdir(filename):
319 """read sequence of ONE chain, 1-letter or 3-letter, returns dict of
320 no:3-letter code. Fails on unknown amino acids.
323 filename = os.path.abspath(filename)
326 except IOError
as msg:
327 raise IOError(
'Could not open sequence file "%s".' % filename)
328 seq = f.read().upper()
330 if seq.startswith(
'>'):
331 print "Detected FASTA 1-letter sequence"
334 seq =
''.join(seq[pos + 1:].split())
335 names = [code[i]
for i
in seq]
336 numbers = range(first_residue_number, first_residue_number + len(seq))
337 return dict(zip(numbers, names))
341 if not x
in code.values():
342 print 'Warning: unknown 3-letter code: %s' % x
343 numbers = range(first_residue_number, first_residue_number + len(l))
344 return dict(zip(numbers, l))
350 "checks whether residue codes a and b are the same, doing necessary conversions"
355 print 'Warning: unknown 1-letter code: %s' % a
360 print 'Warning: unknown 1-letter code: %s' % b
364 print 'Unknown residue code %s' % a
367 print 'Unknown residue code %s' % b
370 print 'Residues %s and %s are not the same' % (a, b)
376 def my_glob(x, do_touch=False):
378 from glob
import glob
384 path, name = os.path.split(x)
392 def Dump(this, filename, gzip=0, mode='w', bin=1):
394 Dump(this, filename, gzip = 0)
395 Supports also '~' or '~user'.
401 filename = os.path.expanduser(filename)
403 if not mode
in [
'w',
'a']:
404 raise "mode has to be 'w' (write) or 'a' (append)"
408 f = gzip.GzipFile(filename, mode)
410 f = open(filename, mode)
412 cPickle.dump(this, f, bin)
417 def Load(filename, gzip=0, force=0):
419 Load(filename, gzip=0, force=0)
421 force: returns all objects that could be unpickled. Useful
422 when unpickling of sequential objects fails at some point.
427 filename = os.path.expanduser(filename)
432 f = gzip.GzipFile(filename)
446 object = cPickle.load(f)
460 print 'Could not load chunk %d. Stopped.' % n
465 object = cPickle.load(f)
472 def get_pdb(pdb_entry, dest='.', verbose_level=0):
475 from tempfile
import mktemp
478 url =
'ftp.ebi.ac.uk'
479 path =
'pub/databases/rcsb/pdb-remediated/data/structures/all/pdb'
480 filename_template =
'pdb%s.ent.gz'
482 dest = os.path.expanduser(dest)
484 ftp = ftplib.FTP(url)
486 ftp.set_debuglevel(verbose_level)
490 filename = os.path.join(dest,
'%s.pdb.gz' % pdb_entry)
492 f = open(filename,
'wb')
495 ftp.retrbinary(
'RETR %s' % filename_template % pdb_entry.lower(),
502 except ftplib.error_perm:
503 raise IOError(
'File %s not found on server' % filename)
505 os.system(
'gunzip -f %s' % filename)
508 def compile_index_list(chain, atom_names, residue_index_list=None):
510 if residue_index_list
is None:
511 residue_index_list = range(len(chain))
521 for res_index
in residue_index_list:
523 if atom_names
is None:
524 names = sorted(chain[res_index].keys())
528 if n
in chain[res_index]:
529 index = chain[res_index][n].index
530 index_list.append(index)
534 return index_list, index_map
537 def get_coordinates(universe, E, indices=None, atom_names=(
'CA',),
538 residue_index_list=
None, atom_index_list=
None):
540 from numpy.oldnumeric
import array, take
543 indices = range(len(E))
545 chain = universe.get_polymer()
547 if atom_index_list
is None:
548 atom_index_list, index_map = compile_index_list(chain, atom_names,
555 chain.set_torsions(E.torsion_angles[i], 1)
557 X = array(take(universe.X, atom_index_list))
566 maps angles into interval [-pi,pi]
569 from numpy.oldnumeric
import fmod, greater, logical_not
572 from numpy.oldnumeric
import pi
as period
574 mask = greater(angles, 0.)
576 return mask * (fmod(angles + period, 2 * period) - period) + \
577 logical_not(mask) * (fmod(angles - period, 2 * period) + period)
580 def remove_from_dict(d, items):
587 def myrange(a, b, n):
589 from numpy.oldnumeric
import arange
591 step = (b - a) / (n - 1)
593 x = arange(a, b + step, step)
598 def indent(lines, prefix):
600 tag =
' ' * len(str(prefix))
602 lines[0] = prefix + lines[0]
603 lines = [lines[0]] + map(
lambda s, t=tag: t + s, lines[1:])
605 return '\n'.join(lines)
608 def make_block(s, length=80, tol=10):
609 blocks = s.split(
'\n')
612 l += _make_block(block, length, tol)
617 def _make_block(s, length, tol):
620 l = [(w,
' ')
for w
in l]
625 g = [w +
'/' for w
in g]
626 g[-1] = g[-1][:-1] +
' '
633 for i
in range(len(words)):
636 if len(line + word) <= length:
640 if length - len(line) > tol:
641 m = length - len(line)
645 if len(line) > 1
and line[0] ==
' ' and \
653 if len(line) > 1
and line[0] ==
' ' and \
662 def _save_dump(x, filename, err_msg=None, delay=10, show_io_err=True,
663 gzip=
False, bin=
True):
666 Dump(x, filename, gzip=gzip, bin=bin)
668 except IOError
as msg:
673 print 'IOError: %s' % str(msg)
677 print '%s. %s' % (str(msg), err_msg)
685 time.sleep(60. * delay)
688 Dump(x, filename, gzip=gzip, bin=bin)
695 def save_dump(x, filename, err_msg=None, delay=10, show_io_err=True,
696 gzip=
False, mode=
'w', bin=
True):
701 path, _filename = os.path.split(filename)
703 temp_path, temp_filename = os.path.split(tempfile.mktemp())
704 temp_filename = os.path.join(path, temp_filename)
706 _save_dump(x, temp_filename, err_msg, delay, show_io_err,
712 os.rename(temp_filename, filename)
715 os.unlink(temp_filename)
716 Dump(x, filename, mode=
'a', gzip=gzip, bin=bin)
719 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