File: subdivide_collate_code.rst

package info (click to toggle)
python-ruffus 2.6.3%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 20,828 kB
  • ctags: 2,843
  • sloc: python: 15,745; makefile: 180; sh: 14
file content (155 lines) | stat: -rw-r--r-- 7,491 bytes parent folder | download | duplicates (6)
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
.. include:: ../../global.inc
.. include:: manual_chapter_numbers.inc

.. _new_manual.subdivide_collate.code:

#############################################################################################################################################################################################################################################################################################################################
|new_manual.subdivide_collate.chapter_num|: Python Code for :ref:`@subdivide <decorators.subdivide>` tasks to run efficiently and regroup with :ref:`@collate <decorators.collate>`
#############################################################################################################################################################################################################################################################################################################################

.. seealso::

    * :ref:`Manual Table of Contents <new_manual.table_of_contents>`
    * :ref:`@jobs_limit <decorators.jobs_limit>` syntax
    * :ref:`pipeline_run() <pipeline_functions.pipeline_run>` syntax
    * :ref:`drmaa_wrapper.run_job() <drmaa_wrapper.run_job>` syntax
    * Back to |new_manual.subdivide_collate.chapter_num|: :ref:`:ref:`@subdivide tasks to run efficiently and regroup with @collate <new_manual.subdivide_collate>`

*****************************************************************************************************************************
:ref:`@subdivide <decorators.subdivide>` and regroup with :ref:`@collate <decorators.collate>` example
*****************************************************************************************************************************

    .. code-block:: python
        :emphasize-lines: 17

        from ruffus import *
        import os, random, sys

        # Create files a random number of lines
        @originate(["a.start",
                    "b.start",
                    "c.start"])
        def create_test_files(output_file):
            cnt_lines = random.randint(1,3) * 2
            with open(output_file, "w") as oo:
                for ii in range(cnt_lines):
                    oo.write("data item = %d\n" % ii)
                print "        %s has %d lines" % (output_file, cnt_lines)


        #
        #   subdivide the input files into NNN fragment files of 2 lines each
        #
        @subdivide( create_test_files,
                    formatter(),
                    "{path[0]}/{basename[0]}.*.fragment",
                    "{path[0]}/{basename[0]}")
        def subdivide_files(input_file, output_files, output_file_name_stem):
            #
            #   cleanup any previous results
            #
            for oo in output_files:
                os.unlink(oo)
            #
            #   Output files contain two lines each
            #       (new output files every even line)
            #
            cnt_output_files = 0
            for ii, line in enumerate(open(input_file)):
                if ii % 2 == 0:
                    cnt_output_files += 1
                    output_file_name = "%s.%d.fragment" % (output_file_name_stem, cnt_output_files)
                    output_file = open(output_file_name, "w")
                    print "        Subdivide %s -> %s" % (input_file, output_file_name)
                output_file.write(line)


        #
        #   Analyse each fragment independently
        #
        @transform(subdivide_files, suffix(".fragment"), ".analysed")
        def analyse_fragments(input_file, output_file):
            print "        Analysing %s -> %s" % (input_file, output_file)
            with open(output_file, "w") as oo:
                for line in open(input_file):
                    oo.write("analysed " + line)


        #
        #   Group results using original names
        #
        @collate(   analyse_fragments,

                    # split file name into [abc].NUMBER.analysed
                    formatter("/(?P<NAME>[abc]+)\.\d+\.analysed$"),

                    "{path[0]}/{NAME[0]}.final_result")
        def recombine_analyses(input_file_names, output_file):
            with open(output_file, "w") as oo:
                for input_file in input_file_names:
                    print "        Recombine %s -> %s" % (input_file, output_file)
                    for line in open(input_file):
                        oo.write(line)




        #pipeline_printout(sys.stdout, verbose = 3)


        pipeline_run(verbose = 1)

    Results in

    .. code-block:: pycon

        >>> pipeline_run(verbose = 1)

                a.start has 2 lines
            Job  = [None -> a.start] completed
                b.start has 6 lines
            Job  = [None -> b.start] completed
                c.start has 6 lines
            Job  = [None -> c.start] completed
        Completed Task = create_test_files

                Subdivide a.start -> /home/lg/temp/a.1.fragment
            Job  = [a.start -> a.*.fragment, a] completed
                Subdivide b.start -> /home/lg/temp/b.1.fragment
                Subdivide b.start -> /home/lg/temp/b.2.fragment
                Subdivide b.start -> /home/lg/temp/b.3.fragment
            Job  = [b.start -> b.*.fragment, b] completed
                Subdivide c.start -> /home/lg/temp/c.1.fragment
                Subdivide c.start -> /home/lg/temp/c.2.fragment
                Subdivide c.start -> /home/lg/temp/c.3.fragment
            Job  = [c.start -> c.*.fragment, c] completed
        Completed Task = subdivide_files

                Analysing /home/lg/temp/a.1.fragment -> /home/lg/temp/a.1.analysed
            Job  = [a.1.fragment -> a.1.analysed] completed
                Analysing /home/lg/temp/b.1.fragment -> /home/lg/temp/b.1.analysed
            Job  = [b.1.fragment -> b.1.analysed] completed
                Analysing /home/lg/temp/b.2.fragment -> /home/lg/temp/b.2.analysed
            Job  = [b.2.fragment -> b.2.analysed] completed
                Analysing /home/lg/temp/b.3.fragment -> /home/lg/temp/b.3.analysed
            Job  = [b.3.fragment -> b.3.analysed] completed
                Analysing /home/lg/temp/c.1.fragment -> /home/lg/temp/c.1.analysed
            Job  = [c.1.fragment -> c.1.analysed] completed
                Analysing /home/lg/temp/c.2.fragment -> /home/lg/temp/c.2.analysed
            Job  = [c.2.fragment -> c.2.analysed] completed
                Analysing /home/lg/temp/c.3.fragment -> /home/lg/temp/c.3.analysed
            Job  = [c.3.fragment -> c.3.analysed] completed
        Completed Task = analyse_fragments

                Recombine /home/lg/temp/a.1.analysed -> /home/lg/temp/a.final_result
            Job  = [[a.1.analysed] -> a.final_result] completed
                Recombine /home/lg/temp/b.1.analysed -> /home/lg/temp/b.final_result
                Recombine /home/lg/temp/b.2.analysed -> /home/lg/temp/b.final_result
                Recombine /home/lg/temp/b.3.analysed -> /home/lg/temp/b.final_result
            Job  = [[b.1.analysed, b.2.analysed, b.3.analysed] -> b.final_result] completed
                Recombine /home/lg/temp/c.1.analysed -> /home/lg/temp/c.final_result
                Recombine /home/lg/temp/c.2.analysed -> /home/lg/temp/c.final_result
                Recombine /home/lg/temp/c.3.analysed -> /home/lg/temp/c.final_result
            Job  = [[c.1.analysed, c.2.analysed, c.3.analysed] -> c.final_result] completed
        Completed Task = recombine_analyses