-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path02-unix-shell-intro.html
711 lines (664 loc) · 33 KB
/
02-unix-shell-intro.html
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Introduction to Bioinformatics</title>
<meta name="description" content="Introduction to Bioinformatics">
<!-- Disabled for now - embeds HTML in author string -->
<!-- <meta name="author" content="Lesson 2 - Unix Foundations" /> -->
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="reveal.js/css/reveal.css">
<link rel="stylesheet" href="src/theme/csmall.css" id="theme">
<!-- For syntax highlighting -->
<link rel="stylesheet" href="reveal.js/lib/css/zenburn.css">
<!-- If the query includes 'print-pdf', use the PDF print sheet -->
<script>
document.write( '<link rel="stylesheet" href="reveal.js/css/print/' +
( window.location.search.match( /print-pdf/gi ) ? 'pdf' : 'paper' ) +
'.css" type="text/css" media="print">' );
</script>
<!--[if lt IE 9]>
<script src="reveal.js/lib/js/html5shiv.js"></script>
<![endif]-->
<!-- Note escaped dollars -->
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}
});
</script>
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h1>Introduction to Bioinformatics</h1>
<h3>Lesson 2 - Unix Foundations</h3>
<p>
<h4></h4>
</p>
</section>
<section id="the-shell" class="level1">
<h1>The Shell</h1>
<section id="shell-bioinformatician-water-fish" class="level2">
<h2>Shell : Bioinformatician :: Water : Fish</h2>
<p><br/></p>
<ul>
<li>Philosophy</li>
<li>Tools</li>
<li>Cluster computing</li>
</ul>
</section>
</section>
<section id="the-unix-philosophy" class="level1">
<h1>The Unix Philosophy</h1>
<ul>
<li>Small tools that do one thing right</li>
<li>Plumb tools together into pipelines/scripts</li>
<li>Embrace plain text/data</li>
</ul>
<p>All of this is in service of <strong>composability</strong>.</p>
<p>How does this help bioinformatics?</p>
</section>
<section id="breaking-down-unix" class="level1">
<h1>Breaking down Unix</h1>
</section>
<section id="directories-files-and-paths-oh-my" class="level1">
<h1>Directories, files and paths, oh my!</h1>
<p><em>In which we see the static world of things</em></p>
</section>
<section id="in-unix-almost-everything-is-a-file" class="level1">
<h1>In Unix, (almost) everything is a file</h1>
<ul>
<li>programs</li>
<li>devices</li>
<li><code>/dev/null</code></li>
<li>etc.</li>
</ul>
</section>
<section id="and-files-have-paths" class="level1">
<h1>And files have <strong>paths</strong></h1>
<p>Paths are the addresses for files, and are based on <em>directories</em>.</p>
</section>
<section id="directories-aka-folders-are-containers" class="level1">
<h1>Directories (aka “folders”) are containers</h1>
<section id="for-more-directories-and-files" class="level2">
<h2>…for more directories and files…</h2>
</section>
</section>
<section id="some-special-paths-directories" class="level1">
<h1>Some special paths & directories:</h1>
<ul>
<li><code>/</code> - The “root” of the filesystem; everything lives here</li>
<li><code>.</code> - Current directory (depends on where you are)</li>
<li><code>..</code> - Parent directory (depends on where you are)</li>
<li><code>~</code> - Home directory</li>
</ul>
</section>
<section id="composing-paths" class="level1">
<h1>Composing paths</h1>
<p>We can also use <code>/</code> to represent how paths relate to each other:</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="bu">cd</span> ~ <span class="co"># set our "working directory" to be home</span>
<span class="co"># First try listing bioinfclass contents</span>
<span class="fu">ls</span> bioinfclass
<span class="co"># Now try listing data directory contents</span>
<span class="fu">ls</span> data
<span class="co"># ^ fails (no data in ~); Relative to current working directory tho?</span>
<span class="fu">ls</span> bioinfclass/data
<span class="co"># We could also say</span>
<span class="fu">ls</span> ./bioinfclass/data
<span class="fu">ls</span> ~/bioinfclass/data
<span class="fu">ls</span> /home/<span class="op"><</span>yourusername<span class="op">></span>/bioinfclass/data # ALL can be traced to /</code></pre></div>
</section>
<section id="more-on-current-working-directory" class="level1">
<h1>More on current working directory</h1>
<p>Every process in Unix (including your shell sessions) will have a <em>working directory</em> associated with it, and you can see this with <code>pwd</code>. Files can be accessed relative to this directory.</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="bu">cd</span> ~
<span class="bu">pwd</span>
<span class="co"># list the contents of this directory</span>
<span class="fu">ls</span> bioinfclass/data
<span class="bu">cd</span> bioinfclass
<span class="bu">pwd</span>
<span class="co"># now data is fine relative to our current working directory</span>
<span class="fu">ls</span> data</code></pre></div>
</section>
<section id="exercise-going-up-the-directory-chain" class="level1">
<h1>Exercise: going up the directory chain</h1>
<p>Now that we are in the <code>~/bioinformatics</code> directory, how could we refer to another file in <code>~</code>, like your <code>.tmux.conf</code>?</p>
</section>
<section id="going-up-the-directory-chain" class="level1">
<h1>Going up the directory chain</h1>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">ls</span> ~/.tmux.conf
<span class="fu">ls</span> /home/<span class="op"><</span>yourusername<span class="op">></span>/.tmux.conf
<span class="co"># Using relative paths</span>
<span class="fu">ls</span> ../.tmux.conf</code></pre></div>
</section>
<section id="we-can-change-the-path-of-a-file-using-mv" class="level1">
<h1>We can change the path of a file using <code>mv</code></h1>
<section id="section" class="level2">
<h2></h2>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="co"># move the data directory to ~, our current directory</span>
<span class="bu">cd</span> ~
<span class="fu">mv</span> bioinfclass/data .
<span class="fu">ls</span>
<span class="fu">ls</span> data
<span class="fu">ls</span> bioinfclass</code></pre></div>
<p><strong>Exercise:</strong> How do we get <code>data</code> back? (click down for answer)</p>
</section>
<section id="getting-data-back" class="level2">
<h2>Getting data back</h2>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">mv</span> data bioinfclass</code></pre></div>
</section>
</section>
<section id="mv-is-for-renaming-as-well" class="level1">
<h1><code>mv</code> is for renaming as well</h1>
<section id="section-1" class="level2">
<h2></h2>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="co"># nothing to see here...</span>
<span class="fu">mv</span> bioinfclass catgifs
<span class="fu">ls</span></code></pre></div>
<p>Think of <code>mv</code> as updating the “path” stamp on a file</p>
<p><strong>Exercise:</strong> How do we get <code>bioinfclass</code> back? (down for answer)</p>
</section>
<section id="getting-bioinfclass-back" class="level2">
<h2>Getting bioinfclass back</h2>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">mv</span> catgifs bioinfclass</code></pre></div>
</section>
</section>
<section id="creating-and-removing-files" class="level1">
<h1>Creating and removing files</h1>
<p>We can make new directories or files with <code>mkdir</code> and <code>touch</code>, and remove with <code>rm</code> and <code>rmdir</code></p>
<ul>
<li><code>mkdir name</code> will create a directory <code>name</code></li>
<li><code>touch name</code> will create an empty file <code>name</code></li>
<li><code>rm name</code> will remove a file called <code>name</code></li>
<li><code>rmdir name</code> will remove an empty directory called <code>name</code></li>
<li><code>rm -r name</code> will remove <code>name</code> and all it’s contents (CAREFUL!)</li>
</ul>
<p><strong>Exercise:</strong> create a file called <code>birds</code> in a new subdirectory called <code>dinosaurs</code> of your home directory</p>
</section>
<section id="commands" class="level1">
<h1>Commands</h1>
<p><em>In which things take flight and evolve!</em></p>
</section>
<section id="anatomy-of-a-unix-command" class="level1">
<h1>Anatomy of a Unix command</h1>
<p><code>command [flags] [operands]</code></p>
<ul>
<li>command: name of the program</li>
<li>operands: positional based arguments
<ul>
<li>often but not always required</li>
</ul></li>
<li>flags: name based arguments
<ul>
<li>position frequently ignored</li>
<li>usually for controlling details of program behavior</li>
<li>often not required</li>
<li>sometimes take arguments, sometimes not</li>
</ul></li>
</ul>
</section>
<section id="more-on-flag-shape" class="level1">
<h1>More on flag shape</h1>
<ul>
<li>sometimes single dash and character (like <code>-h</code>)</li>
<li>sometimes double dash and word (like <code>--help</code>)</li>
<li>sometimes single dash and word (like <code>-help</code>; esp Java programs)</li>
<li>sometimes there are short and long options (like <code>-h</code> or <code>--help</code>)</li>
<li>sometimes short flags can be munged together (<code>-v -x -f the-file</code> == <code>-vxf the-file</code>)</li>
<li>occasional insanity like <code>dd if=somefile of=somenewfile</code></li>
</ul>
<p>Ultimately, programs can interpret their arguments however they choose! These are just conventions.</p>
</section>
<section id="sequence-alignments" class="level1">
<h1>Sequence alignments</h1>
<p>As genetic sequences evolve, base pairs can be added or dropped. Alignment tries to figure out where these insertion/deletion events happened so that individual sites in an alignment can be compared between sequences:</p>
<pre><code>AAGGCCTT AAGGCCTT
ACGGCCTT ==>> ACGGCCTT
AAGCCTA AAG-CCTA
ACGGCCTA ACGGCCTA</code></pre>
<p>Note: the <code>-</code> character in the alignment represents a putative deletion.</p>
</section>
<section id="making-a-sequence-alignment" class="level1">
<h1>Making a sequence alignment</h1>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="bu">cd</span> ~/bioinfclass
<span class="ex">muscle</span> -maxiters 2 -in data/sfv.fasta -out output/alignment.fasta
<span class="co"># Get bored waiting and want to work on something else...</span>
<span class="op"><</span><span class="ex">Ctrl-a</span> <span class="kw">|</span><span class="op">></span> <span class="co"># create new pane</span></code></pre></div>
</section>
<section id="programs-are-just-files" class="level1">
<h1>Programs are just files!</h1>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">which</span> tree
<span class="co"># -l: "long" list output; lots of info that we will ignore except for `x` presence/absence</span>
<span class="fu">ls</span> -l /usr/bin/tree
<span class="co"># super secret shortcut</span>
<span class="fu">ls</span> -l <span class="va">$(</span><span class="fu">which</span> tree<span class="va">)</span>
<span class="co"># Notice no x; not executable...</span>
<span class="fu">ls</span> -l README.md</code></pre></div>
</section>
<section id="what-else-is-in-usrbin" class="level1">
<h1>What else is in <code>/usr/bin</code>?</h1>
<p><code>ls /usr/bin</code> => LOTS OF STUFF!</p>
<p>This is one of the many places your shell looks for programs</p>
</section>
<section id="how-does-the-shell-know-where-to-look-for-programs" class="level1">
<h1>How does the shell know where to look for programs?</h1>
<p><code>echo $PATH</code> => Your <code>PATH</code> <em>environment variable</em>; a <code>:</code> separated list of places your shell looks for programs.</p>
<p>This can be edited if you want to let your shell know to look other places as well (see <code>.bashrc</code>)</p>
</section>
<section id="questions" class="level1">
<h1>Questions?</h1>
</section>
<section id="pipes-and-streams" class="level1">
<h1>Pipes and streams</h1>
<p><em>In which our constructions in flight don tongues and communicate!</em></p>
</section>
<section id="a-bit-about-std-files" class="level1">
<h1>A bit about <code>std</code> files</h1>
<p>There are three special files that programs often read from and write to by default to facilitate composability:</p>
<p><a href="figures/stdfiles.svg" target="_blank"><img src="figures/stdfiles.svg" height="300"></a></p>
<p>For the most part we’ll ignore <code>stderr</code>…</p>
</section>
<section id="by-default-stdout-gets-printed-to-the-screen" class="level1">
<h1>By default, stdout gets printed to the screen</h1>
<section id="but-we-can-redirect-stdout-to-a-file-using" class="level2">
<h2>But we can <em>redirect</em> stdout to a file using <code>></code></h2>
<p><code>Program</code> vs <code>Program > out-file</code></p>
<p><a href="figures/redirect.svg" target="_blank"><img src="figures/redirect.svg" height="300"></a></p>
</section>
</section>
<section id="example-of-redirection" class="level1">
<h1>Example of redirection</h1>
<p><code>FastTree -nt output/alignment.fasta</code></p>
<p>vs</p>
<p><code>FastTree -nt output/alignment.fasta > output/tree.nw</code></p>
<p> </p>
<p>[make sure you’re in <code>~/bioinfclass</code>, and if your shell can’t see FastTree, don’t forget to <code>module load intro-bio</code>!]</p>
</section>
<section id="we-can-also-stdout-of-one-command-to-stdin-of-another" class="level1">
<h1>We can also <code>|</code> <code>stdout</code> of one command to <code>stdin</code> of another</h1>
<p><code>Prog1 | Prog2</code>:</p>
<p><a href="figures/pipe.svg" target="_blank"><img src="figures/pipe.svg" height="300"></a></p>
</section>
<section id="example" class="level1">
<h1>Example</h1>
<p><code>column -t -s , data/sfv.csv | less -S</code></p>
<ul>
<li><code>column</code> (a program for working with tabular data) writes its output to <code>stdout</code>
<ul>
<li><code>-t</code> specifies tabs for output</li>
<li><code>-s ,</code> specifies command for separator</li>
</ul></li>
<li><code>less</code> reads from <code>/dev/stdin</code> if no file is specified
<ul>
<li><code>-S</code> specifies don’t wrap</li>
</ul></li>
</ul>
</section>
<section id="you-should-see-something-like-this" class="level1">
<h1>You should see something like this</h1>
<pre><code>sequence specimen species gene location ...
BGH150WBGag2 BGH150 human gag Charmaguria ...
BGH31WBGag2 BGH31 human gag Bormi ...
BGH150WBGag4 BGH150 human gag Charmaguria ...
BGH150WBGag8 BGH150 human gag Charmaguria ...
MBG103WBGag101 MBG103 monkey gag Narayanganj ...
BGH31WBGag3 BGH31 human gag Bormi ...
MBG103WBGag102 MBG103 monkey gag Narayanganj ...
BGH31WBGag1 BGH31 human gag Bormi ...
MBG103WBGag104 MBG103 monkey gag Narayanganj ...
MBG104WBGag101 MBG104 monkey gag Narayanganj ...
...</code></pre>
</section>
<section id="section-2" class="level1">
<h1></h1>
<p>Programs have to <em>know</em> to use stdin / stdout for this to work. Some conventions:</p>
<ul>
<li>Assume first argument is input, second output</li>
<li>If only one argument is specified, output to <code>stdout</code></li>
<li>If no arguments specified, input from <code>stdin</code>, output to <code>stdout</code></li>
<li>Sometimes there is no “output” argument; assume <code>stdout</code></li>
<li>Sometimes programs treat <code>-</code> as a shortcut for <code>stdin</code></li>
<li>When all else fails, you can read from & write to paths <code>/dev/stdout</code>, <code>/dev/stdin</code> directly</li>
</ul>
</section>
<section id="more-about-stdin" class="level1">
<h1>More about stdin</h1>
<section id="q-what-does-stdin-default-to" class="level2">
<h2>Q: What does stdin default to?</h2>
<p>(down for answer)</p>
</section>
<section id="q-what-does-stdin-default-to-1" class="level2">
<h2>Q: What does stdin default to?</h2>
<p>A: Your keyboard!</p>
</section>
</section>
<section id="reading-from-stdin" class="level1">
<h1>Reading from stdin</h1>
<p>Recall that cat writes to stdout whatever files you pass it as args. If no files are specified though, it reads from stdin.</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">cat</span></code></pre></div>
<ul>
<li>Enter some text using your keyboard</li>
<li>Use the key command <code>Ctrl-d</code> to close the stdin file</li>
<li><code>cat</code> will print out whatever you typed in</li>
</ul>
</section>
<section id="the-shell-reads-from-stdin" class="level1">
<h1>The shell reads from stdin!</h1>
<p>Your bash shell works very much like cat:</p>
<ul>
<li><strong>R</strong>ead line of <code>stdin</code> file</li>
<li><strong>E</strong>valuate that line</li>
<li><strong>P</strong>rint <code>stdout</code> to screen</li>
<li><strong>L</strong>oop back and start over</li>
</ul>
<p>Note: this is why Ctrl-d works to quit the shell! You’re literally closing the stdin file that the shell <strong>REPL</strong> is reading from.</p>
</section>
<section id="thats-it-for-pipes-and-streams" class="level1">
<h1>That’s it for pipes and streams</h1>
<p>Questions?</p>
</section>
<section id="practicum" class="level1">
<h1>Practicum!</h1>
<p><em>In which we apply our knowledge of these flying, speaking beasts</em></p>
</section>
<section id="navigating-data" class="level1">
<h1>Navigating data</h1>
<p>We’ll start with some more basic examples using piping to more effectively look through our data.</p>
</section>
<section id="head-first" class="level1">
<h1>Head first…</h1>
<p>Head writes to <code>stdout</code> the first several lines of <code>stdin</code>.</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">head</span> data/sfv.csv
<span class="ex">column</span> -t -s , data/sfv.csv <span class="kw">|</span> <span class="fu">head</span></code></pre></div>
<p>This is useful for looking at the very top – or <em>head</em> – of big files (particularly CSV; -> header + a few rows).</p>
</section>
<section id="looking-at-alignments" class="level1">
<h1>Looking at alignments</h1>
<p>It’s important to always check your alignments to make sure they look “reasonable” (more or less, look like a plausable history, not completely “random”).</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="co"># We'll use the `alnvu` program</span>
<span class="ex">av</span> -L 99999 -w 99999 -c output/alignment.fasta <span class="kw">|</span> <span class="fu">less</span> -S</code></pre></div>
<p><code>av</code> is an <em>alignment viewer</em> which – with these settings – prints the differences from <em>consensus</em> for each sequence, making it easy to scan.</p>
</section>
<section id="we-can-also-look-at-our-tree" class="level1">
<h1>We can also look at our tree</h1>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="ex">nw_display</span> output/tree.nw <span class="kw">|</span> <span class="fu">less</span> -S</code></pre></div>
</section>
<section id="analyzing-data" class="level1">
<h1>Analyzing data</h1>
<p>In these examples we’ll be combinging several shell commands using piping in order to perform multistep computations, effectively making the shell into a programming language.</p>
</section>
<section id="side-note-strategy" class="level1">
<h1>Side note: strategy</h1>
<p>The way to work through and understand these examples:</p>
<ul>
<li>run the first part of the command and think about what it’s doing</li>
<li>add the next part of the pipe to see what that does</li>
<li>repeat, till through the whole command</li>
<li>reflect and think about the strategy used to put each of these pieces together towards the end product</li>
</ul>
<p>This is analogous to how we typically write these computations, breaking a computation down into steps corresponding to known shell commands, one at a time.</p>
</section>
<section id="sequences-per-species" class="level1">
<h1>Sequences per species</h1>
<p>There is a species column in our data set with <code>human</code> and <code>monkey</code> entries.</p>
<p><br/></p>
<p>Question: How would we figure out how many human and monkey sequences there are in the data?</p>
</section>
<section id="sequences-per-species-1" class="level1">
<h1>Sequences per species</h1>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="co"># How many human sequences are there?</span>
<span class="fu">grep</span> human data/sfv.csv <span class="kw">|</span> <span class="fu">wc</span> -l
<span class="co"># What about monkey sequences?</span>
<span class="fu">grep</span> monkey data/sfv.csv <span class="kw">|</span> <span class="fu">wc</span> -l</code></pre></div>
<ul>
<li><code>grep</code> searches through files for a pattern</li>
<li><code>wc -l</code> counts the number of lines</li>
<li>Thus, <code>grep ... | wc -l</code> counts the number of lines in a file matching some pattern</li>
</ul>
<p>There’s one line per sequence, so this gives us what we’re after.</p>
</section>
<section id="what-are-some-problems-with-this" class="level1">
<h1>What are some problems with this?</h1>
</section>
<section id="some-problems-with-this" class="level1">
<h1>Some problems with this</h1>
<p>We’re not accounting for the tabular structure:</p>
<ul>
<li>We can’t restrict matches to just a particular column in the data</li>
<li>We have to do this twice; if we had many categories, this could take forever</li>
</ul>
</section>
<section id="a-better-way" class="level1">
<h1>A better way</h1>
<p>How many sequences are there for each species?</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">cut</span> -d , -f 3 data/sfv.csv <span class="kw">|</span> <span class="fu">sort</span> <span class="kw">|</span> <span class="fu">uniq</span> -c</code></pre></div>
<ul>
<li><code>cut -d , -f 3</code> extracts the third <code>,</code> separated entry for each line</li>
<li><code>sort | uniq -c</code> counts occurrences for each unique entry</li>
</ul>
<p>The number of times <code>"monkey"</code> shows up in the count then will be the number of sequences taking that value in the species column.</p>
</section>
<section id="how-to-visualize-this" class="level1">
<h1>How to visualize this</h1>
<p>The first few columns in the data</p>
<pre><code>|-----------------+----------+---------+------+-------------|
| sequence | specimen | species | gene | location |
|-----------------+----------+---------+------+-------------|
| BGH150WBGag2 | BGH150 | human | gag | Charmaguria |
| BGH31WBGag2 | BGH31 | human | gag | Bormi |
| BGH150WBGag4 | BGH150 | human | gag | Charmaguria |
| BGH150WBGag8 | BGH150 | human | gag | Charmaguria |
| MBG103WBGag101 | MBG103 | monkey | gag | Narayanganj |
| BGH31WBGag3 | BGH31 | human | gag | Bormi |
| MBG103WBGag102 | MBG103 | monkey | gag | Narayanganj |
| BGH31WBGag1 | BGH31 | human | gag | Bormi |
| MBG103WBGag104 | MBG103 | monkey | gag | Narayanganj |
| MBG104WBGag101 | MBG104 | monkey | gag | Narayanganj |
| ...</code></pre>
</section>
<section id="the-results-of-cutting-out-the-third-column" class="level1">
<h1>The results of cutting out the third column</h1>
<p><code>cut -d , -f 3 data/sfv.csv</code></p>
<pre><code>|---------|
| species |
|---------|
| human |
| human |
| human |
| human |
| monkey |
| human |
| monkey |
| human |
| monkey |
| monkey |
| ...</code></pre>
</section>
<section id="the-result-of-sorting" class="level1">
<h1>The result of sorting</h1>
<p><code>cut -d , -f 3 data/sfv.csv | sort</code></p>
<pre><code>|---------|
| species |
|---------|
| human |
| human |
| human |
| human |
| human |
| human |
| ...
| monkey |
| monkey |
| monkey |
| monkey |
| ...</code></pre>
</section>
<section id="the-result-of-counting" class="level1">
<h1>The result of counting</h1>
<p><code>cut -d , -f 3 data/sfv.csv | sort | uniq -c</code></p>
<pre><code> 77 human
1097 monkey
1 species</code></pre>
</section>
<section id="how-would-we-count-how-many-specimens-there-are-per-species" class="level1">
<h1>How would we count how many specimens there are per species?</h1>
</section>
<section id="counting-specimens-per-species" class="level1">
<h1>Counting specimens per species</h1>
<p>We can do this with a two step version of the <code>cut | uniq | sort</code> pattern we used above:</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="co"># First, uniq specimens, with species info</span>
<span class="fu">cut</span> -d , -f 2,3 data/sfv.csv <span class="kw">|</span> <span class="fu">sort</span> <span class="kw">|</span> <span class="fu">uniq</span> <span class="kw">|</span> <span class="fu">head</span>
<span class="co"># Next count that by species</span>
<span class="fu">cut</span> -d , -f 2,3 data/sfv.csv <span class="kw">|</span> <span class="fu">sort</span> <span class="kw">|</span> <span class="fu">uniq</span> <span class="kw">|</span> <span class="fu">cut</span> -d , -f 2 <span class="kw">|</span> <span class="fu">sort</span> <span class="kw">|</span> <span class="fu">uniq</span> -c</code></pre></div>
</section>
<section id="columns-2-and-3-cut-out-of-data" class="level1">
<h1>Columns 2 and 3 cut out of data</h1>
<p><code>cut -d , -f 2,3 data/sfv.csv</code></p>
<pre><code>|----------+---------|
| specimen | species |
|----------+---------|
| BGH150 | human |
| BGH31 | human |
| BGH150 | human |
| BGH150 | human |
| MBG103 | monkey |
| BGH31 | human |
| MBG103 | monkey |
| BGH31 | human |
| MBG103 | monkey |
| MBG104 | monkey |
| ...</code></pre>
</section>
<section id="the-results-of-the-first-sort-and-uniq-without--c" class="level1">
<h1>The results of the first sort and uniq (without <code>-c</code>)</h1>
<p><code>cut -d , -f 2,3 data/sfv.csv | sort | uniq</code></p>
<pre><code>|----------+---------|
| specimen | species |
|----------+---------|
| BGH150 | human |
| BGH31 | human |
| MBG103 | monkey |
| MBG104 | monkey |
| ...</code></pre>
<p>Now we now have exactly 1 row per specimen (species just “comes along for the ride”).</p>
</section>
<section id="now-we-cut-out-the-species-and-count" class="level1">
<h1>Now we cut out the species, and count</h1>
<p><code>cut -d , -f 2,3 data/sfv.csv | sort | uniq | cut -d , -f 2 | sort | uniq -c</code></p>
<pre><code> 8 human
169 monkey
1 species</code></pre>
<p>Since there’s 1 row per specimen in the previous result, counting these rows by species gives us the number of specimens per species.</p>
</section>
<section id="what-if-we-wanted-to-count-number-of-specimens-per-species-for-each-location" class="level1">
<h1>What if we wanted to count number of specimens per species for each <em>location</em>?</h1>
<p>What would we do?</p>
</section>
<section id="specimens-by-location-and-species" class="level1">
<h1>Specimens by location and species</h1>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">cut</span> -d , -f 2,3,5 data/sfv.csv <span class="kw">|</span> <span class="fu">sort</span> <span class="kw">|</span> <span class="fu">uniq</span> <span class="kw">|</span> <span class="fu">cut</span> -d , -f 2,3 <span class="kw">|</span> <span class="fu">sort</span> <span class="kw">|</span> <span class="fu">uniq</span> -c</code></pre></div>
</section>
<section id="look-at-these-numbers" class="level1">
<h1>Look at these numbers</h1>
<p>What do you notice about where we tend to see humans getting infected?</p>
</section>
<section id="moral-of-the-story" class="level1">
<h1>Moral of the story</h1>
<p>Small composable tools offer a lot of power and flexibility.</p>
<p>From very simple pieces we were able to compute answers to very nontrivial queries. This is the promise of Unix composability, and why we love Unix for bioinformatics.</p>
</section>
<section id="is-anyone-else-getting-tired-of-all-this--d--f-235-business" class="level1">
<h1>Is anyone else getting tired of all this <code>-d , -f 2,3,5</code> business?</h1>
<section id="enter-csvkit" class="level2">
<h2>Enter <code>csvkit</code></h2>
<p>We have <code>csv...</code> versions of many of the standard Unix utils:</p>
<p><code>csvcut</code>, <code>csvgrep</code>, <code>csvjoin</code>, <code>csvlook</code>, <code>csvsort</code>, <code>csvstack</code>, <code>csvstat</code></p>
</section>
</section>
<section id="advantages-of-csvkit" class="level1">
<h1>Advantages of csvkit</h1>
<ul>
<li>Can use header names, not just indices</li>
<li>Don’t have to specify <code>-s ,</code>/<code>-d ,</code> every time</li>
<li>Headers are treated separately from the data</li>
</ul>
</section>
<section id="looking-at-just-the-human-sequences" class="level1">
<h1>Looking at just the human sequences</h1>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="ex">csvgrep</span> -c species -m human data/sfv.csv <span class="kw">|</span> <span class="ex">csvlook</span> <span class="kw">|</span> <span class="fu">less</span> -S</code></pre></div>
</section>
<section id="the-one-missing-piece-csvuniq" class="level1">
<h1>The one missing piece: csvuniq</h1>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="co"># ~/bin is one of the places we can put programs</span>
<span class="fu">mkdir</span> ~/bin
<span class="co"># Download script, and mark as executable</span>
<span class="fu">wget</span> https://goo.gl/ZdNoUL -O ~/bin/csvuniq
<span class="fu">chmod</span> +x ~/bin/csvuniq
<span class="co"># Test with help message</span>
<span class="ex">csvuniq</span> -h</code></pre></div>
<p>(if this fails to work, try <code>export PATH=~/bin:$PATH</code> and <code>module load intro-bio</code>)</p>
</section>
<section id="now-lets-see-what-our-last-example-looks-like" class="level1">
<h1>Now let’s see what our last example looks like:</h1>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="ex">csvuniq</span> -c specimen,species,location data/sfv.csv <span class="kw">|</span> <span class="ex">csvuniq</span> -zc species,location <span class="kw">|</span> <span class="ex">csvlook</span></code></pre></div>
<p>Notice that <code>csvuniq</code> does all of the sorting and cutting for us…</p>
</section>
<section id="homework" class="level1">
<h1>Homework</h1>
<p>For homework go over these examples, and translate into the equivalents using <code>csvkit</code> utils.</p>
<p>Recommended reading:</p>
<ul>
<li>Chapter 3, and chapter 7 (till around page 148).</li>
</ul>
<p>Reading for next class (if you want to read ahead):</p>
<ul>
<li>Chapter 12</li>
</ul>
</section>
<section id="resources" class="level1">
<h1>Resources</h1>
<ul>
<li>Unix command reference: <a href="https://ubuntudanmark.dk/filer/fwunixref.pdf" class="uri">https://ubuntudanmark.dk/filer/fwunixref.pdf</a></li>
<li><a href="https://www.codecademy.com/learn/learn-the-command-line">Codecademy Command line course</a></li>
</ul>
<p><a href="http://fredhutchio.github.io/intro-bioinformatics">Back to homepage</a></p>
</section>
</div>
<script src="reveal.js/lib/js/head.min.js"></script>
<script src="reveal.js/js/reveal.min.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: false,
slideNumber: true,
rollingLinks: false,
// available themes are in /css/theme
theme: Reveal.getQueryHash().theme || 'csmall',
// default/cube/page/concave/zoom/linear/fade/none
transition: Reveal.getQueryHash().transition || 'fade',
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'reveal.js/lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'reveal.js/plugin/markdown/showdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'reveal.js/plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'reveal.js/plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'reveal.js/plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'MathJax/MathJax.js?config=TeX-AMS-MML_HTMLorMML'}
// { src: 'reveal.js/plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }
// { src: 'reveal.js/plugin/remotes/remotes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
Reveal.addEventListener( 'slidechanged', function( event ) {
MathJax.Hub.Rerender();
} );
</script>
</body>
</html>