|
| 1 | +""" |
| 2 | +Script that builds a single dataset. |
| 3 | +""" |
| 4 | + |
| 5 | +import os |
| 6 | +import argparse |
| 7 | +import subprocess |
| 8 | +import shutil |
| 9 | +import gzip |
| 10 | +from concurrent.futures import ThreadPoolExecutor |
| 11 | +import glob |
| 12 | + |
| 13 | +def run_docker_cmd(cmd_arr, filename): |
| 14 | + ''' |
| 15 | + Wrapper for 'docker run' command. Executes a Docker container with the specified command. |
| 16 | + ''' |
| 17 | + print('Running...', filename) |
| 18 | + env = os.environ.copy() |
| 19 | + if 'SYNAPSE_AUTH_TOKEN' not in env: |
| 20 | + print('You need to set the SYNAPSE_AUTH_TOKEN to access the MPNST and beatAML datasets') |
| 21 | + docker_run = ['docker', 'run', '-v', f"{env['PWD']}/local/:/tmp/", '--platform=linux/amd64'] |
| 22 | + else: |
| 23 | + docker_run = ['docker', 'run', '-v', f"{env['PWD']}/local/:/tmp/", '-e', f"SYNAPSE_AUTH_TOKEN={env['SYNAPSE_AUTH_TOKEN']}", '--platform=linux/amd64'] |
| 24 | + |
| 25 | + cmd = docker_run + cmd_arr |
| 26 | + print('Executing command:', ' '.join(cmd)) |
| 27 | + res = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 28 | + if res.returncode != 0: |
| 29 | + print(res.stderr.decode()) |
| 30 | + exit(f'{filename} failed') |
| 31 | + else: |
| 32 | + print(f'{filename} completed successfully') |
| 33 | + |
| 34 | +def process_docker(dataset,validate): |
| 35 | + ''' |
| 36 | + Build Docker images required for the specified dataset. |
| 37 | + ''' |
| 38 | + compose_file = 'build/docker/docker-compose.yml' |
| 39 | + dataset_map = { |
| 40 | + 'broad_sanger': ['broad_sanger_exp', 'broad_sanger_omics'], |
| 41 | + 'hcmi': ['hcmi'], |
| 42 | + 'beataml': ['beataml'], |
| 43 | + 'mpnst': ['mpnst'], |
| 44 | + 'mpnstpdx': ['mpnstpdx'], |
| 45 | + 'cptac': ['cptac'], |
| 46 | + 'genes': ['genes'], |
| 47 | + 'upload': ['upload'] |
| 48 | + } |
| 49 | + |
| 50 | + # Collect container names to build based on the dataset provided. Always build 'genes'. |
| 51 | + datasets_to_build = ['genes'] |
| 52 | + # Append upload if validation step is included |
| 53 | + if validate is True: |
| 54 | + datasets_to_build.append('upload') |
| 55 | + |
| 56 | + datasets_to_build.extend(dataset_map.get(dataset, [])) |
| 57 | + |
| 58 | + compose_command = ['docker-compose', '-f', compose_file, 'build'] + datasets_to_build |
| 59 | + |
| 60 | + log_file_path = 'local/docker.log' |
| 61 | + env = os.environ.copy() |
| 62 | + |
| 63 | + print(f"Docker-compose is building images for {', '.join(datasets_to_build)}. View output in {log_file_path}.") |
| 64 | + |
| 65 | + with open(log_file_path, 'w') as log_file: |
| 66 | + try: |
| 67 | + subprocess.run(compose_command, env=env, stdout=log_file, stderr=log_file, text=True, check=True) |
| 68 | + log_file.write("Docker images built successfully.\n") |
| 69 | + print(f"Docker images for {', '.join(datasets_to_build)} built successfully. Details logged in {log_file_path}.") |
| 70 | + except subprocess.CalledProcessError as e: |
| 71 | + log_file.write(f"Docker compose build failed with error: {e}\n") |
| 72 | + print(f"Docker compose build failed. See {log_file_path} for details.") |
| 73 | + raise |
| 74 | + |
| 75 | +def process_genes(executor): |
| 76 | + ''' |
| 77 | + Build the genes file if it does not exist. |
| 78 | + ''' |
| 79 | + if not os.path.exists('local/genes.csv'): |
| 80 | + executor.submit(run_docker_cmd, ['genes', 'bash', 'build_genes.sh'], 'genes file') |
| 81 | + |
| 82 | +def process_samples(executor, dataset, use_prev_dataset, should_continue): |
| 83 | + ''' |
| 84 | + Build the samples file for the specified dataset. |
| 85 | + ''' |
| 86 | + samples_file = f'local/{dataset}_samples.csv' |
| 87 | + if should_continue and os.path.exists(samples_file): |
| 88 | + print(f"Samples file for {dataset} already exists. Skipping samples build.") |
| 89 | + return |
| 90 | + |
| 91 | + prev_samples_file = f'/tmp/{use_prev_dataset}_samples.csv' if use_prev_dataset else '' |
| 92 | + di = 'broad_sanger_omics' if dataset == 'broad_sanger' else dataset |
| 93 | + filename = f'{dataset} samples' |
| 94 | + executor.submit(run_docker_cmd, [di, 'bash', 'build_samples.sh', prev_samples_file], filename) |
| 95 | + |
| 96 | +def process_drugs(executor, dataset, use_prev_dataset, should_continue): |
| 97 | + ''' |
| 98 | + Build the drugs file for the specified dataset. |
| 99 | + ''' |
| 100 | + if dataset in ['cptac', 'hcmi']: |
| 101 | + return # No drugs to process for these datasets |
| 102 | + |
| 103 | + drugs_file = f'local/{dataset}_drugs.tsv' |
| 104 | + if should_continue and os.path.exists(drugs_file): |
| 105 | + print(f"Drugs file for {dataset} already exists. Skipping drugs build.") |
| 106 | + return |
| 107 | + |
| 108 | + prev_drugs_file = f'/tmp/{use_prev_dataset}_drugs.tsv' if use_prev_dataset else '' |
| 109 | + dflist = [prev_drugs_file] if use_prev_dataset else [] |
| 110 | + di = 'broad_sanger_exp' if dataset == 'broad_sanger' else dataset |
| 111 | + filename = f'{dataset} drugs' |
| 112 | + executor.submit(run_docker_cmd, [di, 'bash', 'build_drugs.sh', ','.join(dflist)], filename) |
| 113 | + |
| 114 | + |
| 115 | +def process_omics(executor, dataset, should_continue): |
| 116 | + ''' |
| 117 | + Build the omics files for the specified dataset. |
| 118 | + ''' |
| 119 | + # Map datasets to their expected omics files |
| 120 | + dataset_omics_files = { |
| 121 | + 'beataml': ['mutations', 'proteomics', 'transcriptomics'], |
| 122 | + 'mpnst': ['copy_number', 'mutations', 'proteomics', 'transcriptomics'], |
| 123 | + 'broad_sanger': ['copy_number', 'mutations', 'proteomics', 'transcriptomics'], |
| 124 | + 'cptac': ['copy_number', 'mutations', 'proteomics', 'transcriptomics'], |
| 125 | + 'hcmi': ['mutations', 'transcriptomics'], |
| 126 | + 'mpnstpdx':['copy_number', 'mutations', 'proteomics', 'transcriptomics'] |
| 127 | + } |
| 128 | + |
| 129 | + expected_omics = dataset_omics_files.get(dataset, []) |
| 130 | + |
| 131 | + if not expected_omics: |
| 132 | + print(f"No omics data expected for dataset {dataset}. Skipping omics build.") |
| 133 | + return |
| 134 | + |
| 135 | + # Check if all expected omics files exist |
| 136 | + omics_files_exist = True |
| 137 | + for omics_type in expected_omics: |
| 138 | + patterns = [ |
| 139 | + f'local/{dataset}_{omics_type}.csv', |
| 140 | + f'local/{dataset}_{omics_type}.csv.gz', |
| 141 | + f'local/{dataset}_{omics_type}.tsv', |
| 142 | + f'local/{dataset}_{omics_type}.tsv.gz' |
| 143 | + ] |
| 144 | + file_found = False |
| 145 | + for pattern in patterns: |
| 146 | + matches = glob.glob(pattern) |
| 147 | + if matches: |
| 148 | + file_found = True |
| 149 | + break |
| 150 | + if not file_found: |
| 151 | + omics_files_exist = False |
| 152 | + break # If any omics files are missing, just build / rebuild them all. |
| 153 | + |
| 154 | + if should_continue and omics_files_exist: |
| 155 | + print(f"Omics files for {dataset} already exist. Skipping omics build.") |
| 156 | + return |
| 157 | + |
| 158 | + di = 'broad_sanger_omics' if dataset == 'broad_sanger' else dataset |
| 159 | + filename = f'{dataset} omics' |
| 160 | + executor.submit(run_docker_cmd, [di, 'bash', 'build_omics.sh', '/tmp/genes.csv', f'/tmp/{dataset}_samples.csv'], filename) |
| 161 | + |
| 162 | + |
| 163 | +def process_experiments(executor, dataset, should_continue): |
| 164 | + ''' |
| 165 | + Build the experiments files for the specified dataset. |
| 166 | + ''' |
| 167 | + if dataset in ['cptac', 'hcmi']: |
| 168 | + return # No experiments to process for these datasets |
| 169 | + |
| 170 | + experiments_file = f'local/{dataset}_experiments.tsv' |
| 171 | + if should_continue and os.path.exists(experiments_file): |
| 172 | + print(f"Experiments file for {dataset} already exists. Skipping experiments build.") |
| 173 | + return |
| 174 | + |
| 175 | + di = 'broad_sanger_exp' if dataset == 'broad_sanger' else dataset |
| 176 | + filename = f'{dataset} experiments' |
| 177 | + executor.submit(run_docker_cmd, [di, 'bash', 'build_exp.sh', f'/tmp/{dataset}_samples.csv', f'/tmp/{dataset}_drugs.tsv'], filename) |
| 178 | + |
| 179 | + |
| 180 | + |
| 181 | +def process_misc(executor, datasets): |
| 182 | + ''' |
| 183 | + Run all misc scripts concurrently or one at a time. |
| 184 | + ''' |
| 185 | + last_misc_future = None |
| 186 | + #Currently this only applies to broad_sanger. Add others here if they need a final step. |
| 187 | + if "broad_sanger" in datasets: |
| 188 | + datasets = ["broad_sanger"] |
| 189 | + else: |
| 190 | + return |
| 191 | + for da in datasets: |
| 192 | + di = 'broad_sanger_omics' if da == 'broad_sanger' else da |
| 193 | + #Run all at once: |
| 194 | + if last_misc_future: |
| 195 | + last_misc_future.result() |
| 196 | + last_misc_future = executor.submit(run_docker_cmd, [di, 'bash', 'build_misc.sh'], f'{da} misc') |
| 197 | + |
| 198 | + |
| 199 | + |
| 200 | +def decompress_file(file_path): |
| 201 | + """Decompress a gzip file and delete the original compressed file.""" |
| 202 | + with gzip.open(file_path, 'rb') as f_in: |
| 203 | + decompressed_file_path = file_path[:-3] # Remove '.gz' from the filename |
| 204 | + with open(decompressed_file_path, 'wb') as f_out: |
| 205 | + shutil.copyfileobj(f_in, f_out) |
| 206 | + os.remove(file_path) |
| 207 | + |
| 208 | +def compress_file(file_path): |
| 209 | + """Compress a file using gzip and delete the original uncompressed file.""" |
| 210 | + compressed_file_path = file_path + '.gz' |
| 211 | + with open(file_path, 'rb') as f_in: |
| 212 | + with gzip.open(compressed_file_path, 'wb') as f_out: |
| 213 | + shutil.copyfileobj(f_in, f_out) |
| 214 | + os.remove(file_path) |
| 215 | + |
| 216 | +def run_docker_validate_cmd(cmd_arr, all_files_dir, name): |
| 217 | + ''' |
| 218 | + Wrapper for 'docker run' command used during validation and uploads. |
| 219 | + ''' |
| 220 | + env = os.environ.copy() |
| 221 | + docker_run = ['docker', 'run', '-v', f"{env['PWD']}/local/{all_files_dir}:/tmp"] |
| 222 | + docker_run.extend(['upload']) |
| 223 | + docker_run.extend(cmd_arr) |
| 224 | + print('Executing:', ' '.join(docker_run)) |
| 225 | + res = subprocess.run(docker_run, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 226 | + if res.returncode != 0: |
| 227 | + print(res.stderr.decode()) |
| 228 | + exit(f'{name} failed') |
| 229 | + else: |
| 230 | + print(f'{name} completed successfully') |
| 231 | + |
| 232 | +def run_schema_checker(dataset): |
| 233 | + ''' |
| 234 | + Run schema checker on the built files for the specified dataset. |
| 235 | + ''' |
| 236 | + # Prepare the directory with the built files |
| 237 | + prefixes = ['genes', dataset] |
| 238 | + datasets = [dataset] |
| 239 | + broad_sanger_datasets = ["ccle","ctrpv2","fimm","gdscv1","gdscv2","gcsi","prism","nci60"] |
| 240 | + all_files_dir = 'all_files_dir' |
| 241 | + if "broad_sanger" == dataset: |
| 242 | + prefixes.extend(broad_sanger_datasets) |
| 243 | + datasets.extend(broad_sanger_datasets) |
| 244 | + datasets.remove("broad_sanger") |
| 245 | + prefixes.remove("broad_sanger") |
| 246 | + |
| 247 | + if not os.path.exists(f'local/{all_files_dir}'): |
| 248 | + os.makedirs(f'local/{all_files_dir}') |
| 249 | + |
| 250 | + # Move relevant files to all_files_dir |
| 251 | + for file in os.listdir('local'): |
| 252 | + if any(file.startswith(prefix) for prefix in prefixes): |
| 253 | + shutil.move(os.path.join('local', file), os.path.join('local', all_files_dir, file)) |
| 254 | + |
| 255 | + # Decompress any compressed files |
| 256 | + for file in os.listdir(f'local/{all_files_dir}'): |
| 257 | + if file.endswith('.gz'): |
| 258 | + decompress_file(os.path.join('local', all_files_dir, file)) |
| 259 | + |
| 260 | + # Run schema checker |
| 261 | + schema_check_command = ['python3', 'scripts/check_schema.py', '--datasets'] + datasets |
| 262 | + run_docker_validate_cmd(schema_check_command, all_files_dir, 'Validation') |
| 263 | + |
| 264 | +def main(): |
| 265 | + parser = argparse.ArgumentParser( |
| 266 | + description="This script builds a single dataset." |
| 267 | + ) |
| 268 | + parser.add_argument('--dataset', required=True, help='Name of the dataset to build') |
| 269 | + parser.add_argument('--use_prev_dataset', help='Prefix of the previous dataset for sample and drug ID assignment') |
| 270 | + parser.add_argument('--build', action='store_true', help='Run data build.') |
| 271 | + parser.add_argument('--validate', action='store_true', help='Run schema checker on the built files') |
| 272 | + parser.add_argument('--continue', dest='should_continue', action='store_true', help='Continue from where the build left off by skipping existing files') |
| 273 | + |
| 274 | + args = parser.parse_args() |
| 275 | + |
| 276 | + if not os.path.exists('local'): |
| 277 | + os.mkdir('local') |
| 278 | + |
| 279 | + # Build Docker Image |
| 280 | + process_docker(args.dataset,args.validate) |
| 281 | + |
| 282 | + if args.build: |
| 283 | + # Use ThreadPoolExecutor for parallel execution |
| 284 | + with ThreadPoolExecutor() as executor: |
| 285 | + # Always build genes file |
| 286 | + process_genes(executor) |
| 287 | + |
| 288 | + # Build samples and drugs |
| 289 | + samples_future = executor.submit(process_samples, executor, args.dataset, args.use_prev_dataset, args.should_continue) |
| 290 | + drugs_future = executor.submit(process_drugs, executor, args.dataset, args.use_prev_dataset, args.should_continue) |
| 291 | + |
| 292 | + samples_future.result() |
| 293 | + drugs_future.result() |
| 294 | + |
| 295 | + print("Samples and Drugs Files Completed.") |
| 296 | + |
| 297 | + with ThreadPoolExecutor() as executor: |
| 298 | + |
| 299 | + # Build omics and experiments |
| 300 | + omics_future = executor.submit(process_omics, executor, args.dataset, args.should_continue) |
| 301 | + experiments_future = executor.submit(process_experiments, executor, args.dataset, args.should_continue) |
| 302 | + |
| 303 | + omics_future.result() |
| 304 | + experiments_future.result() |
| 305 | + |
| 306 | + print("Experiments and Omics Files completed.") |
| 307 | + |
| 308 | + with ThreadPoolExecutor() as executor: |
| 309 | + |
| 310 | + if args.build: |
| 311 | + misc_thread = executor.submit(process_misc, executor, args.dataset) |
| 312 | + if args.build: |
| 313 | + misc_thread.result() |
| 314 | + print("Final build step complete.") |
| 315 | + |
| 316 | + if args.validate: |
| 317 | + run_schema_checker(args.dataset) |
| 318 | + print("Validation completed.") |
| 319 | + |
| 320 | +if __name__ == '__main__': |
| 321 | + main() |
0 commit comments