File size: 3,265 Bytes
9f29b8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
## Create python scripts to do a batch run for states in parallel 
## This is setup to do a separate run for each sample type found in the metadf file
## then run "python3 run_batch_<date>.py
## it will create a jumble of text output but will run in parellel speeding things up a bit

import os
import importlib
import sys

hxex_path = os.path.join('/home/tuttle/data/HDX-MS/pyHXExpress') ### <- modify as needed using pyhxexpress.py file
sys.path.append(hxex_path)

import pyhxexpress as hxex      ### <- modify as needed, this is set up for a pyhxexpress.py file
import numpy as np, pandas as pd
import config_fimh as config  ### <- modify as needed

config_file_name = "config_fimh" ### <- set this so it updates value in output script
python_name = "python3" # <- set to e.g. python or python3 as appropriate

def hxex_reload():
    importlib.reload(hxex)
    importlib.reload(config)
    hxex.config = config

hxex_reload()

hxex.config.Output_DIR = os.path.join(hxex.config.Data_DIR,'batchrun_'+str(config.date))
if not os.path.exists(hxex.config.Output_DIR): os.makedirs(hxex.config.Output_DIR)
print("saving output to ",hxex.config.Output_DIR)

metadf = hxex.get_metadf()
samples = metadf['sample'].unique()


### Create scripts for runs of each sample type
scripts = {}
for sample in samples:
    run_dir = os.path.join(hxex.config.Output_DIR,str(sample)+"_run")
    if not os.path.exists(run_dir): os.makedirs(run_dir)
    filename = "run_"+str(sample)+"_"+config.date+".py"
    write_file = os.path.join(run_dir,filename)
    scripts[sample] = write_file

    with open(write_file,"w") as p_file:

        script_text = f'''
# run script for {sample}

import os
import importlib
import sys

hxex_path = os.path.join('{hxex_path}')
sys.path.append(hxex_path)

import pyhxexpress as hxex
import numpy as np, pandas as pd
import {config_file_name} as config

def hxex_reload():
    importlib.reload(hxex)
    importlib.reload(config)
    hxex.config = config

hxex_reload()

hxex.config.Output_DIR = os.path.join("{run_dir}")
if not os.path.exists(hxex.config.Output_DIR): os.makedirs(hxex.config.Output_DIR)
print("saving output to ",hxex.config.Output_DIR)

metadf = hxex.get_metadf()

filtered = hxex.filter_df(metadf,samples="{sample}")

hxex.run_hdx_fits(filtered)
        
        '''

        #print(script_text)
        p_file.write(script_text)



### Create the multiprocessing script that will pool each of the sample scripts
## based on https://medium.com/@rohitobrai11/multithreading-in-python-running-2-scripts-in-parallel-8258c4635182

mp_script_text = f'''
import threading
import subprocess

def run_script(script_name):
    subprocess.run(["{python_name}", script_name])

scripts = {scripts}

if __name__ == "__main__":
    script_thread = {{}}
    for k,v in scripts.items():
        script_thread[k] = threading.Thread(target=run_script, args=[v])
        #print("value",v)

    for k,v in scripts.items():
        script_thread[k].start()

    for k,v in scripts.items():
        script_thread[k].join()

    print("Batch scripts have finished executing.")
'''

#print(mp_script_text)
write_file = os.path.join(hxex.config.Output_DIR,'run_batch_'+config.date+'.py')
with open(write_file,"w") as p_file:
    p_file.write(mp_script_text)