forked from junlabucsd/mm3
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtransfer_channels_to_chtc.py
executable file
·157 lines (124 loc) · 5.48 KB
/
transfer_channels_to_chtc.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
#!/usr/bin/env python3
# import modules
import sys
import os
import argparse
import yaml
import inspect
from pprint import pprint
from getpass import getpass
import glob
try:
import cPickle as pickle
except:
import pickle
import paramiko
# user modules
# realpath() will make your script run, even if you symlink it
cmd_folder = os.path.realpath(os.path.abspath(
os.path.split(inspect.getfile(inspect.currentframe()))[0]))
if cmd_folder not in sys.path:
sys.path.insert(0, cmd_folder)
# This makes python look for modules in ./external_lib
cmd_subfolder = os.path.realpath(os.path.abspath(
os.path.join(os.path.split(inspect.getfile(
inspect.currentframe()))[0], "external_lib")))
if cmd_subfolder not in sys.path:
sys.path.insert(0, cmd_subfolder)
# this is the mm3 module with all the useful functions and classes
import mm3_helpers as mm3
# set switches and parameters
parser = argparse.ArgumentParser(prog='python mm3_Compile.py',
description='Identifies and slices out channels into individual TIFF stacks through time.')
parser.add_argument('-f', '--paramfile', type=str,
required=True, help='Yaml file containing parameters.')
parser.add_argument('-s', '--transfer_segmentation', action='store_true',
required=False, help='Add this option at command line to send segmentation files.')
parser.add_argument('-c', '--transfer_all_channels', action='store_true',
required=False, help='Add this option at command line to send all channels, not just phase.')
parser.add_argument('-j', '--transfer_job_file', action='store_true',
required=False, help='Add this option at command line to compile text file containing file names for job submission at chtc.')
namespace = parser.parse_args()
# Load the project parameters file
mm3.information('Loading experiment parameters.')
if namespace.paramfile:
param_file_path = namespace.paramfile
else:
mm3.warning('No param file specified. Using 100X template.')
param_file_path = 'yaml_templates/params_SJ110_100X.yaml'
param_file_path = os.path.join(os.getcwd(), param_file_path)
p = mm3.init_mm3_helpers(param_file_path) # initialized the helper library
# load specs file
specs = mm3.load_specs()
# identify files to be copied to chtc
files_to_transfer = []
if namespace.transfer_job_file:
job_file_name = '{}_files_list.txt'.format(os.path.basename(param_file_path.split('.')[0]))
job_file = open(job_file_name,'w')
spec_file_name = os.path.join(p['ana_dir'], 'specs.yaml')
new_spec_file_name = '{}_specs.yaml'.format(p['experiment_name'])
time_file_name = os.path.join(p['ana_dir'], 'time_table.yaml')
new_time_file_name = '{}_time.yaml'.format(p['experiment_name'])
new_param_file_name = '{}_params.yaml'.format(p['experiment_name'])
for fov_id,peak_ids in specs.items():
for peak_id,val in peak_ids.items():
if val == 1:
if namespace.transfer_all_channels:
base_name = '{}_xy{:0=3}_p{:0=4}_*.tif'.format(
p['experiment_name'],
fov_id,
peak_id
)
match_list = glob.glob(os.path.join(p['chnl_dir'],base_name))
match_list.sort()
else:
base_name = '{}_xy{:0=3}_p{:0=4}_{}.tif'.format(
p['experiment_name'],
fov_id,
peak_id,
p['phase_plane']
)
match_list = glob.glob(os.path.join(p['chnl_dir'],base_name))
if namespace.transfer_segmentation:
base_name = '{}_xy{:0=3}_p{:0=4}_{}.tif'.format(
p['experiment_name'],
fov_id,
peak_id,
'seg_unet'
)
fname = os.path.join(p['seg_dir'],base_name)
match_list.append(fname)
# pprint(match_list)
files_to_transfer.extend(match_list)
match_base_names = [os.path.basename(fname) for fname in match_list]
if namespace.transfer_job_file:
match_base_names.append(new_spec_file_name)
match_base_names.append(new_time_file_name)
match_base_names.append(param_file_path.split('/')[-1])
line_to_write = ','.join(match_base_names)
line_to_write = line_to_write + '\n'
job_file.write(line_to_write)
if namespace.transfer_job_file:
job_file.close()
files_to_transfer.append(job_file_name)
# files_to_transfer.append(param_file_path)
print("You'll be sending {} files total to chtc.".format(len(files_to_transfer)))
# connect to chtc
ssh = paramiko.SSHClient()
ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts")))
username = input("Username: ")
server = input("Hostname: ")
password = getpass("Password for {}@{}: ".format(username,server))
ssh.connect(server, username=username, password=password)
# copy files
sftp = ssh.open_sftp()
for localpath in files_to_transfer:
print(localpath)
remotepath = localpath.split('/')[-1]
sftp.put(localpath, remotepath)
sftp.put(param_file_path, new_param_file_name)
if namespace.transfer_job_file:
sftp.put(spec_file_name, new_spec_file_name)
sftp.put(time_file_name, new_time_file_name)
sftp.close()
ssh.close()