comparison tools/protein_analysis/seq_analysis_utils.py @ 7:5e62aefb2918 draft

Uploaded v0.1.2 to Test Tool Shed
author peterjc
date Tue, 26 Mar 2013 14:24:56 -0400
parents 39a6e46cdda3
children 41a42022f815
comparison
equal deleted inserted replaced
6:39a6e46cdda3 7:5e62aefb2918
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 = "", ""
98 pass 148 pass
99 for f in files: 149 for f in files:
100 if os.path.isfile(f): 150 if os.path.isfile(f):
101 os.remove(f) 151 os.remove(f)
102 raise err 152 raise err
153 for f in files:
154 assert os.path.isfile(f), "Missing split file %r (!??)" % f
103 return files 155 return files
104 156
105 def run_jobs(jobs, threads, pause=10, verbose=False): 157 def run_jobs(jobs, threads, pause=10, verbose=False):
106 """Takes list of cmd strings, returns dict with error levels.""" 158 """Takes list of cmd strings, returns dict with error levels."""
107 pending = jobs[:] 159 pending = jobs[:]
108 running = [] 160 running = []
109 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
110 while pending or running: 167 while pending or running:
111 #See if any have finished 168 #See if any have finished
112 for (cmd, process) in running: 169 for (cmd, process) in running:
113 return_code = process.poll() #non-blocking 170 return_code = process.poll() #non-blocking
114 if return_code is not None: 171 if return_code is not None: