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
|
#!/usr/bin/env python
from __future__ import print_function
"""
test_split_and_combine.py
test branching dependencies
"""
import sys
import os
tempdir = os.path.relpath(os.path.abspath(os.path.splitext(__file__)[0])) + "/"
verbose_output = sys.stderr
# add grandparent to search path for testing
grandparent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
sys.path.insert(0, grandparent_dir)
# module name = script name without extension
module_name = os.path.splitext(os.path.basename(__file__))[0]
# funky code to import by file name
parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
from ruffus import posttask, split, merge, transform, pipeline_printout, pipeline_run, Pipeline, suffix
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
# imports
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
import unittest
import shutil
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
# Tasks
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
#
# split_fasta_file
#
@posttask(lambda: verbose_output.write(" Split into %d files\n" % 10))
@split(tempdir + "original.fa", [tempdir + "files.split.success", tempdir + "files.split.*.fa"])
def split_fasta_file (input_file, outputs):
#
# remove previous fasta files
#
success_flag = outputs[0]
output_file_names = outputs[1:]
for f in output_file_names:
os.unlink(f)
#
# create as many files as we are simulating in jobs_per_task
#
for i in range(10):
with open(tempdir + "files.split.%03d.fa" % i, "w") as oo:
pass
with open(success_flag, "w") as oo:
pass
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
#
# align_sequences
#
@posttask(lambda: verbose_output.write(" Sequences aligned\n"))
@transform(split_fasta_file, suffix(".fa"), ".aln") # fa -> aln
def align_sequences (input_file, output_filename):
with open(output_filename, "w") as oo:
oo.write("%s\n" % output_filename)
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
#
# percentage_identity
#
@posttask(lambda: verbose_output.write(" %Identity calculated\n"))
@transform(align_sequences, # find all results from align_sequences
suffix(".aln"), # replace suffix with:
[r".pcid", # .pcid suffix for the result
r".pcid_success"]) # .pcid_success to indicate job completed
def percentage_identity (input_file, output_files):
(output_filename, success_flag_filename) = output_files
with open(output_filename, "w") as oo:
oo.write("%s\n" % output_filename)
with open(success_flag_filename, "w") as oo:
pass
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
#
# combine_results
#
@posttask(lambda: verbose_output.write(" Results recombined\n"))
@merge(percentage_identity, [tempdir + "all.combine_results",
tempdir + "all.combine_results_success"])
def combine_results (input_files, output_files):
"""
Combine all
"""
(output_filename, success_flag_filename) = output_files
with open(output_filename, "w") as out:
for inp, flag in input_files:
with open(inp) as ii:
out.write(ii.read())
with open(success_flag_filename, "w") as oo:
pass
class Test_ruffus(unittest.TestCase):
def setUp(self):
try:
shutil.rmtree(tempdir)
except:
pass
os.makedirs(tempdir)
open(tempdir + "original.fa", "w").close()
def tearDown(self):
try:
shutil.rmtree(tempdir)
pass
except:
pass
def test_ruffus (self):
pipeline_run(multiprocess = 50, verbose = 0, pipeline= "main")
if not os.path.exists(tempdir + "all.combine_results"):
raise Exception("Missing %s" % (tempdir + "all.combine_results"))
def test_newstyle_ruffus (self):
test_pipeline = Pipeline("test")
test_pipeline.split(task_func = split_fasta_file,
input = tempdir + "original.fa",
output = [tempdir + "files.split.success",
tempdir + "files.split.*.fa"])\
.posttask(lambda: verbose_output.write(" Split into %d files\n" % 10))
test_pipeline.transform(task_func = align_sequences,
input = split_fasta_file,
filter = suffix(".fa"),
output = ".aln" # fa -> aln
)\
.posttask(lambda: verbose_output.write(" Sequences aligned\n"))
test_pipeline.transform(task_func = percentage_identity,
input = align_sequences, # find all results from align_sequences
filter = suffix(".aln"), # replace suffix with:
output = [r".pcid", # .pcid suffix for the result
r".pcid_success"] # .pcid_success to indicate job completed
)\
.posttask(lambda: verbose_output.write(" %Identity calculated\n"))
test_pipeline.merge(task_func = combine_results,
input = percentage_identity,
output = [tempdir + "all.combine_results",
tempdir + "all.combine_results_success"])\
.posttask(lambda: verbose_output.write(" Results recombined\n"))
test_pipeline.run(multiprocess = 50, verbose = 0)
if not os.path.exists(tempdir + "all.combine_results"):
raise Exception("Missing %s" % (tempdir + "all.combine_results"))
if __name__ == '__main__':
unittest.main()
|