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
|
.. include:: ../../global.inc
.. include:: manual_chapter_numbers.inc
.. _new_manual.on_the_fly.code:
############################################################################################################################################################################################################
|new_manual.on_the_fly.chapter_num|: Esoteric: Python Code for Generating parameters on the fly with :ref:`@files<decorators.files_on_the_fly>`
############################################################################################################################################################################################################
.. seealso::
* :ref:`Manual Table of Contents <new_manual.table_of_contents>`
* :ref:`@files on-the-fly syntax in detail <decorators.files_on_the_fly>`
* Back to |new_manual.on_the_fly.chapter_num|: :ref:`Generating parameters on the fly <new_manual.on_the_fly>`
************************************
Introduction
************************************
| This script takes N pairs of input file pairs (with the suffices .gene and .gwas)
| and runs them against M sets of simulation data (with the suffix .simulation)
| A summary per input file pair is then produced
In pseudo-code:
STEP_1:
::
for n_file in NNN_pairs_of_input_files:
for m_file in MMM_simulation_data:
[n_file.gene,
n_file.gwas,
m_file.simulation] -> n_file.m_file.simulation_res
STEP_2:
::
for n_file in NNN_pairs_of_input_files:
n_file.*.simulation_res -> n_file.mean
| n = CNT_GENE_GWAS_FILES
| m = CNT_SIMULATION_FILES
************************************
Code
************************************
::
from ruffus import *
import os
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
# constants
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
working_dir = "temp_NxM"
simulation_data_dir = os.path.join(working_dir, "simulation")
gene_data_dir = os.path.join(working_dir, "gene")
CNT_GENE_GWAS_FILES = 2
CNT_SIMULATION_FILES = 3
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
# imports
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
import os, sys
from itertools import izip
import glob
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
# Functions
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
#_________________________________________________________________________________________
#
# get gene gwas file pairs
#
#_________________________________________________________________________________________
def get_gene_gwas_file_pairs( ):
"""
Helper function to get all *.gene, *.gwas from the direction specified
in --gene_data_dir
Returns
file pairs with both .gene and .gwas extensions,
corresponding roots (no extension) of each file
"""
gene_files = glob.glob(os.path.join(gene_data_dir, "*.gene"))
gwas_files = glob.glob(os.path.join(gene_data_dir, "*.gwas"))
#
common_roots = set(map(lambda x: os.path.splitext(os.path.split(x)[1])[0], gene_files))
common_roots &=set(map(lambda x: os.path.splitext(os.path.split(x)[1])[0], gwas_files))
common_roots = list(common_roots)
#
p = os.path; g_dir = gene_data_dir
file_pairs = [[p.join(g_dir, x + ".gene"), p.join(g_dir, x + ".gwas")] for x in common_roots]
return file_pairs, common_roots
#_________________________________________________________________________________________
#
# get simulation files
#
#_________________________________________________________________________________________
def get_simulation_files( ):
"""
Helper function to get all *.simulation from the direction specified
in --simulation_data_dir
Returns
file with .simulation extensions,
corresponding roots (no extension) of each file
"""
simulation_files = glob.glob(os.path.join(simulation_data_dir, "*.simulation"))
simulation_roots =map(lambda x: os.path.splitext(os.path.split(x)[1])[0], simulation_files)
return simulation_files, simulation_roots
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
# Main logic
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
#_________________________________________________________________________________________
#
# setup_simulation_data
#
#_________________________________________________________________________________________
#
# mkdir: makes sure output directories exist before task
#
@follows(mkdir(gene_data_dir, simulation_data_dir))
def setup_simulation_data ():
"""
create simulation files
"""
for i in range(CNT_GENE_GWAS_FILES):
open(os.path.join(gene_data_dir, "%03d.gene" % i), "w")
open(os.path.join(gene_data_dir, "%03d.gwas" % i), "w")
#
# gene files without corresponding gwas and vice versa
open(os.path.join(gene_data_dir, "orphan1.gene"), "w")
open(os.path.join(gene_data_dir, "orphan2.gwas"), "w")
open(os.path.join(gene_data_dir, "orphan3.gwas"), "w")
#
for i in range(CNT_SIMULATION_FILES):
open(os.path.join(simulation_data_dir, "%03d.simulation" % i), "w")
#_________________________________________________________________________________________
#
# cleanup_simulation_data
#
#_________________________________________________________________________________________
def try_rmdir (d):
if os.path.exists(d):
try:
os.rmdir(d)
except OSError:
sys.stderr.write("Warning:\t%s is not empty and will not be removed.\n" % d)
def cleanup_simulation_data ():
"""
cleanup files
"""
sys.stderr.write("Cleanup working directory and simulation files.\n")
#
# cleanup gene and gwas files
#
for f in glob.glob(os.path.join(gene_data_dir, "*.gene")):
os.unlink(f)
for f in glob.glob(os.path.join(gene_data_dir, "*.gwas")):
os.unlink(f)
try_rmdir(gene_data_dir)
#
# cleanup simulation
#
for f in glob.glob(os.path.join(simulation_data_dir, "*.simulation")):
os.unlink(f)
try_rmdir(simulation_data_dir)
#
# cleanup working_dir
#
for f in glob.glob(os.path.join(working_dir, "simulation_results", "*.simulation_res")):
os.unlink(f)
try_rmdir(os.path.join(working_dir, "simulation_results"))
#
for f in glob.glob(os.path.join(working_dir, "*.mean")):
os.unlink(f)
try_rmdir(working_dir)
#_________________________________________________________________________________________
#
# Step 1:
#
# for n_file in NNN_pairs_of_input_files:
# for m_file in MMM_simulation_data:
#
# [n_file.gene,
# n_file.gwas,
# m_file.simulation] -> working_dir/n_file.m_file.simulation_res
#
#_________________________________________________________________________________________
def generate_simulation_params ():
"""
Custom function to generate
file names for gene/gwas simulation study
"""
simulation_files, simulation_file_roots = get_simulation_files()
gene_gwas_file_pairs, gene_gwas_file_roots = get_gene_gwas_file_pairs()
#
for sim_file, sim_file_root in izip(simulation_files, simulation_file_roots):
for (gene, gwas), gene_file_root in izip(gene_gwas_file_pairs, gene_gwas_file_roots):
#
result_file = "%s.%s.simulation_res" % (gene_file_root, sim_file_root)
result_file_path = os.path.join(working_dir, "simulation_results", result_file)
#
yield [gene, gwas, sim_file], result_file_path, gene_file_root, sim_file_root, result_file
#
# mkdir: makes sure output directories exist before task
#
@follows(mkdir(working_dir, os.path.join(working_dir, "simulation_results")))
@files(generate_simulation_params)
def gwas_simulation(input_files, result_file_path, gene_file_root, sim_file_root, result_file):
"""
Dummy calculation of gene gwas vs simulation data
Normally runs in parallel on a computational cluster
"""
(gene_file,
gwas_file,
simulation_data_file) = input_files
#
simulation_res_file = open(result_file_path, "w")
simulation_res_file.write("%s + %s -> %s\n" % (gene_file_root, sim_file_root, result_file))
#_________________________________________________________________________________________
#
# Step 2:
#
# Statistical summary per gene/gwas file pair
#
# for n_file in NNN_pairs_of_input_files:
# working_dir/simulation_results/n.*.simulation_res
# -> working_dir/n.mean
#
#_________________________________________________________________________________________
@collate(gwas_simulation, regex(r"simulation_results/(\d+).\d+.simulation_res"), r"\1.mean")
@posttask(lambda : sys.stdout.write("\nOK\n"))
def statistical_summary (result_files, summary_file):
"""
Simulate statistical summary
"""
summary_file = open(summary_file, "w")
for f in result_files:
summary_file.write(open(f).read())
pipeline_run([setup_simulation_data], multiprocess = 5, verbose = 2)
pipeline_run([statistical_summary], multiprocess = 5, verbose = 2)
# uncomment to printout flowchar
#
# pipeline_printout(sys.stdout, [statistical_summary], verbose=2)
# graph_printout ("flowchart.jpg", "jpg", [statistical_summary])
#
cleanup_simulation_data ()
************************************
Resulting Output
************************************
::
>>> pipeline_run([setup_simulation_data], multiprocess = 5, verbose = 2)
Make directories [temp_NxM/gene, temp_NxM/simulation] completed
Completed Task = setup_simulation_data_mkdir_1
Job completed
Completed Task = setup_simulation_data
>>> pipeline_run([statistical_summary], multiprocess = 5, verbose = 2)
Make directories [temp_NxM, temp_NxM/simulation_results] completed
Completed Task = gwas_simulation_mkdir_1
Job = [[temp_NxM/gene/001.gene, temp_NxM/gene/001.gwas, temp_NxM/simulation/000.simulation] -> temp_NxM/simulation_results/001.000.simulation_res, 001, 000, 001.000.simulation_res] completed
Job = [[temp_NxM/gene/000.gene, temp_NxM/gene/000.gwas, temp_NxM/simulation/000.simulation] -> temp_NxM/simulation_results/000.000.simulation_res, 000, 000, 000.000.simulation_res] completed
Job = [[temp_NxM/gene/001.gene, temp_NxM/gene/001.gwas, temp_NxM/simulation/001.simulation] -> temp_NxM/simulation_results/001.001.simulation_res, 001, 001, 001.001.simulation_res] completed
Job = [[temp_NxM/gene/000.gene, temp_NxM/gene/000.gwas, temp_NxM/simulation/001.simulation] -> temp_NxM/simulation_results/000.001.simulation_res, 000, 001, 000.001.simulation_res] completed
Job = [[temp_NxM/gene/000.gene, temp_NxM/gene/000.gwas, temp_NxM/simulation/002.simulation] -> temp_NxM/simulation_results/000.002.simulation_res, 000, 002, 000.002.simulation_res] completed
Job = [[temp_NxM/gene/001.gene, temp_NxM/gene/001.gwas, temp_NxM/simulation/002.simulation] -> temp_NxM/simulation_results/001.002.simulation_res, 001, 002, 001.002.simulation_res] completed
Completed Task = gwas_simulation
Job = [[temp_NxM/simulation_results/000.000.simulation_res, temp_NxM/simulation_results/000.001.simulation_res, temp_NxM/simulation_results/000.002.simulation_res] -> temp_NxM/000.mean] completed
Job = [[temp_NxM/simulation_results/001.000.simulation_res, temp_NxM/simulation_results/001.001.simulation_res, temp_NxM/simulation_results/001.002.simulation_res] -> temp_NxM/001.mean] completed
|