Mercurial > repos > peterjc > tmhmm_and_signalp
comparison tools/protein_analysis/seq_analysis_utils.py @ 9:e52220a9ddad draft
Uploaded v0.1.2
Use the new <stdio> settings in the XML wrappers to catch errors.
Obeys SGE style XNSLOTS environment variable for thread count (otherwise default to 4).
author | peterjc |
---|---|
date | Fri, 25 Jan 2013 06:08:31 -0500 |
parents | 9b45a8743100 |
children | e6cc27d182a8 |
comparison
equal
deleted
inserted
replaced
8:976a5f2833cd | 9:e52220a9ddad |
---|---|
16 | 16 |
17 def stop_err(msg, error_level=1): | 17 def stop_err(msg, error_level=1): |
18 """Print error message to stdout and quit with given error level.""" | 18 """Print error message to stdout and quit with given error level.""" |
19 sys.stderr.write("%s\n" % msg) | 19 sys.stderr.write("%s\n" % msg) |
20 sys.exit(error_level) | 20 sys.exit(error_level) |
21 | |
22 try: | |
23 from multiprocessing import cpu_count | |
24 except ImportError: | |
25 #Must be under Python 2.5, this is copied from multiprocessing: | |
26 def cpu_count(): | |
27 """Returns the number of CPUs in the system.""" | |
28 if sys.platform == 'win32': | |
29 try: | |
30 num = int(os.environ['NUMBER_OF_PROCESSORS']) | |
31 except (ValueError, KeyError): | |
32 num = 0 | |
33 elif 'bsd' in sys.platform or sys.platform == 'darwin': | |
34 comm = '/sbin/sysctl -n hw.ncpu' | |
35 if sys.platform == 'darwin': | |
36 comm = '/usr' + comm | |
37 try: | |
38 with os.popen(comm) as p: | |
39 num = int(p.read()) | |
40 except ValueError: | |
41 num = 0 | |
42 else: | |
43 try: | |
44 num = os.sysconf('SC_NPROCESSORS_ONLN') | |
45 except (ValueError, OSError, AttributeError): | |
46 num = 0 | |
47 | |
48 if num >= 1: | |
49 return num | |
50 else: | |
51 raise NotImplementedError('cannot determine number of cpus') | |
52 | |
53 | |
54 def thread_count(command_line_arg, default=1): | |
55 try: | |
56 num = int(command_line_arg) | |
57 except: | |
58 num = default | |
59 if num < 1: | |
60 stop_err("Threads argument %r is not a positive integer" % command_line_arg) | |
61 #Cap this with the pysical limit of the machine, | |
62 try: | |
63 num = min(num, cpu_count()) | |
64 except NotImplementedError: | |
65 pass | |
66 #For debugging, | |
67 #hostname = os.environ.get("HOSTNAME", "this machine") | |
68 #sys.stderr.write("Using %i cores on %s\n" % (num, hostname)) | |
69 return num | |
70 | |
21 | 71 |
22 def fasta_iterator(filename, max_len=None, truncate=None): | 72 def fasta_iterator(filename, max_len=None, truncate=None): |
23 """Simple FASTA parser yielding tuples of (title, sequence) strings.""" | 73 """Simple FASTA parser yielding tuples of (title, sequence) strings.""" |
24 handle = open(filename) | 74 handle = open(filename) |
25 title, seq = "", "" | 75 title, seq = "", "" |
107 def run_jobs(jobs, threads, pause=10, verbose=False): | 157 def run_jobs(jobs, threads, pause=10, verbose=False): |
108 """Takes list of cmd strings, returns dict with error levels.""" | 158 """Takes list of cmd strings, returns dict with error levels.""" |
109 pending = jobs[:] | 159 pending = jobs[:] |
110 running = [] | 160 running = [] |
111 results = {} | 161 results = {} |
162 if threads == 1: | |
163 #Special case this for speed, don't need the waits | |
164 for cmd in jobs: | |
165 results[cmd] = subprocess.call(cmd, shell=True) | |
166 return results | |
112 while pending or running: | 167 while pending or running: |
113 #See if any have finished | 168 #See if any have finished |
114 for (cmd, process) in running: | 169 for (cmd, process) in running: |
115 return_code = process.poll() #non-blocking | 170 return_code = process.poll() #non-blocking |
116 if return_code is not None: | 171 if return_code is not None: |