Debian Codegraph @

docs/content/tools/groupby.rst

raw
1 .. _groupby:
2
3 ###############
4 *groupby*
5 ###############
6 ``bedtools groupby`` is a useful tool that mimics the "group by" clause in database
7 systems. Given a file or stream that is sorted by the appropriate "grouping
8 columns" (``-g``), ``groupby`` will compute summary statistics on another
9 column (``-c``) in the file or stream. This will work with output from all
10 BEDTools as well as any other tab-delimited file or stream. As such, this is a
11 generally useful tool for all command-line analyses, not just genomics related
12 research.
13
14 .. note::
15 When using ``bedtools groupby``, the input data must be ordered by the same
16 columns as specified with the ``-grp`` argument, which establish which
17 columns should be used to define a group of similar data. For example,
18 if ``-grp`` is 1,2,3, the data should be pre-grouped accordingly. When
19 ``bedtools groupby`` detects changes in the group columns it then summarizes
20 all lines with that group. For example, ``sort -k1,1 -k2,2 -k3,3 data.txt
21 | bedtools groupby -g 1,2,3 -c 4 -o mean``.
22
23
24 ==========================================================================
25 Usage and option summary
26 ==========================================================================
27 Usage
28 ::
29
30 bedtools groupby [OPTIONS] -i <input> -g <group columns> -c <op. column> -o <operation>
31
32 or:
33 ::
34
35 groupBy [OPTIONS] -i <input> -g <group columns> -c <op. column> -o <operation>
36
37 =========================== ===============================================================================================================================================================================================================
38 Option Description
39 =========================== ===============================================================================================================================================================================================================
40 **-i** The input file that should be grouped and summarized. *Use "stdin" when using piped input*.
41 **Note: if -i is omitted, input is assumed to come from standard input (stdin)**
42 **-g (-grp)** Specifies which column(s) (1-based) should be used to group the input. Columns may be comma-separated with each column must be explicitly listed. Or, ranges (e.g. 1-4) are also allowed. *Default: 1,2,3*
43 **-c (-opCol)** Specify the column (1-based) that should be summarized. *Required*.
44 **-o (-op)** Specify the operation that should be applied to **opCol**.
45
46 | Valid operations:
47
48 | **sum** - *numeric only*
49 | **count** - *numeric or text*
50 | **count_distinct** - *numeric or text*
51 | **min** - *numeric only*
52 | **max** - *numeric only*
53 | **mean** - *numeric only*
54 | **median** - *numeric only*
55 | **mode** - *numeric or text*
56 | **antimode** - *numeric or text*
57 | **stdev** - *numeric only*
58 | **sstdev** - *numeric only*
59 | **collapse** (i.e., print a comma separated list) - *numeric or text*
60 | **distinct** (i.e., print a comma separated list) - *numeric or text*
61 | **concat** (i.e., print a comma separated list) - *numeric or text*
62 | **freqasc** - *print a comma separated list of values observed and the number of times they were observed.*
63 | Reported in ascending order of frequency*
64 | **freqdesc** - *print a comma separated list of values observed and the number of times they were observed.*
65 | Reported in descending order of frequency*
66 | **first** - *numeric or text*
67 | **last** - *numeric or text*
68 | *Default: sum*
69 =========================== ===============================================================================================================================================================================================================
70
71
72
73
74
75 ==========================================================================
76 Default behavior.
77 ==========================================================================
78 Let's imagine we have three incredibly interesting genetic variants that we are
79 studying and we are interested in what annotated repeats these variants overlap.
80
81 .. code-block:: bash
82
83 $ cat variants.bed
84 chr21 9719758 9729320 variant1
85 chr21 9729310 9757478 variant2
86 chr21 9795588 9796685 variant3
87
88 $ bedtools intersect -a variants.bed -b repeats.bed -wa -wb > variantsToRepeats.bed
89 $ cat variantsToRepeats.bed
90 chr21 9719758 9729320 variant1 chr21 9719768 9721892 ALR/Alpha 1004 +
91 chr21 9719758 9729320 variant1 chr21 9721905 9725582 ALR/Alpha 1010 +
92 chr21 9719758 9729320 variant1 chr21 9725582 9725977 L1PA3 3288 +
93 chr21 9719758 9729320 variant1 chr21 9726021 9729309 ALR/Alpha 1051 +
94 chr21 9729310 9757478 variant2 chr21 9729320 9729809 L1PA3 3897 -
95 chr21 9729310 9757478 variant2 chr21 9729809 9730866 L1P1 8367 +
96 chr21 9729310 9757478 variant2 chr21 9730866 9734026 ALR/Alpha 1036 -
97 chr21 9729310 9757478 variant2 chr21 9734037 9757471 ALR/Alpha 1182 -
98 chr21 9795588 9796685 variant3 chr21 9795589 9795713 (GAATG)n 308 +
99 chr21 9795588 9796685 variant3 chr21 9795736 9795894 (GAATG)n 683 +
100 chr21 9795588 9796685 variant3 chr21 9795911 9796007 (GAATG)n 345 +
101 chr21 9795588 9796685 variant3 chr21 9796028 9796187 (GAATG)n 756 +
102 chr21 9795588 9796685 variant3 chr21 9796202 9796615 (GAATG)n 891 +
103 chr21 9795588 9796685 variant3 chr21 9796637 9796824 (GAATG)n 621 +
104
105
106 We can see that variant1 overlaps with 3 repeats, variant2 with 4 and variant3
107 with 6. We can use bedtools groupby to summarize the hits for each variant in
108 several useful ways. The default behavior is to compute the *sum* of the opCol.
109
110 .. code-block:: bash
111
112 $ bedtools groupby -i variantsToRepeats.bed -g 1,2,3 -c 9
113 chr21 9719758 9729320 6353
114 chr21 9729310 9757478 14482
115 chr21 9795588 9796685 3604
116
117
118
119 ==========================================================================
120 Computing the min and max.
121 ==========================================================================
122 Now let's find the *min* and *max* repeat score for each variant. We do this
123 by "grouping" on the variant coordinate columns (i.e. cols. 1,2 and 3) and
124 ask for the min and max of the repeat score column (i.e. col. 9).
125
126 .. code-block:: bash
127
128 $ bedtools groupby -i variantsToRepeats.bed -g 1,2,3 -c 9 -o min
129 chr21 9719758 9729320 1004
130 chr21 9729310 9757478 1036
131 chr21 9795588 9796685 308
132
133 We can also group on just the *name* column with similar effect.
134
135 .. code-block:: bash
136
137 $ bedtools groupby -i variantsToRepeats.bed -g 4 -c 9 -o min
138 variant1 1004
139 variant2 1036
140 variant3 308
141
142 What about the *max* score? Let's keep the coordinates and the name of the
143 variants so that we stay in BED format.
144
145 .. code-block:: bash
146
147 $ bedtools groupby -i variantsToRepeats.bed -grp 1-4 -c 9 -o max
148 chr21 9719758 9729320 variant1 3288
149 chr21 9729310 9757478 variant2 8367
150 chr21 9795588 9796685 variant3 891
151
152
153
154 ==========================================================================
155 Computing the mean and median.
156 ==========================================================================
157 Now let's find the *mean* and *median* repeat score for each variant.
158
159 .. code-block:: bash
160
161 $ cat variantsToRepeats.bed | bedtools groupby -g 1-4 -c 9 -o mean
162 chr21 9719758 9729320 variant1 1588.25
163 chr21 9729310 9757478 variant2 3620.5
164 chr21 9795588 9796685 variant3 600.6667
165
166 $ bedtools groupby -i variantsToRepeats.bed -g 1-4 -c 9 -o median
167 chr21 9719758 9729320 variant1 1030.5
168 chr21 9729310 9757478 variant2 2539.5
169 chr21 9795588 9796685 variant3 652
170
171
172 ==========================================================================
173 Computing the mode and "antimode".
174 ==========================================================================
175 Now let's find the *mode* and *antimode* (i.e., the least frequent) repeat
176 score for each variant (in this case they are identical).
177
178 .. code-block:: bash
179
180 $ bedtools groupby -i variantsToRepeats.bed -g 1-4 -c 9 -o mode
181 chr21 9719758 9729320 variant1 1004
182 chr21 9729310 9757478 variant2 1036
183 chr21 9795588 9796685 variant3 308
184
185 $ bedtools groupby -i variantsToRepeats.bed -g 1-4 -c 9 -o antimode
186 chr21 9719758 9729320 variant1 1004
187 chr21 9729310 9757478 variant2 1036
188 chr21 9795588 9796685 variant3 308
189
190
191
192 ==========================================================================
193 Computing the count of lines for a given group.
194 ==========================================================================
195 Figure:
196
197 .. code-block:: bash
198
199 $ bedtools groupby -i variantsToRepeats.bed -g 1-4 -c 9 -o count
200 chr21 9719758 9729320 variant1 4
201 chr21 9729310 9757478 variant2 4
202 chr21 9795588 9796685 variant3 6
203
204
205
206
207 ============================================================================
208 Collapsing: listing all of the values in the opCol for a given group.
209 ============================================================================
210 Now for something different. What if we wanted all of the names of the repeats
211 listed on the same line as the variants? Use the collapse option. This
212 "denormalizes" things. Now you have a list of all the repeats on a single line.
213
214 .. code-block:: bash
215
216 $ bedtools groupby -i variantsToRepeats.bed -grp 1-4 -c 9 -o collapse
217 chr21 9719758 9729320 variant1 ALR/Alpha,ALR/Alpha,L1PA3,ALR/Alpha,
218 chr21 9729310 9757478 variant2 L1PA3,L1P1,ALR/Alpha,ALR/Alpha,
219 chr21 9795588 9796685 variant3 (GAATG)n,(GAATG)n,(GAATG)n,(GAATG)n,(GAATG)n,(GAATG)n,
220
221
222
223 ==========================================================================
224 Computing frequencies: freqasc and freqdesc.
225 ==========================================================================
226 What if we want to report each distinct value along with its number of
227 occurrence (much like uniq -c)? The ``freqasc`` and freqdesc`` operations
228 handle this.
229
230 .. code-block:: bash
231
232 $ cat variantsToRepeats.bed | bedtools groupby -g 1 -c 8 -o freqdesc
233 chr21 (GAATG)n:6,ALR/Alpha:5,L1PA3:2,L1P1:1,
234
235 $ cat variantsToRepeats.bed | bedtools groupby -g 1 -c 8 -o freqasc
236 chr21 L1P1:1,L1PA3:2,ALR/Alpha:5,(GAATG)n:6,
237