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
|
.. include:: ../global.inc
.. _decorators.combinations:
.. index::
pair: @combinations; Syntax
.. seealso::
* :ref:`@combinations <new_manual.combinations>` in the **Ruffus** Manual
* :ref:`Decorators <decorators>` for more decorators
.. |input| replace:: `input`
.. _input: `decorators.combinations.input`_
.. |tuple_size| replace:: `tuple_size`
.. _tuple_size: `decorators.combinations.tuple_size`_
.. |filter| replace:: `filter`
.. _filter: `decorators.combinations.filter`_
.. |extras| replace:: `extras`
.. _extras: `decorators.combinations.extras`_
.. |output| replace:: `output`
.. _output: `decorators.combinations.output`_
.. |matching_formatter| replace:: `matching_formatter`
.. _matching_formatter: `decorators.combinations.matching_formatter`_
################################################################################################################################################
@combinations( |input|_, |filter|_, |tuple_size|_, |output|_, [|extras|_,...] )
################################################################################################################################################
**Purpose:**
Generates the **combinations**, between all the elements of a set of |input|_ (e.g. **A B C D**),
i.e. r-length tuples of |input|_ elements with no repeated elements (*not* **A A**)
and where order of the tuples is irrelevant (either **A B** or **B A**, not both).
The effect is analogous to the python `itertools <http://docs.python.org/2/library/itertools.html#itertools.combinations>`__
function of the same name:
.. code-block:: pycon
:emphasize-lines: 2
>>> from itertools import combinations
>>> # combinations('ABCD', 3) --> ABC ABD ACD BCD
>>> [ "".join(a) for a in combinations("ABCD", 3)]
['ABC', 'ABD', 'ACD', 'BCD']
Only out of date tasks (comparing |input|_ and |output|_ files) will be run
|output|_ file names and strings in the extra parameters
are generated by string replacement via the :ref:`formatter()<decorators.formatter>` filter
from the |input|_. This can be, for example, a list of file names or the
|output|_ of up stream tasks.
.
The replacement strings require an extra level of nesting to refer to
parsed components.
#. The first level refers to which *set* in each tuple of |input|_.
#. The second level refers to which |input|_ file in any particular *set* of |input|_.
This will be clear in the following example:
**Example**:
Calculate the **@combinations** of **A,B,C,D** files
If |input|_ is four pairs of file names
.. code-block:: python
:emphasize-lines: 1-6
input_files = [ [ 'A.1_start', 'A.2_start'], # 0
[ 'B.1_start', 'B.2_start'], # 1
[ 'C.1_start', 'C.2_start'], # 2
[ 'D.1_start', 'D.2_start'] ] # 3
The first job of:
.. code-block:: python
@combinations(input_files, formatter(), 3, ...)
Will be
.. <<python
.. code-block:: python
:emphasize-lines: 1-6
# Three file pairs at a time
['A.1_start', 'A.2_start'], # 0
# versus
['B.1_start', 'B.2_start'], # 1
# versus
['C.1_start', 'c.2_start'], # 2
..
python
First level of nesting:
.. code-block:: python
:emphasize-lines: 1-6
['A.1_start', 'A.2_start'] # [0]
['B.1_start', 'B.2_start'] # [1]
['C.1_start', 'C.2_start'] # [2]
Second level of nesting:
.. code-block:: python
:emphasize-lines: 1-6
'A.2_start' # [0][1]
'B.2_start' # [1][1]
'C.2_start' # [2][1]
Parse filename without suffix
.. code-block:: python
:emphasize-lines: 1-6
'A' # {basename[0][1]}
'B' # {basename[1][1]}
'C' # {basename[2][1]}
Python code:
.. code-block:: python
:emphasize-lines: 13,17,20,25,28-30
from ruffus import *
from ruffus.combinatorics import *
# initial file pairs
@originate([ ['A.1_start', 'A.2_start'],
['B.1_start', 'B.2_start'],
['C.1_start', 'C.2_start'],
['D.1_start', 'D.2_start']])
def create_initial_files_ABCD(output_files):
for output_file in output_files:
with open(output_file, "w") as oo: pass
# @combinations
@combinations(create_initial_files_ABCD, # Input
formatter(), # match input files
# tuple of 3 at a time
3,
# Output Replacement string
"{path[0][0]}/"
"{basename[0][1]}_vs_"
"{basename[1][1]}_vs_"
"{basename[2][1]}.combinations",
# Extra parameter: path for 1st set of files, 1st file name
"{path[0][0]}",
# Extra parameter
["{basename[0][0]}", # basename for 1st set of files, 1st file name
"{basename[1][0]}", # 2nd
"{basename[2][0]}", # 3rd
])
def combinations_task(input_file, output_parameter, shared_path, basenames):
print " - ".join(basenames)
#
# Run
#
pipeline_run(verbose=0)
This results in:
.. code-block:: pycon
>>> pipeline_run(verbose=0)
A - B - C
A - B - D
A - C - D
B - C - D
**Parameters:**
.. _decorators.combinations.input:
* **input** = *tasks_or_file_names*
can be a:
#. Task / list of tasks.
File names are taken from the |output|_ of the specified task(s)
#. (Nested) list of file name strings.
File names containing ``*[]?`` will be expanded as a |glob|_.
E.g.:``"a.*" => "a.1", "a.2"``
.. _decorators.combinations.filter:
.. _decorators.combinations.matching_formatter:
* **filter** = *formater(...)*
a :ref:`formatter<decorators.formatter>` indicator object containing optionally
a python `regular expression (re) <http://docs.python.org/library/re.html>`_.
.. _decorators.combinations.tuple_size:
* **tuple_size** = *N*
Select N elements at a time.
.. _decorators.combinations.output:
* **output** = *output*
Specifies the resulting output file name(s) after string substitution
.. _decorators.combinations.extras:
* **extras** = *extras*
Any extra parameters are passed verbatim to the task function
If you are using named parameters, these can be passed as a list, i.e. ``extras= [...]``
Any extra parameters are consumed by the task function and not forwarded further down the pipeline.
|