-
Notifications
You must be signed in to change notification settings - Fork 8
/
setup_new_proj.py
439 lines (382 loc) · 17.3 KB
/
setup_new_proj.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#
# setup_new_proj.py - create a new PhysiCell Jupyter notebook project [for nanoHUB] by copying the contents of this
# project and an existing PhysiCell project into the new project.
#
# Assumptions:
# - the new project is a cloned, empty, public GitHub repo
# - you have an existing PhysiCell project with files in specified locations:
# - config/PhysiCell_settings.xml
# - output/initial.xml)
#
# Usage:
# python setup_new_proj.py <full-path-to-new-project> <simple-project-name> <full-path-to-PhysiCell-project>
# e.g.:
# python setup_new_proj.py /Users/heiland/git/iu399_proj1 iu399_proj1 /Users/heiland/dev/PhysiCell_heterogeneity
#
import sys
import shutil
import os
import platform
import xml.etree.ElementTree as ET
num_args = len(sys.argv)
print('num_args=',num_args)
if (num_args < 4):
# print("Usage: %s <full-path-to-new-project> <simple-project-name> <full-path-to-PhysiCell-project>")
print("Usage: %s <full-path-to-new-project> <full-path-to-PhysiCell-project> <tool name> [<makefile name> <main cpp>]")
# print("Usage: %s <your repo name>")
sys.exit(1)
#print('sys.argv[0] = ',sys.argv[0])
proj_fullpath = sys.argv[1]
if not proj_fullpath[-1].isalnum():
proj_fullpath = proj_fullpath[0:-1]
print('proj_fullpath = ',proj_fullpath)
#proj_name = sys.argv[2]
proj_name = os.path.basename(proj_fullpath)
print('proj_name = ',proj_name)
physicell_fullpath= sys.argv[2]
print('physicell_fullpath = ',physicell_fullpath)
tool_name = sys.argv[3]
print('tool_name = ',tool_name)
make_file = "Makefile"
main_cpp_file = "main.cpp"
if (num_args > 4):
make_file = sys.argv[4]
main_cpp_file = sys.argv[5]
print("\n====== STEP 1: copy tool4nanobio to new project:\n")
to_file = os.path.join(proj_fullpath, ".travis.yml") # (from_file, to_file)
shutil.copy(".travis.yml", to_file)
for elm in os.listdir('.'):
print("---- elm (dir)= ",elm)
try:
if ('Example_GUI' in elm): # avoid /.git, etc
continue
elif (elm[0] != '.'): # avoid /.git, etc
if os.path.isdir(elm):
from_dir = elm
to_dir = os.path.join(proj_fullpath, elm)
print(from_dir, " --> ", to_dir)
# shutil.copytree(elm, os.path.join(proj_fullpath, elm)) # (from_dir, to_dir)
shutil.copytree(from_dir, to_dir)
else:
if (elm == sys.argv[0] or elm == "README.md"):
# print('------- skipping over ',elm)
continue
from_file = elm
to_file = os.path.join(proj_fullpath, elm) # (from_file, to_file)
print(from_file, " --> ", to_file)
shutil.copy(from_file, to_file)
except:
# print(" can't copy ",elm," to ", proj_fullpath, " ... maybe you already did?")
print(" can't copy ... maybe you already did?")
#------------------------------------
print("\n\n====== STEP 2: copy PhysiCell project's source (and data files) to new project's /src:\n")
# Copy (most of) your PhysiCell project to your project's /src directory
#os.chdir(os.path.join(proj_fullpath, 'src'))
proj_src_dir = os.path.join(proj_fullpath, 'src')
print("proj_src_dir = ",proj_src_dir)
dir_names = ["core", "BioFVM", "modules", "custom_modules"]
for dname in dir_names:
from_dir = os.path.join(physicell_fullpath, dname)
to_dir = os.path.join(proj_src_dir, dname)
print(from_dir, " --> ", to_dir)
try:
shutil.copytree(from_dir, to_dir)
except:
print(" can't copy ... maybe you already did?")
#from_file = os.path.join(physicell_fullpath, "Makefile")
from_file = os.path.join(physicell_fullpath, make_file)
to_file = os.path.join(proj_src_dir, "Makefile")
print(from_file, " --> ", to_file, " plus, create 'myproj' and insert nanoHUB-specific targets.")
#shutil.copy(from_file, to_file)
with open(from_file,"r") as infile:
with open(to_file,"w") as outfile:
for line in infile:
sline = line.split()
if len(sline) > 0:
if sline[0] == "PROGRAM_NAME":
# print('got PROGRAM_NAME')
newline = sline[0] + sline[1] + " myproj\n"
outfile.write(newline)
# outfile.write("\nUNAME := $(shell uname)\n")
#--------- new crap that Steve had me add during dev of pc4covid19 ------------------
# nanohub_extra_crap = "install: \n\t$(MODINIT); $(MODCMD); make all\n\tinstall --mode 0755 -D $(PROGRAM_NAME) $(BIN)/$(PROGRAM_NAME)\n\ndistclean: clean\n\trm -f $(BIN)/$(PROGRAM_NAME)\n\trm -rf $(BIN)/__pycache__\n\trm -rf ../.ipynb_checkpoints\n\n"
nanohub_extra_crap = 'osRelease = $(shell lsb_release -r | sed -e "s/Release:\W*//" -e "s/\..*//")\nifeq ($(osRelease),7)\n\thostName = $(shell hostname | sed -e "s:[-_].*::")$(osRelease)\nelse\n\thostName = $(shell hostname | sed -e "s:[-_].*::")\nendif\n\nifeq ($(hostName),nanohub7)\n\tBIN = ../bin\n\tMODINIT = . /etc/environ.sh\n\tMODCMD = use -e -r anaconda3-5.1\nelse\nifeq ($(hostName),rice7)\n\tBIN = ../bin/rice7\n\tMODINIT = . $(MODULESHOME)/init/sh\n\tMODCMD = module purge 2> /dev/null; module load gcc/7.3.0\nendif\nifeq ($(hostName),brown7)\n\tBIN = ../bin/brown7\n\tMODINIT = . $(MODULESHOME)/init/sh\n\tMODCMD = module purge 2> /dev/null; module load gcc/7.3.0\nendif\nendif\n'
outfile.write(nanohub_extra_crap)
# osRelease = $(shell lsb_release -r | sed -e "s/Release:\W*//" -e "s/\..*//")
# ifeq ($(osRelease),7)
# hostName = $(shell hostname | sed -e "s:[-_].*::")$(osRelease)
# else
# hostName = $(shell hostname | sed -e "s:[-_].*::")
# endif
# ifeq ($(hostName),nanohub7)
# BIN = ../bin
# MODINIT = . /etc/environ.sh
# MODCMD = use -e -r anaconda3-5.1
# else
# ifeq ($(hostName),rice7)
# BIN = ../bin/rice7
# MODINIT = . $(MODULESHOME)/init/sh
# MODCMD = module purge 2> /dev/null; module load gcc/7.3.0
# endif
# ifeq ($(hostName),brown7)
# BIN = ../bin/brown7
# MODINIT = . $(MODULESHOME)/init/sh
# MODCMD = module purge 2> /dev/null; module load gcc/7.3.0
# endif
# endif
elif sline[0][0:6] == "CFLAGS":
newline = "# remove the -march arg to avoid SIGILL error on nanoHUB\n"
outfile.write(newline)
newline = "CFLAGS := -O3 -fomit-frame-pointer -mfpmath=both -fopenmp -m64 -std=c++11\n"
outfile.write(newline)
elif sline[0] == "clean:":
# print('got ',sline[0])
# outfile.write(line)
# nanohub_targets = "# next 2 targets for nanoHUB\ninstall: \n\t. /etc/environ.sh; use -e -r anaconda3-5.1; make all\n\tcp $(PROGRAM_NAME) ../bin\n\ndistclean: clean\n\trm -f ../bin/$(PROGRAM_NAME)\n\n"
nanohub_targets = "# next 2 targets for nanoHUB\ninstall: \n\t$(MODINIT); $(MODCMD); make all\n\tinstall --mode 0755 -D $(PROGRAM_NAME) $(BIN)/$(PROGRAM_NAME)\n\ndistclean: clean\n\trm -f $(BIN)/$(PROGRAM_NAME)\n\trm -rf $(BIN)/__pycache__\n\trm -rf ../.ipynb_checkpoints\n\n"
outfile.write(nanohub_targets)
outfile.write("clean:\n")
elif "rm -f $(PROGRAM_NAME)*" in line: # don't want last "*" on nanoHUB
# outfile.write("\tifeq ($(OS),Windows_NT)\n")
# outfile.write("\t\trm -f $(PROGRAM_NAME)*\n")
# outfile.write("\telse\n")
# outfile.write("\t\trm -f $(PROGRAM_NAME)\n")
outfile.write("\trm -f $(PROGRAM_NAME)\n") # what happens on Windows?
else:
outfile.write(line)
else:
outfile.write(line)
#from_file = os.path.join(physicell_fullpath, "main.cpp")
#to_file = os.path.join(proj_src_dir, "main.cpp")
from_file = os.path.join(physicell_fullpath, main_cpp_file)
to_file = os.path.join(proj_src_dir, main_cpp_file)
print(from_file, " --> ", to_file)
shutil.copy(from_file, to_file)
from_file = os.path.join(physicell_fullpath, "VERSION.txt")
to_file = os.path.join(proj_src_dir, "VERSION.txt")
print(from_file, " --> ", to_file)
shutil.copy(from_file, to_file)
# We want to copy (and slightly edit) the original config (.xml) file (in /config) into the app's /data directory.
from_file = os.path.join(physicell_fullpath, "config", "PhysiCell_settings.xml")
to_file = os.path.join(proj_fullpath, "data", "PhysiCell_settings.xml")
print(from_file, " --> ", to_file)
shutil.copy(from_file, to_file)
# Edit the output folder: "output" --> "." (output will be diverted into the app's /tmpdir)
# old_way = False
# print(" Editing ", to_file, ": <folder>output</folder> --> <folder>.</folder>")
# if old_way:
# with open(to_file, 'r') as myfile:
# new_text = myfile.read().replace('output', ".") # a rather hacky way of doing this for now
# with open(to_file, 'w') as myfile:
# myfile.write(new_text)
# else:
tree = ET.parse(to_file)
xml_root = tree.getroot()
uep = xml_root.find('.//save//folder') # find unique entry point
uep.text = '.'
uep = xml_root.find('.//parallel//omp_num_threads')
uep.text = '4'
uep2 = xml_root.find('.//initial_conditions//cell_positions//folder')
print('\n >>>>>>>>>>> uep2 (cell pos) = ',uep2)
if uep2 != None:
print('>>>>>>>>>>> changing <initial_conditions><folder> = data')
uep2.text = '../data' # recall, the executable is in /bin
tree.write(to_file)
uep2 = xml_root.find('.//initial_conditions//cell_positions//filename')
if uep2 == None:
print("****** FYI: no initial_conditions//cell_positions//filename to process.")
else:
csv_file = uep2.text
from_file = os.path.join(physicell_fullpath, "config", csv_file)
to_file = os.path.join(proj_fullpath, "data", csv_file)
print(from_file, " --> ", to_file)
shutil.copy(from_file, to_file)
# Similar to the config file going into /data, we also want the original output/initial.xml to be in /data
from_file = os.path.join(physicell_fullpath, "output", "initial.xml")
to_file = os.path.join(proj_fullpath, "data", "initial.xml")
print(from_file, " --> ", to_file)
shutil.copy(from_file, to_file)
#------------------------------------
print("\n\n====== STEP 3: renaming files and file content to have new project and tool names:\n")
# maybe attempt to execute 'make_my_tool.py' from the new project dir?
print("\n----- Now we will prepare your new project. ")
os.chdir(proj_fullpath)
gui_name = os.path.basename(proj_fullpath)
print('gui_name=',gui_name)
# NOTE: let's not do this now; rather edit the invoke script *on* github to avoid
# the (Windows) problem of making it a non-executable file
with open('middleware/invoke', 'r') as myfile:
# new_text = myfile.read().replace('tool4nanobio', gui_name)
new_text = myfile.read().replace("-t tool4nanobio", "-t " + tool_name)
new_text = new_text.replace("tool4nanobio", gui_name)
with open('middleware/invoke', 'w') as myfile:
myfile.write(new_text)
#--------------
old_file = os.path.join("bin", 'tool4nanobio.py')
new_file = os.path.join("bin", gui_name + '.py')
try:
shutil.move(old_file, new_file)
print('Renaming ',old_file, ' to ',new_file)
except:
print(" ---> Cannot rename ",old_file," to ",new_file, ", but we will continue")
print('Replacing gui_name in ',new_file)
with open(new_file, 'r') as myfile:
new_text = myfile.read().replace('tool4nanobio', gui_name)
with open(new_file, 'w') as myfile:
myfile.write(new_text)
#--------------
old_file = 'tool4nanobio.ipynb'
new_file = gui_name + '.ipynb'
try:
shutil.move(old_file, new_file)
print('Renaming ',old_file, ' to ',new_file)
except:
print(" ---> Cannot rename ",old_file," to ",new_file, ", but we will continue")
print('Replacing gui_name in ',new_file)
with open(new_file, 'r') as myfile:
new_text = myfile.read().replace('tool4nanobio', gui_name)
with open(new_file, 'w') as myfile:
myfile.write(new_text)
#--------------
"""
In /data:
python xml2jupyter.py PhysiCell_settings.xml
Copy the generated user_params.py to ../bin
"""
print('--------------------------------------------------------')
print('Trying to run xml2jupyter.py on your .xml file in /data')
os.chdir("data")
cmd = "python xml2jupyter.py PhysiCell_settings.xml"
print(cmd)
try:
os.system("python xml2jupyter.py PhysiCell_settings.xml")
except:
print(" ---> Cannot execute: ",cmd)
new_file = os.path.join("..","bin")
try:
print("-------------------------------------")
print("cp user_params.py " + new_file)
shutil.copy("user_params.py", new_file)
except:
print(" ---> Cannot copy data/user_params.py to bin/user_params.py")
print(" You will need to do that manually.\n")
try:
print("-------------------------------------")
print("cp microenv_params.py " + new_file)
shutil.copy("microenv_params.py", new_file)
except:
print(" ---> Cannot copy data/microenv_params.py to bin/microenv_params.py")
print(" You will need to do that manually.\n")
print("-------------------------------------")
config_file = "PhysiCell_settings.xml"
tree = ET.parse(config_file)
root = tree.getroot()
if root.find('.//cell_definitions'):
#os.chdir("..")
# print('Trying to run create_cell_def.py on your .xml file in /data')
print('------ <cell_definitions> are present.')
#os.chdir("data")
backup_xml_file = "PhysiCell_settings_original.xml"
print('-- creating a backup of original: ',backup_xml_file)
shutil.copy(config_file, backup_xml_file)
# -------- NEW: March 2021
# Before we create the tab of widgets for cell_definitions, we want to
# convert a "hierarchical" cell_definitions (using 'parent_type' attribute)
# to "flattened" cell_definitions (there are no parents; each cell_def is expanded).
print('------ Convert hierarchical <cell_definitions> to flattened.')
cmd = 'python xml_hier2flat.py ' + config_file
print('------> ',cmd)
try:
os.system(cmd)
except:
print(" ---> Cannot execute: ",cmd)
# shutil.copy("recurse_xml_out.xml", config_file)
shutil.copy("flat_xml_out.xml", config_file)
#-------
# OK, now continue with the creation of the tab ("Cell Types") of widgets for cell_definitions
print('-----------------------------------------------------------')
print('Trying to run create_cell_def.py on your .xml file in /data')
cmd = "python create_cell_types.py PhysiCell_settings.xml"
print(cmd)
try:
os.system("python create_cell_types.py PhysiCell_settings.xml")
except:
print(" ---> Cannot execute: ",cmd)
new_file = os.path.join("..","bin")
try:
print("Trying: cp cell_types.py " + new_file)
shutil.copy("cell_types.py", new_file)
except:
print(" ---> Cannot copy data/cell_types.py to bin/cell_types.py")
print(" You will need to do that manually.\n")
else:
print(".xml does not contain <cell_definitions>, so will not create that tab")
print('---------------------------\n')
#----------------------------------
if platform.system() != 'Windows':
try:
print("Trying to import hublib.ui")
import hublib.ui
except:
print("hublib.ui is not found, will try to install it.")
os.system("pip install -U hublib")
print("\n==================== doing sys.exit(1) =======================")
sys.exit(1)
# NOTE: let's not do this now; rather edit the invoke script *on* github to avoid
# the (Windows) problem of making it a non-executable file
#with open('middleware/invoke', 'r') as myfile:
# new_text = myfile.read().replace('tool4nanobio', gui_name)
#with open('middleware/invoke', 'w') as myfile:
# myfile.write(new_text)
#--------------
old_file = os.path.join("bin", 'tool4nanobio.py')
new_file = os.path.join("bin", gui_name + '.py')
try:
shutil.move(old_file, new_file)
print('Renaming ',old_file, ' to ',new_file)
except:
print(" ---> Cannot rename ",old_file," to ",new_file, ", but we will continue")
print('Replacing gui_name in ',new_file)
with open(new_file, 'r') as myfile:
new_text = myfile.read().replace('tool4nanobio', gui_name)
with open(new_file, 'w') as myfile:
myfile.write(new_text)
#--------------
old_file = 'tool4nanobio.ipynb'
new_file = gui_name + '.ipynb'
try:
shutil.move(old_file, new_file)
print('Renaming ',old_file, ' to ',new_file)
except:
print(" ---> Cannot rename ",old_file," to ",new_file, ", but we will continue")
print('Replacing gui_name in ',new_file)
with open(new_file, 'r') as myfile:
new_text = myfile.read().replace('tool4nanobio', gui_name)
with open(new_file, 'w') as myfile:
myfile.write(new_text)
#--------------
"""
In /data:
python xml2jupyter.py PhysiCell_settings.xml
Copy the generated user_params.py to ../bin
"""
print('Trying to run xml2jupyter.py on your .xml file in /data')
os.chdir("data")
cmd = "python xml2jupyter.py PhysiCell_settings.xml"
try:
os.system("python xml2jupyter.py PhysiCell_settings.xml")
except:
print(" ---> Cannot execute: ",cmd)
new_file = os.path.join("..","bin")
try:
shutil.copy("user_params.py", new_file)
except:
print(" ---> Cannot copy data/user_params.py to bin/user_params.py")
print(" You will need to do that manually.\n")
if platform.system() != 'Windows':
try:
print("Trying to import hublib.ui")
import hublib.ui
except:
print("hublib.ui is not found, will try to install it.")
os.system("pip install -U hublib")