-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
79 lines (65 loc) · 2.54 KB
/
Copy pathutils.py
File metadata and controls
79 lines (65 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/python
import os.path, time, os, sys
# flush the stdout buffer
def flush():
sys.stdout.flush()
return ""
# make a directory and return the absolute path to it
def mkdir(dir):
d = os.path.abspath(dir)
run("mkdir -p %s" % d)
return d
# make directory and cd into it
def mkdir_cd(dir):
mkdir(dir)
cd(dir)
def cd(dir):
os.chdir(dir)
# parse the pdb file name from file paths
def parse_pdbnames(fns):
pdbnames = []
for fn in fns:
pdbnames += [os.path.basename(fn).replace('.pdb', '').replace('.gz','')]
return pdbnames
def parse_pdbname(fn): return parse_pdbnames([fn])[0]
# convert an array to a pretty string
def arr2str2(a, precision=2, length=5, col_names=None, row_names=None):
fmt = "%" + str(length) + "s"
shape = a.shape
assert(len(shape) == 2)
#s = "\n".join([fmt_floats(a[row,:], digits=precision) for row in range(shape[0])]) + "\n"
str_arr = []
if col_names != None: str_arr += [" "*(length+1) + " ".join([fmt%str(col) for col in col_names])]
for i in range(shape[0]):
row_header = ""
if row_names != None: row_header = fmt % str(row_names[i]) + " "
str_arr += [row_header + fmt_floats(a[i,:], digits=precision, length=length)]
return "\n".join(str_arr) + "\n"
# convert an array to a pretty string
def arr1str1(a, precision=2, length=5, col_names=None, row_names=None):
fmt = "%" + str(length) + "s"
shape = a.shape
assert(len(shape) == 1)
#s = "\n".join([fmt_floats(a[row,:], digits=precision) for row in range(shape[0])]) + "\n"
str_arr = []
if col_names != None: str_arr += [" "*(length+1) + " ".join([fmt%str(col) for col in col_names])]
for i in range(shape[0]):
row_header = ""
if row_names != None: row_header = fmt % str(row_names[i]) + " "
str_arr += [row_header + fmt_floats( [a[i]], digits=precision, length=length)]
return "\n".join(str_arr) + "\n"
# run a shell cmd
def run(cmd):
import commands
status, output = commands.getstatusoutput(cmd)
if status != 0: raise Exception("run('%s') exited with status %d; Output:\n %s" % (cmd, status, output))
return output
# track elapsed time in seconds
class Timer:
def __init__(self): self._start = time.time()
def elapsed(self): return time.time() - self._start
def __str__(self): return "%0d:%02d" % (self.elapsed()/60, self.elapsed()%60)
# pretty print a list of floats
def fmt_floats(fs, digits=2, length="", sep=" "):
fmt = "%"+str(length)+"."+str(digits)+"f"
return sep.join([fmt%f for f in fs])