-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathdistruct.py
167 lines (149 loc) · 4.47 KB
/
distruct.py
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
from __future__ import print_function
from shutil import copyfile
from syscall import SysCall
import os
import sys
class Distruct():
'Class for preparing distruct output from the output produced by clumpak'
def __init__(self,wd,otl,cb,ptcb):
self.wd = wd
self.nd = os.path.join(self.wd, "best_results")
self.oldtoplabels = otl
self.bottomlabels = "bottomlabels"
self.toplabels = os.path.join(wd,self.oldtoplabels)
#Check if file exists
self.fileExists(self.toplabels)
self.colorbrew = cb
self.pathtocb = ptcb
self.cbdict=self.makecbsuffixdict()
def copyFiles(self):
nf = os.path.join(self.nd, self.oldtoplabels)
copyfile(self.toplabels,nf)
def makedir(self,wd,d):
if not os.path.exists(nd):
os.makedirs(nd)
def writeDrawparams(self,pfile, popq, indivq, k, outfile, pops, numind, width):
drawp = os.path.join(self.nd, pfile)
#popqdir = os.path.join(self.nd,popq)
#indivqdir = os.path.join(self.nd,indivq)
#topdir = os.path.join(self.nd,self.oldtoplabels)
#btmdir = os.path.join(self.nd,self.oldbottomlabels)
fh = open(drawp, 'w')
fh.write("#define INFILE_POPQ ")
fh.write(popq)
fh.write("\n")
fh.write("#define INFILE_INDIVQ ")
fh.write(indivq)
fh.write("\n")
fh.write("#define INFILE_LABEL_BELOW ")
fh.write(self.bottomlabels)
fh.write("\n")
fh.write("#define INFILE_LABEL_ATOP ")
fh.write(self.oldtoplabels)
fh.write("\n")
#fh.write("#define INFILE_CLUST_PERM /home/mussmann/local/src/distruct1.1/ColorBrewer/BrBG_")
fh.write("#define INFILE_CLUST_PERM ")
fh.write(self.pathtocb)
fh.write(self.colorbrew)
fh.write("_")
fh.write(k)
fh.write("_")
fh.write(self.cbdict[self.colorbrew])
fh.write("\n")
fh.write("#define OUTFILE ")
fh.write(outfile)
fh.write("\n")
fh.write("#define K ")
fh.write(k)
fh.write("\n")
fh.write("#define NUMPOPS ")
fh.write(str(pops))
fh.write("\n")
fh.write("#define NUMINDS ")
fh.write(str(numind))
fh.write("\n")
fh.write("#define PRINT_INDIVS 1\n")
fh.write("#define PRINT_LABEL_ATOP 1\n")
fh.write("#define PRINT_LABEL_BELOW 0\n")
fh.write("#define PRINT_SEP 1\n")
fh.write("#define FONTHEIGHT 6\n")
fh.write("#define DIST_ABOVE -160\n")
fh.write("#define DIST_BELOW -50\n")
fh.write("#define BOXHEIGHT 150\n")
fh.write("#define INDIVWIDTH ")
fh.write(width)
fh.write("\n")
fh.write("#define ORIENTATION 1\n")
fh.write("#define XORIGIN 200\n")
fh.write("#define YORIGIN 10\n")
fh.write("#define XSCALE 1\n")
fh.write("#define YSCALE 1\n")
fh.write("#define ANGLE_LABEL_ATOP 270\n")
fh.write("#define ANGLE_LABEL_BELOW 270\n")
fh.write("#define LINEWIDTH_RIM 3\n")
fh.write("#define LINEWIDTH_SEP 1\n")
fh.write("#define LINEWIDTH_IND 4\n")
fh.write("#define GRAYSCALE 0\n")
fh.write("#define ECHO_DATA 1\n")
fh.write("#define REPRINT_DATA 1\n")
fh.write("#define PRINT_INFILE_NAME 0\n")
fh.write("#define PRINT_COLOR_BREWER 1\n")
fh.close()
def runDistruct(self):
print("Now running distruct for all drawparams files...")
contents = os.listdir(self.nd)
os.chdir(self.nd)
for f in contents:
if f.startswith("drawparams"):
distructCommand = "distruct -d " + str(f) + "; echo"
call = SysCall(distructCommand)
call.run_program()
print("WARNING: Check that distruct ran properly.")
print("This program does not check the exit status of DISTRUCT because its exit status always equals 1.")
print("")
def fileExists(self, filename):
if( os.path.isfile(filename) != True ):
print( filename, "does not exist" )
print( "Exiting program..." )
print( "" )
raise SystemExit
else:
print(filename, "Exists")
def makecbsuffixdict(self):
cbdict = dict()
cbdict["Accent"]="qual"
cbdict["Blues"]="seq"
cbdict["BrBG"]="div"
cbdict["BuGn"]="seq"
cbdict["BuPu"]="seq"
cbdict["Dark2"]="qual"
cbdict["GnBu"]="seq"
cbdict["Greens"]="seq"
cbdict["Greys"]="seq"
cbdict["Oranges"]="seq"
cbdict["OrRd"]="seq"
cbdict["Paired"]="qual"
cbdict["Pastel1"]="qual"
cbdict["Pastel2"]="qual"
cbdict["PiYG"]="div"
cbdict["PRGn"]="div"
cbdict["PuBuGn"]="seq"
cbdict["PuBu"]="seq"
cbdict["PuOr"]="div"
cbdict["PuRd"]="seq"
cbdict["Purples"]="seq"
cbdict["RdBu"]="div"
cbdict["RdGy"]="div"
cbdict["RdPu"]="seq"
cbdict["RdYlBu"]="div"
cbdict["RdYlGn"]="div"
cbdict["Reds"]="seq"
cbdict["Set1"]="qual"
cbdict["Set2"]="qual"
cbdict["Set3"]="qual"
cbdict["Spectral"]="div"
cbdict["YlGnBu"]="seq"
cbdict["YlGn"]="seq"
cbdict["YlOrBr"]="seq"
cbdict["YlOrRd"]="seq"
return cbdict