-
Notifications
You must be signed in to change notification settings - Fork 0
/
bioem_toolkit.py
1530 lines (1407 loc) · 67.4 KB
/
bioem_toolkit.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
import sys, os, stat, shutil, argparse, zipfile, time
import subprocess
import pandas as pd
import mrcfile as mrc
import numpy as np
# import ray
# adding library to the system path
# from multiprocessing import Process
#TODO refactor code so that consensus is just a type of job.
# TODO submit make orientation for consensus job.
# TODO make grid multiplication a part of round 1.
sys.path.insert(0, "helper_functions.py")
from helper_functions import *
#################################### NORMAL CLASSES
class NORMAL_MODE_ROUND1:
"""A simple example class"""
def __init__(
self, model_path, model_list, group_list, param_path, particle_path, output_path
):
self.model_path = model_path
self.model_list = model_list
self.group_list = group_list
self.param_path = param_path
self.particle_path = particle_path
self.output_path = output_path
def PREP(self):
global partition_choice #### MAYBE ADD N_NODE
partition_choice = choosing_cluster(1)
MODELS_LIST = open(self.model_list)
MODELS = MODELS_LIST.readlines()
GROUPS = pd.read_csv(
self.group_list,
names=["particle_file", "group", "start", "end", "nframe"],
delim_whitespace="True",
comment="#",dtype=str
)
# print(GROUPS)
# Strips the newline character
for MODEL in MODELS:
MODEL = MODEL.strip()
if MODEL[0] == "#":
print("%s is skipped." % (MODEL[1:]))
continue
a_model_path = os.path.join(op_v, MODEL)
os.makedirs(a_model_path, exist_ok="True")
os.makedirs(os.path.join(a_model_path, "round1"), exist_ok="True")
for ind, GROUP in GROUPS.iterrows():
round1_path = os.path.join(a_model_path,"round1")
r1_group_path = os.path.join(round1_path, GROUP['group'])
os.makedirs(r1_group_path, exist_ok='True')
shutil.copy(os.path.join(mp_v, MODEL + ".txt"), a_model_path)
shutil.copy(param_v + "/Quat_36864", round1_path)
with open(param_v + "/Param_BioEM_template", "r+") as file:
param_file = file.read()
param_file_out_path = str(round1_path) + "/Param_BioEM_%s"%(GROUP['group'])
# print(param_file_out_path)
param_file = param_file.replace("WhereRound1AngleFile", r1_group_path+"/angle_output_probabilities.txt")
with open(param_file_out_path, "w+") as outfile:
outfile.write(param_file)
os.chmod(param_file_out_path, stat.S_IRWXU)
with open(param_v + "/slurm-r1-template.sh", "r+") as file:
slurm_file = file.read()
slurm_file_out_path = str(r1_group_path) + "/slurm-r1-rusty.sh"
# print(slurm_file_out_path)
slurm_file = slurm_file.replace("WhereSlurm", r1_group_path)
slurm_file = slurm_file.replace("WhatModel", MODEL)
slurm_file = slurm_file.replace(
"WhereParticle",
os.path.join(self.particle_path, GROUP["particle_file"]),
)
slurm_file = slurm_file.replace("WhereModel", os.path.join(self.model_path))
slurm_file = slurm_file.replace("WhatGroup", GROUP["group"])
slurm_file = slurm_file.replace("WhatPartition", partition_choice)
slurm_file = slurm_file.replace("WhereParam", os.path.join(round1_path,"Param_BioEM_%s"%(GROUP['group'])))
slurm_file = slurm_file.replace("WhereQuatern", os.path.join(round1_path,"Quat_36864") )
slurm_file = slurm_file.replace("WhereOutputStored", os.path.join(r1_group_path,"Output_Probabilities") )
with open(slurm_file_out_path, "w+") as outfile:
outfile.write(slurm_file)
os.chmod(slurm_file_out_path, stat.S_IRWXU)
def RUN(self):
# print('running!')
partition_choice = choosing_cluster(1)
centraltask_path = os.path.join(self.output_path,"0-CentralTask")
os.makedirs(centraltask_path,exist_ok=True)
MODELS_LIST = open(self.model_list)
MODELS = MODELS_LIST.readlines()
GROUPS = pd.read_csv(
self.group_list,
names=["particle_file", "group", "start", "end", "nframe"],
delim_whitespace="True",
comment="#",dtype=str
)
centraltask_filename ='CENTRAL_TASK_R1'
with open(centraltask_path+"/CENTRAL_TASK_R1","w+") as ct:
for MODEL in MODELS:
MODEL = MODEL.strip()
if MODEL[0] == "#":
print("%s is skipped." % (MODEL[1:]))
continue
a_model_path = os.path.join(op_v, MODEL)
round1_path = os.path.join(a_model_path, "round1")
for ind, GROUP in GROUPS.iterrows():
r1_group_path = os.path.join(round1_path, GROUP["group"])
r1_slurm_file_path = os.path.join(r1_group_path,"slurm-r1-rusty.sh")
r1_slurm_file_abs = os.path.abspath(r1_slurm_file_path)
ct.write("%s &> REPORT_R1_%s_%s\n"%(r1_slurm_file_abs,MODEL,GROUP['group']))
cwd = os.getcwd()
os.chdir(centraltask_path)
n_task = input("How many tasks to run concurrently using disBatch? This will utilize 128 core/node.\n")
#### We request to run n=1 task alone in the /CENTRAL_TASK_R1 on a node (-c 128 cores). This needs to be adjusted for optimized performance!!!
sbatch_cmd = ('sbatch -n %s -c 128 -p %s -J R1 disBatch %s' % (
n_task,
partition_choice,
centraltask_filename,
)
)
subprocess.run(sbatch_cmd,shell=True,check=True)
os.chdir(cwd)
# cwd = os.getcwd()
# os.chdir(r1_group_path)
# slurm_file_out_path = "slurm-r1-rusty.sh"
# os.chmod(slurm_file_out_path, stat.S_IRWXU)
# # print(slurm_file_out_path)
# sbatch_cmd = ('sbatch %s'%(slurm_file_out_path))
# subprocess.run(str(sbatch_cmd), shell=True, check=True)
# os.chdir(cwd)
class NORMAL_MODE_ROUND2:
def __init__(
self, model_path, model_list, group_list, param_path, particle_path, output_path
):
self.model_path = model_path
self.model_list = model_list
self.group_list = group_list
self.param_path = param_path
self.particle_path = particle_path
self.output_path = output_path
def PREP(self):
global cluster_choice, partition_choice
partition_choice = choosing_cluster(0)
if partition_choice is not None:
n_node = input("How many nodes to request for disBatch?\n")
# n_cpu = int(n_node)*128
else: ### LOCAL MACHINE
n_node = "1"
n_cpu = "32"
centraltask_path = os.path.join(op_v,"1-QMTask")
if os.path.isdir(centraltask_path) is True:
shutil.rmtree(centraltask_path)
MODELS_LIST = open(self.model_list)
MODELS = MODELS_LIST.readlines()
GROUPS = pd.read_csv(
self.group_list,
names=["particle_file", "group", "start", "end", "nframe"],
delim_whitespace="True",
comment="#",dtype=str
)
# print(GROUPS)
# Strips the newline character
centralTask_R2_path = os.path.join(self.output_path,"2-CentralTask-R2")
check_path = os.path.isdir(centralTask_R2_path)
if check_path is True:
overwrite = input("\n========== Overwrite %s? Y/N\n"%(centralTask_R2_path))
if overwrite =='Y' or overwrite =='y':
shutil.rmtree(centralTask_R2_path)
os.makedirs(centralTask_R2_path,exist_ok = True)
else:
print("\n========== 2-CentralTask-R2 IS NOT UPDATED!!\n")
else:
os.makedirs(centralTask_R2_path,exist_ok = True)
task_path = os.path.join(
centralTask_R2_path,"CENTRAL_TASK_R2_LAUNCH"
)
for MODEL in MODELS:
MODEL = MODEL.strip()
if MODEL[0] == "#":
print("%s is skipped." % (MODEL[1:]))
continue
a_model_path = os.path.join(op_v, MODEL)
os.makedirs(a_model_path, exist_ok="True")
os.makedirs(os.path.join(a_model_path, "round2"), exist_ok="True")
round1_path = os.path.join(a_model_path, "round1")
for ind, GROUP in GROUPS.iterrows():
round2_path = os.path.join(a_model_path, "round2")
r1_group_path = os.path.join(round1_path, GROUP["group"])
r2_group_path = os.path.join(round2_path, GROUP["group"])
os.makedirs(r2_group_path, exist_ok="True")
# print(r2_group_path)
subdir_list = [
"/parameters",
"/orientations",
"/tasks",
"/outputs",
"/tmp_files",
]
output_path = os.path.join(r2_group_path,"outputs")
orientations_path = os.path.join(r2_group_path,"orientations")
parameters_path = os.path.join(r2_group_path,"parameters")
output_path_abs = os.path.abspath(output_path)
orientations_path_abs = os.path.abspath(orientations_path)
parameters_path_abs = os.path.abspath(parameters_path)
for sub_dir in range(len(subdir_list)):
os.makedirs(r2_group_path + subdir_list[sub_dir], exist_ok="True")
group_param_path = os.path.join(
r2_group_path + subdir_list[sub_dir]
)
if os.path.basename(group_param_path) == "tmp_files":
shutil.copy(
param_v + "/Param_BioEM_template", group_param_path
)
param_bio_template_path = os.path.join(
group_param_path, "Param_BioEM_template"
)
with open(param_bio_template_path, "r+") as file:
param_file = file.readlines()
param_file.remove("ANG_PROB_FILE WhereRound1AngleFile\n") ### FOR SPEEDING UP SINCE R2 DOES NOT NEED TO WRITE OUT BEST ANGLES
param_file.remove("WRITE_PROB_ANGLES 300\n")
# param_file = param_file.replace("WhereRound1AngleFile", r2_group_path+"/tmp_files/angle_output_probabilities.txt")
string = " ".join(map(str, param_file))
with open(param_bio_template_path, "w+") as outfile:
outfile.write(str(string))
os.chmod(param_bio_template_path, stat.S_IRWXU)
shutil.copy(
r1_group_path + "/Output_Probabilities",
group_param_path + "/Output_Probabilities-R1",
)
Out_Prob_R1_path = os.path.join(
group_param_path, "Output_Probabilities-R1"
)
clean_R1_Probability(
r2_group_path,
Out_Prob_R1_path,
param_bio_template_path,
GROUP
)
print("\n========== Done with PARAMETER FILES for %s" % (MODEL))
r1_prob = group_param_path + "/PROB_ANGLE_R1.txt"
shutil.copy(
r1_group_path + "/angle_output_probabilities.txt",
r1_prob,
)
making_orientations_submission (
libraryParmPath=self.param_path,
r1_foo=r1_prob,
model_now=MODEL,
group_now=GROUP['group'],
model_tmp_path=group_param_path,
model_group_path=r2_group_path,
partition_choice=partition_choice,
n_node=n_node,
n_cpu=n_cpu,
path_to_output=self.output_path,
startFrame=GROUP['start']
)
elif os.path.basename(group_param_path) == "tasks":
# if os.path.basename(group_param_path)=="tasks": # FOR TESTING
shutil.copy(
param_v + "/launch-one-NONCONSENSUS-template.sh", group_param_path
)
launch_one_path = os.path.join(
group_param_path, "launch-one-NONCONSENSUS-template.sh"
)
# print(launch_one_path)
with open(launch_one_path, "r+") as launchIn:
with open(
group_param_path + "/launch-one.sh", "w+"
) as launchOut:
lines = launchIn.readlines()
for line in lines:
line = line.split()
# print(line)
if len(line) >= 2:
if line[1] == "SLURM_JOB_NAME=WhatModel-R2":
line[1] = "SLURM_JOB_NAME=%s-R2" % (MODEL)
elif line[1] == "WhereRound2=WhereRound2":
line[1] = "WhereRound2=%s" % (round2_path)
elif line[1] == "WhereParticles=WhereParticles":
line[1] = "WhereParticles=%s" % (
self.particle_path
)
elif line[1] == "WhereModel=WhereModel":
line[1] = "WhereModel=%s" % (
os.path.abspath(self.model_path)
)
elif line[1] =="WhereOutput=WhereOutput":
line[1] = "WhereOutput=%s" % (
output_path_abs
)
elif line[1] =="WhereOrientation=WhereOrientation":
line[1] = "WhereOrientation=%s" % (
orientations_path_abs
)
elif line[1] =="WhereParm=WhereParm":
line[1] = "WhereParm=%s" % (
parameters_path_abs
)
# print(*line)
string = " ".join(map(str, line))
launchOut.write(string + "\n")
launchOut.close()
launchIn.close()
os.chmod(group_param_path + "/launch-one.sh", stat.S_IRWXU)
# os.remove(launch_one_path)
launch_one_group_path = os.path.join(group_param_path,"launch-one.sh")
launch_one_group_path_abs = os.path.abspath(launch_one_group_path)
with open(task_path, "a+") as task:
for i in range(int(GROUP["start"]),int(GROUP["end"])+1):
# print(i)
launch_one_command = (
"%s %s %s %s &>> REPORT_R2_%s_%s" ################################
% (launch_one_group_path_abs,i, GROUP["group"], MODEL,GROUP["group"],MODEL)
)
task.write(launch_one_command + "\n")
print(
"\n========== Done with creating Task File for %s" % (MODEL)
)
centraltask_filename = "CENTRAL_TASK_R2_QM"
cwd = os.getcwd()
os.chdir(centraltask_path)
# n_core = n_node*128
n_task = str(int(n_node)*128)
sbatch_cmd = ('sbatch -n %s -c 1 -p %s -J QM disBatch %s' % (
n_task,
#n_cpu,
partition_choice,
centraltask_filename,
)
)
subprocess.run(sbatch_cmd,shell=True,check=True)
os.chdir(cwd)
def RUN(self):
central_task_r2_path = os.path.join(self.output_path,"2-CentralTask-R2")
os.makedirs(central_task_r2_path, exist_ok=True)
partition_choice = choosing_cluster(0)
try:
subprocess.check_output(
["disBatch", "--help"], stderr=subprocess.STDOUT
).decode("utf8")
print("\n========== disBatch is LOADED. SUBMIT JOBS NOW!\n")
except:
print(
"\nYou need to load disBatch to launch ROUND 2. PROGRAM TERMINATED!!!\n"
)
# else:
# MODELS_LIST = open(self.model_list)
# MODELS = MODELS_LIST.readlines()
# GROUPS = pd.read_csv(
# self.group_list,
# names=["particle_file", "group", "start", "end", "nframe"],
# delim_whitespace="True",
# comment="#",
# )
# for MODEL in MODELS:
# MODEL = MODEL.strip()
# if MODEL[0] == "#":
# print("========== %s is skipped." % (MODEL[1:]))
# continue
# a_model_path = os.path.join(op_v, MODEL)
# round2_path = os.path.join(a_model_path, "round2")
# for ind, GROUP in GROUPS.iterrows():
centraltask_r2_path = os.path.join(self.output_path, "2-CentralTask-R2")
centraltask_r2_file_path = os.path.join(centraltask_r2_path,"CENTRAL_TASK_R2_LAUNCH")
########### THIS NEED TO MOVE OUTOF THE LOOP
current_dir = os.getcwd()
# print(current_dir,task_path)
os.chdir(centraltask_r2_path)
# print(os.getcwd())
# sbatch -n 2 -c 128 -p ccm -J test disBatch CENTRAL_TASK_R1
n_node = input("How many nodes to request for disBatch?\n")
n_task = str(int(n_node)*128)
sbatch_cmd = ('sbatch -n %s -c 1 -p %s -J R2 disBatch %s' % (
n_task,
partition_choice,
centraltask_r2_file_path,
)
)
subprocess.run(sbatch_cmd,shell=True,check=True)
os.chdir(current_dir)
def CLEAN(self):
delete_choice = input(
"Do you want to keep the original files? Choose (0) NO or (1) YES\n"
)
MODELS_LIST = open(self.model_list)
MODELS = MODELS_LIST.readlines()
GROUPS = pd.read_csv(
self.group_list,
names=["particle_file", "group", "start", "end", "nframe"],
delim_whitespace="True",
comment="#",
)
for MODEL in MODELS:
MODEL = MODEL.strip()
if MODEL[0] == "#":
print("%s is skipped." % (MODEL[1:]))
continue
a_model_path = os.path.join(op_v, MODEL)
round2_path = os.path.join(a_model_path, "round2")
print("\n========== Now cleaning %s" % (MODEL))
for ind, GROUP in GROUPS.iterrows():
r2_group_path = os.path.join(round2_path, GROUP["group"])
path_to_your_mess = os.path.join(r2_group_path, "outputs")
process_output_round2(
delete_choice,
MODEL,
GROUP["group"],
path_to_your_mess,
GROUP["nframe"],
GROUP["start"],
GROUP["end"]
)
def CLEAN_PARAMS(self):
delete_choice = input(
"Do you want to keep the original files? Choose (0) NO or (1) YES\n"
)
MODELS_LIST = open(self.model_list)
MODELS = MODELS_LIST.readlines()
GROUPS = pd.read_csv(
self.group_list,
names=["particle_file", "group", "start", "end", "nframe"],
delim_whitespace="True",
comment="#",
)
for MODEL in MODELS:
MODEL = MODEL.strip()
if MODEL[0] == "#":
print("%s is skipped." % (MODEL[1:]))
continue
a_model_path = os.path.join(op_v, MODEL)
round2_path = os.path.join(a_model_path, "round2")
GROUP = None
print("========== Now cleaning %s" % (MODEL))
for ind, GROUP in GROUPS.iterrows():
r2_group_path = os.path.join(round2_path, GROUP["group"])
clean_params(
delete_choice, MODEL, GROUP["group"], r2_group_path, GROUP["nframe"], GROUP["start"],GROUP["end"]
)
#################################### CONSENSUS CLASSES
class CONSENSUS_MODE_ROUND_1:
def __init__(
self, model_path, model_list, group_list, param_path, particle_path, output_path
):
self.model_path = model_path
self.model_list = model_list
self.group_list = group_list
self.param_path = param_path
self.particle_path = particle_path
self.output_path = output_path
def PREP(self):
global cluster_choice, partition_choice
GROUPS = pd.read_csv(
self.group_list,
names=["particle_file", "group", "start", "end", "nframe"],
delim_whitespace="True",
comment="#",dtype=str,
)
if args.command_line_mode==False:
consensus_MODEL_name = input("Please provide the CONSENSUS MODEL NAME:\n")
else:
consensus_MODEL_name = args.consensus_model
consensus_MODEL_path = os.path.join(self.output_path, consensus_MODEL_name)
os.makedirs(consensus_MODEL_path, exist_ok="True")
MODEL = consensus_MODEL_name
print("========== CONSENSUS PATH: %s" % (consensus_MODEL_path))
round1_path = os.path.join(consensus_MODEL_path, "round1")
os.makedirs(round1_path, exist_ok="True")
for ind, GROUP in GROUPS.iterrows():
r1_group_path = os.path.join(round1_path, GROUP["group"])
os.makedirs(r1_group_path, exist_ok="True")
shutil.copy(os.path.join(mp_v, MODEL + ".txt"), consensus_MODEL_path)
shutil.copy(
param_v + "/Param_BioEM_template", round1_path + "/Param_BioEM_ABC"
)
shutil.copy(param_v + "/Quat_36864", round1_path)
with open(param_v + "/slurm-r1-template.sh", "r+") as file:
slurm_file = file.read()
slurm_file_out_path = str(r1_group_path) + "/slurm-r1-rusty.sh"
# print(slurm_file_out_path)
slurm_file = slurm_file.replace("WhereSlurm", r1_group_path)
slurm_file = slurm_file.replace("WhatModel", MODEL)
slurm_file = slurm_file.replace(
"WhereParticle",
os.path.join(self.particle_path, GROUP["particle_file"]),
)
slurm_file = slurm_file.replace("WhatGroup", GROUP["group"])
with open(slurm_file_out_path, "w+") as outfile:
outfile.write(slurm_file)
outfile.close()
file.close()
print("PREPPING CONSENSUS DONE!")
def RUN(self):
GROUPS = pd.read_csv(
self.group_list,
names=["particle_file", "group", "start", "end", "nframe"],
delim_whitespace="True",
comment="#",dtype=str
)
print(GROUPS)
if args.command_line_mode==False:
consensus_MODEL_name = input("Please provide the CONSENSUS MODEL NAME:\n")
else:
consensus_MODEL_name = args.consensus_model
consensus_MODEL_path = os.path.join(self.output_path,consensus_MODEL_name)
round1_path = os.path.join(consensus_MODEL_path, "round1")
print("\n========== CONSENSUS PATH: %s" % (consensus_MODEL_path))
for ind, GROUP in GROUPS.iterrows():
current_dir = os.getcwd()
r1_group_path = os.path.join(round1_path, GROUP["group"])
print(current_dir)
os.chdir(r1_group_path)
slurm_file_out_path = "slurm-r1-rusty.sh"
os.chmod(slurm_file_out_path, stat.S_IRWXU)
# print(slurm_file_out_path)
sbatch_cmd = "sbatch " + slurm_file_out_path
subprocess.run(str(sbatch_cmd), shell=True, check=True)
os.chdir(current_dir)
class CONSENSUS_MODE_ROUND_2:
def __init__(
self, model_path, model_list, group_list, param_path, particle_path, output_path
):
self.model_path = model_path
self.model_list = model_list
self.group_list = group_list
self.param_path = param_path
self.particle_path = particle_path
self.output_path = output_path
def PREP_NONCONSENSUS(self):
global cluster_choice, partition_choice
MODELS_LIST = open(self.model_list)
MODELS = MODELS_LIST.readlines()
GROUPS = pd.read_csv(
self.group_list,
names=["particle_file", "group", "start", "end", "nframe"],
delim_whitespace="True",
comment="#",dtype=str
)
#This is here because we did consensus in round 1 but round 2 still looks at all the models we're interested in.
consensus_MODEL_name = input(
"\n========== Please provide the CONSENSUS MODEL NAME:\n"
)
consensus_MODEL_path = os.path.join(self.output_path, consensus_MODEL_name)
for MODEL in MODELS:
MODEL = MODEL.strip()
if MODEL[0] == "#":
print("\n========== Models to be skip:")
print("%s is skipped." % (MODEL[1:]))
continue
elif MODEL == consensus_MODEL_name:
continue
# print(consensus_MODEL_path)
a_model_path = os.path.join(op_v, MODEL)
os.makedirs(a_model_path, exist_ok="True")
os.makedirs(os.path.join(a_model_path, "round2"), exist_ok="True")
round1_path = os.path.join(a_model_path, "round1")
for ind, GROUP in GROUPS.iterrows():
consensus_round1_path = os.path.join(consensus_MODEL_path, "round1")
consensus_round2_path = os.path.join(consensus_MODEL_path, "round2")
consensus_round1_group_path = os.path.join(
consensus_round1_path, GROUP["group"]
)
consensus_round2_group_path = os.path.join(
consensus_round2_path, GROUP["group"]
)
# print(consensus_round2_group_path)
round2_path = os.path.join(a_model_path, "round2")
r2_group_path = os.path.join(round2_path, GROUP["group"])
os.makedirs(r2_group_path, exist_ok="True")
subdir_list = ["/tasks", "/outputs", "/tmp_files"]
for sub_dir in range(len(subdir_list)):
os.makedirs(r2_group_path + subdir_list[sub_dir], exist_ok="True")
group_param_path = os.path.join(
r2_group_path + subdir_list[sub_dir]
)
if os.path.basename(group_param_path) == "tasks":
# if os.path.basename(group_param_path)=="tasks": # FOR TESTING
launch_one_template_path = os.path.join(
self.param_path, "launch-one-NONCONSENSUS-template.sh"
)
shutil.copy(launch_one_template_path, group_param_path)
launch_one_path = os.path.join(
group_param_path, "launch-one-NONCONSENSUS-template.sh"
)
# print(launch_one_path)
with open(launch_one_path, "r+") as launchIn:
with open(
group_param_path + "/launch-one.sh", "w+"
) as launchOut:
lines = launchIn.readlines()
for line in lines:
line = line.split()
# print(line)
if len(line) >= 2:
if line[1] == "SLURM_JOB_NAME=WhatModel-R2":
line[1] = "SLURM_JOB_NAME=%s-R2" % (MODEL)
elif line[1] == "WhereRound2CM=WhereRound2CM":
# print(consensus_round2_group_path)
line[1] = "WhereRound2CM=%s" % (
consensus_round2_path
)
elif line[1] == "WhereParticles=WhereParticles":
line[1] = "WhereParticles=%s" % (
self.particle_path
)
elif line[1] == "WhereModel=WhereModel":
line[1] = "WhereModel=%s" % (
os.path.abspath(self.model_path)
)
elif line[1] == "WhereOutput=WhereOutput":
line[1] = "WhereOutput=%s" % (round2_path)
elif line[1] == "WhereRound1_CONSENSUS_Results=WhereRound1_CONSENSUS_Results":
# this is a place holder, beacuse we need to run the make ori script on consensus round 2 to produce the finer grid.
# really we should figure out how to make this a variable.
consensus_round2_orientations_processed =(os.path.abspath(os.path.join(consensus_round1_group_path,'../../round2/')))
consensus_round2_orientations_processed = consensus_round2_orientations_processed + '/$2'
line[1] = "WhereRound1_CONSENSUS_Results=%s" % (consensus_round2_orientations_processed)
# print(round2_path)
# print(*line)
string = " ".join(map(str, line))
launchOut.write(string + "\n")
launchOut.close()
launchIn.close()
os.chmod(group_param_path + "/launch-one.sh", stat.S_IRWXU)
# os.remove(launch_one_path)
task_path = os.path.join(
group_param_path, "task_%s_%s" % (MODEL, GROUP["group"])
)
particle_count = int(GROUP["nframe"])
with open(task_path, "w+") as task:
for i in range(particle_count):
# print(i)
launch_one_command = (
"./launch-one.sh %s %s %s &>> out.log"
% (i, GROUP["group"], MODEL)
)
task.write(launch_one_command + "\n")
task.close()
print(
"\n========== Done with TASK FILES for NON-CONSENSUS %s"
% (MODEL)
)
print("\n========== CONSENSUS PATH: %s" % (consensus_MODEL_path))
def PREP_CONSENSUS(self):
global cluster_choice, partition_choice
GROUPS = pd.read_csv(
self.group_list,
names=["particle_file", "group", "start", "end", "nframe"],
delim_whitespace="True",
comment="#",dtype=str
)
if args.command_line_mode == False:
consensus_MODEL_name = input(
"\n========== Please provide the CONSENSUS MODEL NAME:\n"
)
else:
consensus_MODEL_name = args.consensus_model
consensus_MODEL_path = os.path.join(self.output_path, consensus_MODEL_name)
os.makedirs(consensus_MODEL_path, exist_ok="True")
os.makedirs(os.path.join(consensus_MODEL_path, "round2"), exist_ok="True")
consensus_round1_path = os.path.join(consensus_MODEL_path, "round1")
consensus_round2_path = os.path.join(consensus_MODEL_path, "round2")
for ind, GROUP in GROUPS.iterrows():
consensus_round1_group_path = os.path.join(
consensus_round1_path, GROUP["group"]
)
consensus_round2_group_path = os.path.join(
consensus_round2_path, GROUP["group"]
)
# print(consensus_round2_group_path)
os.makedirs(consensus_round2_group_path, exist_ok="True")
subdir_list = [
"/parameters",
"/orientations",
"/tasks",
"/outputs",
"/tmp_files",
]
for sub_dir in range(len(subdir_list)):
os.makedirs(
consensus_round2_group_path + subdir_list[sub_dir], exist_ok="True"
)
group_param_path = os.path.join(
consensus_round2_group_path + subdir_list[sub_dir]
)
if os.path.basename(group_param_path) == "tmp_files":
# self.param_path+"Param_BioEM_template"
param_bio_R1_path = os.path.join(
consensus_round1_path, "Param_BioEM_ABC"
)
shutil.copy(param_bio_R1_path, group_param_path)
output_prob_from_R1 = os.path.join(
consensus_round1_group_path, "Output_Probabilities"
)
Out_copied_Prob_R1_path = os.path.join(
group_param_path, "Output_Probabilities-R1"
)
shutil.copy(output_prob_from_R1, Out_copied_Prob_R1_path)
clean_R1_Probability(
consensus_round2_group_path,
Out_copied_Prob_R1_path,
param_bio_R1_path,
)
print(
"\n========== Done with PARAMETER FILES for %s"
% (consensus_MODEL_name)
)
shutil.copy(
consensus_round1_group_path + "/angle_output_probabilities.txt",
group_param_path + "/PROB_ANGLE_R1.txt",
)
r1_prob = group_param_path + "/PROB_ANGLE_R1.txt"
a_model_path = os.path.join(op_v, consensus_MODEL_name)
r2_group_path = a_model_path + '/round2'
print(consensus_round2_group_path)
making_orientations_submission (libraryPath = param_v, r1_prob = r1_prob, model_now = a_model_path , group_now = consensus_round2_group_path, workdir_round2 = r2_group_path )
print(
"\n========== Done with ORIENTATION FILES for %s"
% (consensus_MODEL_name)
)
elif os.path.basename(group_param_path) == "tasks":
# if os.path.basename(group_param_path)=="tasks": # FOR TESTING
launch_one_template_path = os.path.join(
self.param_path, "launch-one-template.sh"
)
shutil.copy(launch_one_template_path, group_param_path)
launch_one_path = os.path.join(
group_param_path, "launch-one-template.sh"
)
# print(launch_one_path)
with open(launch_one_path, "r+") as launchIn:
with open(
group_param_path + "/launch-one.sh", "w+"
) as launchOut:
lines = launchIn.readlines()
for line in lines:
line = line.split()
# print(line)
if len(line) >= 2:
if line[1] == "SLURM_JOB_NAME=WhatModel-R2":
line[1] = "SLURM_JOB_NAME=%s-R2" % (
consensus_MODEL_name
)
elif line[1] == "WhereRound2=WhereRound2":
# print(consensus_round2_group_path)
line[1] = "WhereRound2=%s" % (
consensus_round2_path
)
elif line[1] == "WhereParticles=WhereParticles":
line[1] = "WhereParticles=%s" % (
self.particle_path
)
elif line[1] == "WhereModel=WhereModel":
line[1] = "WhereModel=%s" % (
os.path.abspath(self.model_path)
)
elif line[1] == "WhereRound1_CONENSUS_Results=WhereRound1_CONENSUS_Results":
print(os.path.abspath(group_param_path))
line[1] = "WhereRound1_CONENSUS_Results=%s" % (
os.path.abspath(group_param_path)
)
# print(*line)
string = " ".join(map(str, line))
launchOut.write(string + "\n")
launchOut.close()
launchIn.close()
os.chmod(group_param_path + "/launch-one.sh", stat.S_IRWXU)
# os.remove(launch_one_path)
task_path = os.path.join(
group_param_path,
"task_%s_%s" % (consensus_MODEL_name, GROUP["group"]),
)
particle_count = int(GROUP["nframe"])
with open(task_path, "w+") as task:
for i in range(particle_count):
# print(i)
launch_one_command = (
"./launch-one.sh %s %s %s &>> out.log"
% (i, GROUP["group"], consensus_MODEL_name)
)
task.write(launch_one_command + "\n")
task.close()
print(
"\n========== Done with TASK FILES for CONSENSUS MODEL: %s"
% (consensus_MODEL_name)
)
# print("\n========== CONSENSUS PATH: %s"%(consensus_MODEL_path))
def RUN_NONCONSENSUS(self):
try:
subprocess.check_output(
["disBatch", "--help"], stderr=subprocess.STDOUT
).decode("utf8")
print("========== disBatch is LOADED. SUBMIT JOBS NOW!")
except:
print(
"\nYou need to load disBatch to launch ROUND 2. PROGRAM TERMINATED!!!\n"
)
exit
consensus_MODEL_name = input("\nPlease provide the CONSENSUS MODEL NAME:\n")
consensus_MODEL_path = os.path.join(self.output_path, consensus_MODEL_name)
MODELS_LIST = open(self.model_list)
MODELS = MODELS_LIST.readlines()
GROUPS = pd.read_csv(
self.group_list,
names=["particle_file", "group", "start", "end", "nframe"],
delim_whitespace="True",
comment="#",dtype=str
)
for MODEL in MODELS:
MODEL = MODEL.strip()
if MODEL[0] == "#":
# print("%s is skipped."%(MODEL[1:]))
continue
elif MODEL == consensus_MODEL_name:
continue
a_model_path = os.path.join(op_v, MODEL)
round2_path = os.path.join(a_model_path, "round2")
GROUP = None
for ind, GROUP in GROUPS.iterrows():
r2_group_path = os.path.join(round2_path, GROUP["group"])
task_path = os.path.join(r2_group_path, "tasks")
task_file_name = "task_%s_%s" % (MODEL, GROUP["group"])
current_directory = os.getcwd()
os.chdir(task_path)
sbatch_cmd = "sbatch -p ccb -J %s -t 128 disBatch %s" % (
MODEL,
task_file_name,
)
# print(sbatch_cmd)
subprocess.run(sbatch_cmd, shell=True)
os.chdir(current_directory)
def RUN_CONSENSUS(self):
try:
subprocess.check_output(
["disBatch", "--help"], stderr=subprocess.STDOUT
).decode("utf8")
print("========== disBatch is LOADED. SUBMIT JOBS NOW!")
except:
print(
"\nYou need to load disBatch to launch ROUND 2. PROGRAM TERMINATED!!!\n"
)
exit
if args.command_line_mode == False:
consensus_MODEL_name = input("\nPlease provide the CONSENSUS MODEL NAME:\n")
elif args.consensus_model is not None:
consensus_MODEL_name = args.consensus_model
else:
raise Exception ("It looks like you haven't specified a consensus model file but you've asked to run in consensus mode. ")
consensus_MODEL_path = os.path.join(self.output_path,consensus_MODEL_name)
GROUPS = pd.read_csv(
self.group_list,
names=["particle_file", "group", "start", "end", "nframe"],
delim_whitespace="True",
comment="#",dtype=str
)
round2_path = os.path.join(consensus_MODEL_path,"round2")
GROUP=None
for ind, GROUP in GROUPS.iterrows():
r2_group_path = os.path.join(round2_path, GROUP["group"])
task_path = os.path.join(r2_group_path, "tasks")
task_file_name = "task_%s_%s" % (consensus_MODEL_name, GROUP["group"])
os.chdir(task_path)
sbatch_cmd = "sbatch -p ccb -J %s -t 128 disBatch %s" % (
consensus_MODEL_name,
task_file_name,
)
# print(sbatch_cmd)
subprocess.run(sbatch_cmd, shell=True)
def CLEAN_OUTPUT_NONCONSENSUS(self):
consensus_MODEL_name = input("\nPlease provide the CONSENSUS MODEL NAME:\n")
consensus_MODEL_path = os.path.join(self.output_path, consensus_MODEL_name)
MODELS_LIST = open(self.model_list)
MODELS = MODELS_LIST.readlines()
GROUPS = pd.read_csv(
self.group_list,
names=["particle_file", "group", "start", "end", "nframe"],
delim_whitespace="True",
comment="#",dtype=str
)
for MODEL in MODELS:
MODEL = MODEL.strip()
if MODEL[0] == "#":
# print("%s is skipped."%(MODEL[1:]))
continue
elif MODEL == consensus_MODEL_name:
continue
a_model_path = os.path.join(op_v, MODEL)
round2_path = os.path.join(a_model_path, "round2")
GROUP = None
print("========== Now cleaning %s" % (MODEL))
for ind, GROUP in GROUPS.iterrows():
r2_group_path = os.path.join(round2_path, GROUP["group"])
path_to_your_mess = os.path.join(r2_group_path, "outputs")
process_output_round2(
MODEL, GROUP["group"], path_to_your_mess, int(GROUP["nframe"])
)
def CLEAN_OUTPUT_CONSENSUS(self):
consensus_MODEL_name = input("\nPlease provide the CONSENSUS MODEL NAME:\n")
consensus_MODEL_path = os.path.join(self.output_path, consensus_MODEL_name)
GROUPS = pd.read_csv(
self.group_list,
names=["particle_file", "group", "start", "end", "nframe"],
delim_whitespace="True",
comment="#",dtype=str
)
round2_path = os.path.join(consensus_MODEL_path, "round2")
GROUP = None
print("========== Now cleaning CONSENSUS MODEL %s" % (consensus_MODEL_name))
for ind, GROUP in GROUPS.iterrows():
r2_group_path = os.path.join(round2_path, GROUP["group"])
path_to_your_mess = os.path.join(r2_group_path, "outputs")
process_output_round2(
consensus_MODEL_name, GROUP["group"], path_to_your_mess, int(GROUP["nframe"])
)
def CLEAN_PARAMS(self):
delete_choice = input(
"Do you want to keep the original files? Choose (0) NO or (1) YES\n"
)
MODELS_LIST = open(self.model_list)
MODELS = MODELS_LIST.readlines()
GROUPS = pd.read_csv(
self.group_list,
names=["particle_file", "group", "start", "end", "nframe"],
delim_whitespace="True",
comment="#",dtype=str
)