| 1 |
.. _merge: |
| 2 |
|
| 3 |
############### |
| 4 |
*merge* |
| 5 |
############### |
| 6 |
|
| 7 |
| |
| 8 |
|
| 9 |
.. image:: ../images/tool-glyphs/merge-glyph.png |
| 10 |
:width: 600pt |
| 11 |
| |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
``bedtools merge`` combines overlapping or "book-ended" features in an interval |
| 16 |
file into a single feature which spans all of the combined features. |
| 17 |
|
| 18 |
.. note:: |
| 19 |
|
| 20 |
``bedtools merge`` requires that you presort your data by chromosome and |
| 21 |
then by start position (e.g., ``sort -k1,1 -k2,2n in.bed > in.sorted.bed`` |
| 22 |
for BED files). |
| 23 |
|
| 24 |
.. seealso:: |
| 25 |
|
| 26 |
:doc:`../tools/cluster` |
| 27 |
:doc:`../tools/complement` |
| 28 |
|
| 29 |
|
| 30 |
========================================================================== |
| 31 |
Usage and option summary |
| 32 |
========================================================================== |
| 33 |
**Usage**: |
| 34 |
:: |
| 35 |
|
| 36 |
bedtools merge [OPTIONS] -i <BED/GFF/VCF/BAM> |
| 37 |
|
| 38 |
**(or)**: |
| 39 |
:: |
| 40 |
|
| 41 |
mergeBed [OPTIONS] -i <BED/GFF/VCF/BAM> |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
=========================== =============================================================================================================================================================================================================== |
| 46 |
Option Description |
| 47 |
=========================== =============================================================================================================================================================================================================== |
| 48 |
**-s** Force strandedness. That is, only merge features that are the same strand. *By default, this is disabled*. |
| 49 |
**-S** Force merge for one specific strand only. Follow with + or - to force merge from only the forward or reverse strand, respectively. *By default, merging is done without respect to strand*. |
| 50 |
**-d** Maximum distance between features allowed for features to be merged. *Default is 0. That is, overlapping and/or book-ended features are merged*. |
| 51 |
**-c** Specify columns from the input file to operate upon (see -o option, below). Multiple columns can be specified in a comma-delimited list. |
| 52 |
**-o** | Specify the operation that should be applied to ``-c``. |
| 53 |
| Valid operations: |
| 54 |
| sum, min, max, absmin, absmax, |
| 55 |
| mean, median, |
| 56 |
| collapse (i.e., print a delimited list (duplicates allowed)), |
| 57 |
| distinct (i.e., print a delimited list (NO duplicates allowed)), |
| 58 |
| count |
| 59 |
| count_distinct (i.e., a count of the unique values in the column), |
| 60 |
| **Default:** sum |
| 61 |
| Multiple operations can be specified in a comma-delimited list. |
| 62 |
| If there is only column, but multiple operations, all operations will be |
| 63 |
| applied on that column. Likewise, if there is only one operation, but |
| 64 |
| multiple columns, that operation will be applied to all columns. |
| 65 |
| Otherwise, the number of columns must match the the number of operations, |
| 66 |
| and will be applied in respective order. |
| 67 |
| |
| 68 |
| E.g., ``-c 5,4,6 -o sum,mean,count`` will give the sum of column 5, |
| 69 |
| the mean of column 4, and the count of column 6. |
| 70 |
| The order of output columns will match the ordering given in the command. |
| 71 |
**-header** | Print the header from the A file prior to results. |
| 72 |
|
| 73 |
**-delim** | Specify a custom delimiter for the -nms and -scores concat options |
| 74 |
| Example: ``-delim "|"`` |
| 75 |
| ``Default: ";"`` |
| 76 |
=========================== =============================================================================================================================================================================================================== |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
========================================================================== |
| 83 |
Default behavior |
| 84 |
========================================================================== |
| 85 |
By default, ``bedtools merge`` combines overlapping (by at least 1 bp) and/or |
| 86 |
bookended intervals into a single, "flattened" or "merged" interval. |
| 87 |
|
| 88 |
.. code-block:: bash |
| 89 |
|
| 90 |
$ cat A.bed |
| 91 |
chr1 100 200 |
| 92 |
chr1 180 250 |
| 93 |
chr1 250 500 |
| 94 |
chr1 501 1000 |
| 95 |
|
| 96 |
$ bedtools merge -i A.bed |
| 97 |
chr1 100 500 |
| 98 |
chr1 501 1000 |
| 99 |
|
| 100 |
|
| 101 |
========================================================================== |
| 102 |
``-s`` Enforcing "strandedness" |
| 103 |
========================================================================== |
| 104 |
The ``-s`` option will only merge intervals that are overlapping/bookended |
| 105 |
*and* are on the same strand. |
| 106 |
|
| 107 |
.. code-block:: bash |
| 108 |
|
| 109 |
$ cat A.bed |
| 110 |
chr1 100 200 a1 1 + |
| 111 |
chr1 180 250 a2 2 + |
| 112 |
chr1 250 500 a3 3 - |
| 113 |
chr1 501 1000 a4 4 + |
| 114 |
|
| 115 |
$ bedtools merge -i A.bed -s |
| 116 |
chr1 100 250 |
| 117 |
chr1 501 1000 |
| 118 |
chr1 250 500 |
| 119 |
|
| 120 |
To also report the strand, you could use the ``-c`` and ``-o`` operators (see below for more details): |
| 121 |
|
| 122 |
.. code-block:: bash |
| 123 |
|
| 124 |
$ bedtools merge -i A.bed -s -c 6 -o distinct |
| 125 |
chr1 100 250 + |
| 126 |
chr1 501 1000 + |
| 127 |
|
| 128 |
========================================================================== |
| 129 |
``-S`` Reporting merged intervals on a specific strand. |
| 130 |
========================================================================== |
| 131 |
The ``-S`` option will only merge intervals for a specific strand. For example, |
| 132 |
to only report merged intervals on the "+" strand: |
| 133 |
|
| 134 |
.. code-block:: bash |
| 135 |
|
| 136 |
$ cat A.bed |
| 137 |
chr1 100 200 a1 1 + |
| 138 |
chr1 180 250 a2 2 + |
| 139 |
chr1 250 500 a3 3 - |
| 140 |
chr1 501 1000 a4 4 + |
| 141 |
|
| 142 |
$ bedtools merge -i A.bed -S + |
| 143 |
chr1 100 250 |
| 144 |
chr1 501 1000 |
| 145 |
|
| 146 |
To also report the strand, you could use the ``-c`` and ``-o`` operators (see below for more details): |
| 147 |
|
| 148 |
.. code-block:: bash |
| 149 |
|
| 150 |
$ bedtools merge -i A.bed -S + -c 6 -o distinct |
| 151 |
chr1 100 250 + |
| 152 |
chr1 501 1000 + |
| 153 |
|
| 154 |
|
| 155 |
========================================================================== |
| 156 |
``-d`` Controlling how close two features must be in order to merge |
| 157 |
========================================================================== |
| 158 |
By default, only overlapping or book-ended features are combined into a new |
| 159 |
feature. However, one can force ``merge`` to combine more distant features |
| 160 |
with the ``-d`` option. For example, were one to set ``-d`` to 1000, any |
| 161 |
features that overlap or are within 1000 base pairs of one another will be |
| 162 |
combined. |
| 163 |
|
| 164 |
.. code-block:: bash |
| 165 |
|
| 166 |
$ cat A.bed |
| 167 |
chr1 100 200 |
| 168 |
chr1 501 1000 |
| 169 |
|
| 170 |
$ bedtools merge -i A.bed |
| 171 |
chr1 100 200 |
| 172 |
chr1 501 1000 |
| 173 |
|
| 174 |
$ bedtools merge -i A.bed -d 1000 |
| 175 |
chr1 100 200 1000 |
| 176 |
|
| 177 |
========================================================================== |
| 178 |
``-c`` and ``-o`` Applying operations to columns from merged intervals. |
| 179 |
========================================================================== |
| 180 |
When merging intervals, we often want to summarize or keep track of the |
| 181 |
values observed in specific columns (e.g., the feature name or score) from |
| 182 |
the original, unmerged intervals. When used together, the ``-c`` and ``-o`` |
| 183 |
options allow one to select specific columns (``-c``) and apply operation |
| 184 |
(``-o``) to each column. The result will be appended to the default, merged |
| 185 |
interval output. For example, one could use the following to report the |
| 186 |
count of intervals that we merged in each resulting interval (this replaces |
| 187 |
the ``-n`` option that existed prior to version ``2.20.0``). |
| 188 |
|
| 189 |
.. code-block:: bash |
| 190 |
|
| 191 |
$ cat A.bed |
| 192 |
chr1 100 200 |
| 193 |
chr1 180 250 |
| 194 |
chr1 250 500 |
| 195 |
chr1 501 1000 |
| 196 |
|
| 197 |
$ bedtools merge -i A.bed -c 1 -o count |
| 198 |
chr1 100 500 3 |
| 199 |
chr1 501 1000 1 |
| 200 |
|
| 201 |
We could also use these options to report the mean of the score (#5) field: |
| 202 |
|
| 203 |
.. code-block:: bash |
| 204 |
|
| 205 |
$ cat A.bed |
| 206 |
chr1 100 200 a1 1 + |
| 207 |
chr1 180 250 a2 2 + |
| 208 |
chr1 250 500 a3 3 - |
| 209 |
chr1 501 1000 a4 4 + |
| 210 |
|
| 211 |
$ bedtools merge -i A.bed -c 5 -o mean |
| 212 |
chr1 100 500 2 |
| 213 |
chr1 501 1000 4 |
| 214 |
|
| 215 |
Let's get fancy and report the mean, min, and max of the score column: |
| 216 |
|
| 217 |
.. code-block:: bash |
| 218 |
|
| 219 |
$ bedtools merge -i A.bed -c 5 -o mean,min,max |
| 220 |
chr1 100 500 2 1 3 |
| 221 |
chr1 501 1000 4 4 4 |
| 222 |
|
| 223 |
Let's also report a comma-separated list of the strands: |
| 224 |
|
| 225 |
.. code-block:: bash |
| 226 |
|
| 227 |
$ bedtools merge -i A.bed -c 5,5,5,6 -o mean,min,max,collapse |
| 228 |
chr1 100 500 2 1 3 +,+,- |
| 229 |
chr1 501 1000 4 4 4 + |
| 230 |
|
| 231 |
Hopefully this provides a clear picture of what can be done. |
| 232 |
|
| 233 |
|
| 234 |
========================================================================== |
| 235 |
``-n`` Reporting the number of features that were merged |
| 236 |
========================================================================== |
| 237 |
.. deprecated:: 2.20.0 |
| 238 |
See the ``-c`` and ``-o`` operators. |
| 239 |
|
| 240 |
========================================================================== |
| 241 |
``-nms`` Reporting the names of the features that were merged |
| 242 |
========================================================================== |
| 243 |
.. deprecated:: 2.20.0 |
| 244 |
See the ``-c`` and ``-o`` operators. |
| 245 |
|
| 246 |
========================================================================== |
| 247 |
``-scores`` Reporting the scores of the features that were merged |
| 248 |
========================================================================== |
| 249 |
.. deprecated:: 2.20.0 |
| 250 |
See the ``-c`` and ``-o`` operators. |
| 251 |
|
| 252 |
|
| 253 |
========================================================================== |
| 254 |
``-delim`` Change the delimiter for ``-c`` and ``-o`` |
| 255 |
========================================================================== |
| 256 |
One can override the use of a comma as the delimiter for the ``-c`` and |
| 257 |
``-o collapse|distinct`` options via the use of the ``-delim`` option. |
| 258 |
|
| 259 |
.. code-block:: bash |
| 260 |
|
| 261 |
$ cat A.bed |
| 262 |
chr1 100 200 A1 |
| 263 |
chr1 150 300 A2 |
| 264 |
chr1 250 500 A3 |
| 265 |
|
| 266 |
Compare: |
| 267 |
|
| 268 |
.. code-block:: bash |
| 269 |
|
| 270 |
$ bedtools merge -i A.bed -c 4 -o collapse |
| 271 |
chr1 100 500 A1,A2,A3 |
| 272 |
|
| 273 |
to: |
| 274 |
|
| 275 |
.. code-block:: bash |
| 276 |
|
| 277 |
$ bedtools merge -i A.bed -c 4 -o collapse -delim "|" |
| 278 |
chr1 100 500 A1|A2|A3 |