| 1 |
# GNU Datamash - Usage Examples |
| 2 |
|
| 3 |
This directory contains sample files demonstrating typical usage of the `datamash` |
| 4 |
program. |
| 5 |
|
| 6 |
`datamash` is a command-line calculator of basic operations on columnar input files. |
| 7 |
|
| 8 |
## Synopsis |
| 9 |
|
| 10 |
`datamash` reads input from STDIN, and performs operations (e.g. sum, mean, count) on |
| 11 |
specified columns: |
| 12 |
|
| 13 |
datamash [OPTIONS] op1 column1 [op2 column2]... |
| 14 |
|
| 15 |
**op1** is the operation to perform, one of: count,sum,min,max,absmin, |
| 16 |
absmax,mean,median,mode,antimode,pstdev,sstdev,pvar,svar. |
| 17 |
|
| 18 |
**column1** is the column (in the input file) to use for **op1**. |
| 19 |
|
| 20 |
**OPTIONS** are possible command-line options which affect the behaviour of `datamash`. |
| 21 |
|
| 22 |
|
| 23 |
Example: sum and count the number of even values between 0 and 100: |
| 24 |
|
| 25 |
$ seq 0 2 100 | datamash sum 1 |
| 26 |
2550 |
| 27 |
$ seq 0 2 100 | datamash count 1 |
| 28 |
51 |
| 29 |
|
| 30 |
|
| 31 |
## Example: Test Scores |
| 32 |
|
| 33 |
The file `scores.txt` contains tests scores of college students of different majors |
| 34 |
(Arts, Business, Health and Medicine, Life Sciences, Engineering, Social Sciences). |
| 35 |
|
| 36 |
The files has three columns: Name, Major, Score: |
| 37 |
|
| 38 |
$ cat scores.txt |
| 39 |
Shawn Arts 65 |
| 40 |
Marques Arts 58 |
| 41 |
Fernando Arts 78 |
| 42 |
Paul Arts 63 |
| 43 |
Walter Arts 75 |
| 44 |
... |
| 45 |
|
| 46 |
Using `datamash`, find the lowest (min) and highest (max) score for each College Major: |
| 47 |
(Major is in column 2, the score values are in column 3): |
| 48 |
|
| 49 |
$ datamash -g 2 min 3 max 3 < scores.txt |
| 50 |
Arts 46 88 |
| 51 |
Business 79 94 |
| 52 |
Health-Medicine 72 100 |
| 53 |
Social-Sciences 27 90 |
| 54 |
Life-Sciences 14 91 |
| 55 |
Engineering 39 99 |
| 56 |
|
| 57 |
Similarly, find the number of students, mean score and sample-standard-deviation for each College major: |
| 58 |
|
| 59 |
$ datamash -g 2 count 3 mean 3 sstdev 3 < scores.txt |
| 60 |
Arts 68.9474 10.4215 |
| 61 |
Business 87.3636 5.18214 |
| 62 |
Health-Medicine 90.6154 9.22441 |
| 63 |
Social-Sciences 60.2667 17.2273 |
| 64 |
Life-Sciences 55.3333 20.606 |
| 65 |
Engineering 66.5385 19.8814 |
| 66 |
|
| 67 |
|
| 68 |
## Example: Header Lines |
| 69 |
|
| 70 |
A *header line* is an optional first line in the input or output files, which labels each column. |
| 71 |
`datamash` can generate header line in the output file, even if the input file doesn't have a header line (`scores.txt` does not have a header line, the first line in the file contains data). |
| 72 |
|
| 73 |
Use '--header-out' to add a header line to the output (when the input does not contain a header line): |
| 74 |
|
| 75 |
$ datamash --header-out -g 2 count 3 mean 3 pstdev 3 < scores.txt |
| 76 |
GroupBy(field-2) mean(field-3) sstdev(field-3) |
| 77 |
Arts 68.9474 10.4215 |
| 78 |
Business 87.3636 5.18214 |
| 79 |
Health-Medicine 90.6154 9.22441 |
| 80 |
Social-Sciences 60.2667 17.2273 |
| 81 |
Life-Sciences 55.3333 20.606 |
| 82 |
Engineering 66.5385 19.8814 |
| 83 |
|
| 84 |
|
| 85 |
When the input file has a header line, `datamash` can will use the labels from each column in the output header line. `scores_h.txt` contains the same information as `scores.txt`, with an additional header line: |
| 86 |
|
| 87 |
$ cat scores_h.txt |
| 88 |
Name Major Score |
| 89 |
Shawn Arts 65 |
| 90 |
Marques Arts 58 |
| 91 |
Fernando Arts 78 |
| 92 |
Paul Arts 63 |
| 93 |
Walter Arts 75 |
| 94 |
... |
| 95 |
|
| 96 |
|
| 97 |
Use `-H` (equivalent to `--header-in --header-out`) to use input headers and print output headers: |
| 98 |
|
| 99 |
$ datamash -H -g 2 count 3 mean 3 pstdev 3 < scores_h.txt |
| 100 |
GroupBy(Major) mean(Score) sstdev(Score) |
| 101 |
Arts 68.9474 10.4215 |
| 102 |
Business 87.3636 5.18214 |
| 103 |
Health-Medicine 90.6154 9.22441 |
| 104 |
Social-Sciences 60.2667 17.2273 |
| 105 |
Life-Sciences 55.3333 20.606 |
| 106 |
Engineering 66.5385 19.8814 |
| 107 |
|
| 108 |
|
| 109 |
## Example: Human Genes |
| 110 |
|
| 111 |
**NOTE:** The follow examples assume some biology background knowledge. |
| 112 |
|
| 113 |
The `genes.txt` file contains a small subset of the Human Genome genes. |
| 114 |
The full dataset is available at the [UCSC Genome Browser](http://hgdownload.cse.ucsc.edu/downloads.html)'s |
| 115 |
[Human Genome Database](http://hgdownload.soe.ucsc.edu/goldenPath/hg19/database/) as |
| 116 |
[refGene.txt.gz](http://hgdownload.soe.ucsc.edu/goldenPath/hg19/database/refGene.txt.gz). |
| 117 |
|
| 118 |
The columns of `genes.txt` are: |
| 119 |
|
| 120 |
1. bin |
| 121 |
2. name - isoform/transcript identifier |
| 122 |
3. chromosome |
| 123 |
4. strand |
| 124 |
5. txStart - transcription start site |
| 125 |
6. txEnda - transcription end site |
| 126 |
7. cdsStart - coding start site |
| 127 |
8. cdsEnd - coding end site |
| 128 |
9. exonCount - number of exons |
| 129 |
10. exonStarts |
| 130 |
11. exonEnds |
| 131 |
12. score |
| 132 |
13. GeneName - gene identifier |
| 133 |
14. cdsStartStat |
| 134 |
15. cdsEndStat |
| 135 |
16. exonFrames |
| 136 |
|
| 137 |
### Number of isoforms per gene |
| 138 |
|
| 139 |
The gene identifiers are in column 13, the transcript identifiers are in column 2. |
| 140 |
To count how many isoforms each gene has, use `datamash` to group by column 13, and for each group, count the values in column 2 (use `-s` to automatically sort the input file): |
| 141 |
|
| 142 |
$ datamash -s -g 13 count 2 < genes.txt |
| 143 |
ABCC1 1 |
| 144 |
ABCC10 2 |
| 145 |
ABCC11 3 |
| 146 |
ABCC12 1 |
| 147 |
ABCC13 2 |
| 148 |
... |
| 149 |
|
| 150 |
Using the `collapse` operation, we can print all the isoforms for each gene: |
| 151 |
|
| 152 |
$ datamash -s -g 13 count 2 collapse 2 < genes.txt |
| 153 |
ABCC1 1 NM_004996 |
| 154 |
ABCC10 2 NM_001198934,NM_033450 |
| 155 |
ABCC11 3 NM_032583,NM_033151,NM_145186 |
| 156 |
ABCC12 1 NM_033226 |
| 157 |
ABCC13 2 NR_003087,NR_003088 |
| 158 |
... |
| 159 |
|
| 160 |
|
| 161 |
### Combining datamash with other programs |
| 162 |
|
| 163 |
Combining `datamash` with additional filtering programs (such as `awk`), we can find relevant information, such as: |
| 164 |
|
| 165 |
Which genes have more than 5 isoforms? |
| 166 |
|
| 167 |
$ cat genes.txt | datamash -s -g 13 count 2 collapse 2 | awk '$2>5' |
| 168 |
AC159540.1 6 NR_040097,NR_103732,NR_103733,NR_040097,NR_103732,NR_103733 |
| 169 |
ACSF3 6 NM_001127214,NM_001243279,NM_001284316,NM_174917,NR_045667,NR_104293 |
| 170 |
ADAM29 7 NM_001130703,NM_001130704,NM_001130705,NM_001278125,NM_001278126,NM_001278127,NM_014269 |
| 171 |
AIPL1 8 NM_001033054,NM_001033055,NM_001285399,NM_001285400,NM_001285401,NM_001285402,NM_001285403,NM_014336 |
| 172 |
ANXA8 6 NM_001040084,NM_001271702,NM_001271703,NM_001040084,NM_001271702,NM_001271703 |
| 173 |
... |
| 174 |
|
| 175 |
|
| 176 |
Using `datamash` we can quickly explore the dataset and answer simple question, such as: |
| 177 |
|
| 178 |
How many genes are transcribed from both strands (that is, they have isoforms with both positive and negative strands. |
| 179 |
strand column is number 4): |
| 180 |
|
| 181 |
$ cat genes.txt | datamash -s -g 13 countunique 4 | awk '$2>1' |
| 182 |
AC159540.1 2 |
| 183 |
AMY1C 2 |
| 184 |
ANXA8 2 |
| 185 |
BMS1P17 2 |
| 186 |
BMS1P18 2 |
| 187 |
... |
| 188 |
|
| 189 |
Which genes are transcribed from multiple chromosomes (that is, they have isoforms from multiple chromosomes. |
| 190 |
Chromosome column is number 3): |
| 191 |
|
| 192 |
$ cat genes.txt | datamash -s -g 13 countunique 2 unique 2 | awk '$2>1' |
| 193 |
AKAP17A 2 chrX,chrY |
| 194 |
ASMT 2 chrX,chrY |
| 195 |
ASMTL 2 chrX,chrY |
| 196 |
ASMTL-AS1 2 chrX,chrY |
| 197 |
BMS1P17 2 chr14,chr22 |
| 198 |
... |
| 199 |
|
| 200 |
|
| 201 |
Explore Exon-count variability (for each gene, list the minimum, maximum, mean and stddev of the |
| 202 |
exon-count of its isoforms. Exon-Count column is number 9): |
| 203 |
|
| 204 |
$ cat genes.txt | datamash -s -g 13 count 9 min 9 max 9 mean 9 pstdev 9 | awk '$2>1' |
| 205 |
ABCC10 2 20 22 21 1 |
| 206 |
ABCC11 3 29 30 29.3 0.471405 |
| 207 |
ABCC13 2 5 6 5.5 0.5 |
| 208 |
ABCC3 2 12 31 21.5 9.5 |
| 209 |
AC159540.1 6 4 5 4.1 0.372678 |
| 210 |
... |
| 211 |
|
| 212 |
### Grouping multiple fields |
| 213 |
|
| 214 |
Chromosome name is in column 3. How many transcripts are in each chromosome? |
| 215 |
|
| 216 |
$ datamash -s -g 3 count 2 < genes.txt |
| 217 |
chr1 365 |
| 218 |
chr10 164 |
| 219 |
chr11 189 |
| 220 |
chr12 187 |
| 221 |
chr13 66 |
| 222 |
... |
| 223 |
|
| 224 |
Strand information is in column 4. How many transcripts are in each chromsomse AND strand? |
| 225 |
|
| 226 |
$ datamash -s -g 3,4 count 2 < genes.txt |
| 227 |
chr1 - 183 |
| 228 |
chr1 + 182 |
| 229 |
chr10 - 52 |
| 230 |
chr10 + 112 |
| 231 |
chr11 - 105 |
| 232 |
chr11 + 84 |
| 233 |
chr12 - 117 |
| 234 |
chr12 + 70 |
| 235 |
... |
| 236 |
|
| 237 |
|
| 238 |
## More Information |
| 239 |
|
| 240 |
For more information about `datamash` usage, run `datamash --help`, and the [datamash Website](https://www.gnu.org/software/datamash). |