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
|
.. include:: ../global.inc
.. _decorators.collate:
.. index::
pair: @collate; Syntax
.. role:: raw-html(raw)
:format: html
:raw-html:`<style> .red {color:red} </style>`
.. role:: red
.. seealso::
* :ref:`@collate <new_manual.collate>` in the **Ruffus** Manual
* :ref:`Decorators <decorators>` for more decorators
.. |input| replace:: `input`
.. _input: `decorators.collate.input`_
.. |extras| replace:: `extras`
.. _extras: `decorators.collate.extras`_
.. |output| replace:: `output`
.. _output: `decorators.collate.output`_
.. |filter| replace:: `filter`
.. _filter: `decorators.collate.filter`_
.. |matching_regex| replace:: `matching_regex`
.. _matching_regex: `decorators.collate.matching_regex`_
.. |matching_formatter| replace:: `matching_formatter`
.. _matching_formatter: `decorators.collate.matching_formatter`_
########################################################################
collate
########################################################################
************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
@collate( |input|_, |filter|_, |output|_, [|extras|_,...] )
************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
**Purpose:**
Use |filter|_ to identify common sets of |input|_\s which are to be grouped or collated together:
Each set of |input|_\ s which generate identical |output|_ and |extras|_ using the
:ref:`formatter<decorators.formatter>` or :ref:`regex<decorators.regex>` (regular expression)
filters are collated into one job.
This is a **many to fewer** operation.
Only out of date jobs (comparing input and output files) will be re-run.
**Example**:
``regex(r".+\.(.+)$")``, ``"\1.summary"`` creates a separate summary file for each suffix::
animal_files = "a.fish", "b.fish", "c.mammals", "d.mammals"
# summarise by file suffix:
@collate(animal_files, regex(r".+\.(.+)$"), r'\1.summary')
def summarize(infiles, summary_file):
pass
#. |output|_ and optional |extras|_ parameters are passed to the functions after string
substitution. Non-string values are passed through unchanged.
#. Each collate job consists of |input|_ files which are aggregated by string substitution
to identical |output|_ and |extras|_
#. | The above example results in two jobs:
| ``["a.fish", "b.fish" -> "fish.summary"]``
| ``["c.mammals", "d.mammals" -> "mammals.summary"]``
**Parameters:**
.. _decorators.collate.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 (as in the example above).
File names containing ``*[]?`` will be expanded as a |glob|_.
E.g.:``"a.*" => "a.1", "a.2"``
.. _decorators.collate.filter:
.. _decorators.collate.matching_regex:
* **filter** = *matching_regex*
is a python regular expression string, which must be wrapped in
a :ref:`regex<decorators.regex>` indicator object
See python `regular expression (re) <http://docs.python.org/library/re.html>`_
documentation for details of regular expression syntax
.. _decorators.collate.matching_formatter:
* **filter** = *matching_formatter*
a :ref:`formatter<decorators.formatter>` indicator object containing optionally
a python `regular expression (re) <http://docs.python.org/library/re.html>`_.
.. _decorators.collate.output:
* **output** = *output*
Specifies the resulting output file name(s) after string substitution
.. _decorators.collate.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.
**Example2**:
Suppose we had the following files::
cows.mammals.animal
horses.mammals.animal
sheep.mammals.animal
snake.reptile.animal
lizard.reptile.animal
crocodile.reptile.animal
pufferfish.fish.animal
and we wanted to end up with three different resulting output::
cow.mammals.animal
horse.mammals.animal
sheep.mammals.animal
-> mammals.results
snake.reptile.animal
lizard.reptile.animal
crocodile.reptile.animal
-> reptile.results
pufferfish.fish.animal
-> fish.results
This is the ``@collate`` code required::
animals = [ "cows.mammals.animal",
"horses.mammals.animal",
"sheep.mammals.animal",
"snake.reptile.animal",
"lizard.reptile.animal",
"crocodile.reptile.animal",
"pufferfish.fish.animal"]
@collate(animals, regex(r"(.+)\.(.+)\.animal"), r"\2.results")
# \1 = species [cow, horse]
# \2 = phylogenetics group [mammals, reptile, fish]
def summarize_animals_into_groups(species_file, result_file):
" ... more code here"
pass
See :ref:`@merge <decorators.merge>` for an alternative way to summarise files.
.. _decorators.collate_ex:
.. index::
pair: @collate (Advanced Usage); Syntax
pair: @collate, inputs(...); Syntax
pair: @collate, add_inputs(...); Syntax
.. seealso::
* :ref:`Use of add_inputs(...) | inputs(...) <new_manual.inputs>` in the **Ruffus** Manual
.. |coll_input| replace:: `input`
.. _coll_input: `decorators.collate_ex.input`_
.. |coll_extras| replace:: `extras`
.. _coll_extras: `decorators.collate_ex.extras`_
.. |coll_output| replace:: `output`
.. _coll_output: `decorators.collate_ex.output`_
.. |coll_filter| replace:: `filter`
.. _coll_filter: `decorators.collate_ex.filter`_
.. |coll_matching_regex| replace:: `matching_regex`
.. _coll_matching_regex: `decorators.collate_ex.matching_regex`_
.. |coll_matching_formatter| replace:: `matching_formatter`
.. _coll_matching_formatter: `decorators.collate_ex.matching_formatter`_
.. |coll_replace_inputs| replace:: `replace_inputs`
.. _coll_replace_inputs: `decorators.collate_ex.replace_inputs`_
.. |coll_add_inputs| replace:: `add_inputs`
.. _coll_add_inputs: `decorators.collate_ex.add_inputs`_
************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
collate( |coll_input|_, |coll_filter|_, |coll_replace_inputs|_ | |coll_add_inputs|_, |coll_output|_, [|coll_extras|_,...] )
************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
**Purpose:**
Use |coll_filter|_ to identify common sets of |coll_input|_\s which are to be grouped or collated together:
Each set of |coll_input|_\ s which generate identical |coll_output|_ and |coll_extras|_ using the
:ref:`formatter<decorators.formatter>` or :ref:`regex<decorators.regex>` (regular expression)
filters are collated into one job.
This variant of ``@collate`` allows additional inputs or dependencies to be added
dynamically to the task, with optional string substitution.
:ref:`add_inputs<decorators.add_inputs>` nests the the original input parameters in a list before adding additional dependencies.
:ref:`inputs<decorators.inputs>` replaces the original input parameters wholescale.
This is a **many to fewer** operation.
Only out of date jobs (comparing input and output files) will be re-run.
**Example of** :ref:`add_inputs<decorators.add_inputs>`
``regex(r".*(\..+)"), "\1.summary"`` creates a separate summary file for each suffix.
But we also add date of birth data for each species::
animal_files = "tuna.fish", "shark.fish", "dog.mammals", "cat.mammals"
# summarise by file suffix:
@collate(animal_files, regex(r".+\.(.+)$"), add_inputs(r"\1.date_of_birth"), r'\1.summary')
def summarize(infiles, summary_file):
pass
This results in the following equivalent function calls::
summarize([ ["shark.fish", "fish.date_of_birth" ],
["tuna.fish", "fish.date_of_birth" ] ], "fish.summary")
summarize([ ["cat.mammals", "mammals.date_of_birth"],
["dog.mammals", "mammals.date_of_birth"] ], "mammals.summary")
**Example of** :ref:`add_inputs<decorators.inputs>`
using ``inputs(...)`` will summarise only the dates of births for each species group::
animal_files = "tuna.fish", "shark.fish", "dog.mammals", "cat.mammals"
# summarise by file suffix:
@collate(animal_files, regex(r".+\.(.+)$"), inputs(r"\1.date_of_birth"), r'\1.summary')
def summarize(infiles, summary_file):
pass
This results in the following equivalent function calls::
summarize(["fish.date_of_birth" ], "fish.summary")
summarize(["mammals.date_of_birth"], "mammals.summary")
**Parameters:**
.. _decorators.collate_ex.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 (as in the example above).
File names containing ``*[]?`` will be expanded as a |glob|_.
E.g.:``"a.*" => "a.1", "a.2"``
.. _decorators.collate_ex.filter:
.. _decorators.collate_ex.matching_regex:
* **filter** = *matching_regex*
is a python regular expression string, which must be wrapped in
a :ref:`regex<decorators.regex>` indicator object
See python `regular expression (re) <http://docs.python.org/library/re.html>`_
documentation for details of regular expression syntax
.. _decorators.collate_ex.matching_formatter:
* **filter** = *matching_formatter*
a :ref:`formatter<decorators.formatter>` indicator object containing optionally
a python `regular expression (re) <http://docs.python.org/library/re.html>`_.
.. _decorators.collate_ex.add_inputs:
.. _decorators.collate_ex.replace_inputs:
* **add_inputs** = *add_inputs*\ (...) or **replace_inputs** = *inputs*\ (...)
Specifies the resulting |coll_input|_\ (s) to each job.
Positional parameters must be disambiguated by wrapping the values in :ref:`inputs(...)<decorators.inputs>` or an :ref:`add_inputs(...)<decorators.add_inputs>`.
Named parameters can be passed the values directly.
Takes:
#. Task / list of tasks.
File names are taken from the output of the specified task(s)
#. (Nested) list of file name strings.
Strings will be subject to substitution.
File names containing ``*[]?`` will be expanded as a |glob|_.
E.g. ``"a.*" => "a.1", "a.2"``
.. _decorators.collate_ex.output:
* **output** = *output*
Specifies the resulting output file name(s).
.. _decorators.collate_ex.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= [...]``
See :ref:`@collate <decorators.collate>` for more straightforward ways to use collate.
|