-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path07-python.html
931 lines (842 loc) · 51.1 KB
/
07-python.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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
<!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 7 - Writing Python programs" /> -->
<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 7 - Writing Python programs</h3>
<p>
<h4></h4>
</p>
</section>
<section id="writing-your-own-program" class="level1">
<h1>Writing your own program</h1>
<p><em>Wherein with our knowledge we take flight and soar through the clouds of logic</em></p>
</section>
<section id="today" class="level1">
<h1>Today</h1>
<ul>
<li>Dive head first into writing a python script to compute something interesting</li>
<li>Hook this up to our bash pipeline</li>
<li>Talk about some higher level things to keep in mind when writing programs</li>
<li>Reflect on everything we’ve learned, and think again about the “big picture”</li>
</ul>
</section>
<section id="a-biological-question" class="level1">
<h1>A biological question</h1>
<p>In one of our Unix classes we revealed there seems to be a much more human infection in Bormi. We’d like to know why.</p>
<p>Hypothesis: A more diverse viral population is responsible for the higher incidence of human infection.</p>
</section>
<section id="objective" class="level1">
<h1>Objective</h1>
<p>Let’s write a script that:</p>
<ul>
<li>Takes a newick tree file</li>
<li>Computes the average distance between sequences/tree-tips</li>
<li>Prints to stdout</li>
<li>Takes an optional argument to return data in csv format</li>
<li>Takes another optional argument to add a location to the csv output</li>
</ul>
<p>We’ll hook this into our pipeline to compare the viral diversity between locations.</p>
</section>
<section id="starting-things-off" class="level1">
<h1>Starting things off</h1>
<p>Go to your project directory and create a new file at <code>scripts/treedists.py</code> using <code>vim</code>:</p>
<pre><code>tmux attach
cd ~/bioinfclass
vim scripts/treedists.py</code></pre>
</section>
<section id="python-shebang" class="level1">
<h1>Python shebang</h1>
<p>When vim starts, insert the following shebang:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">#!/usr/bin/env python</span></code></pre></div>
<p>This tells the shell to use whatever python environment is currently active. This means our <code>intro-bio</code> modules will be available to us.</p>
</section>
<section id="setting-up-as-executable-tmux-panes" class="level1">
<h1>Setting up as executable (+ tmux <em>panes</em>)</h1>
<p>Save the file. Then Create and switch to a new tmux <em>pane</em> with <code>Ctrl-a |</code>, and enter <code>cd ~/bioinfclass</code> into the new bash prompt.</p>
<p>In the bash shell type <code>chmod +x scripts/treedists.py</code> to mark our file as executable.</p>
<p>Use <code>Ctrl-a →</code> to switch back and forth between panes.</p>
</section>
<section id="command-line-arguments" class="level1">
<h1>Command line arguments</h1>
<p>In vim, add the following after our shebang:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">import</span> argparse
<span class="kw">def</span> get_args():
parser <span class="op">=</span> argparse.ArgumentParser()
<span class="co"># add parsing rules</span>
parser.add_argument(<span class="st">"treefile"</span>)
<span class="cf">return</span> parser.parse_args()
args <span class="op">=</span> get_args()
<span class="bu">print</span> <span class="st">"args is:"</span>, args
<span class="bu">print</span> <span class="st">"treefile is:"</span>, args.treefile</code></pre></div>
<p>Try running with <code>./scripts/treedists.py sometreefile</code>.</p>
</section>
<section id="setting-up-a-main-function" class="level1">
<h1>Setting up a <code>main</code> function</h1>
<section id="section" class="level2">
<h2></h2>
<p>This is a common pattern in many languages. A main function is a place for putting all of your “top-level” program logic.</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> main():
args <span class="op">=</span> get_args()
<span class="bu">print</span> <span class="st">"args is:"</span>, args
<span class="bu">print</span> <span class="st">"treefile is:"</span>, args.treefile
<span class="co"># This only gets run if the file is being run as a script</span>
<span class="cf">if</span> <span class="va">__name__</span> <span class="op">==</span> <span class="st">'__main__'</span>:
main()</code></pre></div>
<p>This let’s us <code>import</code> the file from another program as a module, without executing the <code>main</code> function.</p>
</section>
<section id="whole-file" class="level2">
<h2>Whole file</h2>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">import</span> argparse
<span class="kw">def</span> get_args():
parser <span class="op">=</span> argparse.ArgumentParser()
<span class="co"># add parsing rules</span>
parser.add_argument(<span class="st">"treefile"</span>)
<span class="cf">return</span> parser.parse_args()
<span class="kw">def</span> main():
args <span class="op">=</span> get_args()
<span class="bu">print</span> <span class="st">"args is:"</span>, args
<span class="bu">print</span> <span class="st">"treefile is:"</span>, args.treefile
<span class="co"># This only gets run if the file is being run as a script</span>
<span class="cf">if</span> <span class="va">__name__</span> <span class="op">==</span> <span class="st">'__main__'</span>:
main()</code></pre></div>
</section>
</section>
<section id="loading-our-data" class="level1">
<h1>Loading our data</h1>
<section id="section-1" class="level2">
<h2></h2>
<p>Let’s start by setting up our main function to load the tree data. Edit the main function to look like this:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># At the top below other imports, add:</span>
<span class="im">from</span> Bio <span class="im">import</span> Phylo
<span class="kw">def</span> main():
args <span class="op">=</span> get_args()
<span class="co"># parse the tree file and return a generator</span>
trees <span class="op">=</span> Phylo.parse(args.treefile, <span class="st">'newick'</span>)
tree <span class="op">=</span> trees.<span class="bu">next</span>()
<span class="co"># For now, just print</span>
<span class="bu">print</span> tree</code></pre></div>
<p>Save and run <code>./scripts/treedists.py output/Karamjal/tree.nw</code> You’ll see an object representation of the <a href="https://en.wikipedia.org/wiki/Clade">clades</a> on the phylogenetic tree.</p>
</section>
<section id="whole-file-1" class="level2">
<h2>Whole file</h2>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">import</span> argparse
<span class="im">from</span> Bio <span class="im">import</span> Phylo
<span class="kw">def</span> get_args():
parser <span class="op">=</span> argparse.ArgumentParser()
<span class="co"># add parsing rules</span>
parser.add_argument(<span class="st">"treefile"</span>)
<span class="cf">return</span> parser.parse_args()
<span class="kw">def</span> main():
args <span class="op">=</span> get_args()
<span class="co"># parse the tree file and return a generator</span>
trees <span class="op">=</span> Phylo.parse(args.treefile, <span class="st">'newick'</span>)
tree <span class="op">=</span> trees.<span class="bu">next</span>()
<span class="co"># For now, just print</span>
<span class="bu">print</span> tree
<span class="co"># This only gets run if the file is being run as a script</span>
<span class="cf">if</span> <span class="va">__name__</span> <span class="op">==</span> <span class="st">'__main__'</span>:
main()</code></pre></div>
</section>
</section>
<section id="quick-note-on-clades" class="level1">
<h1>Quick note on clades</h1>
<p>A clade on a phylogenetic tree is some node in the tree together with all of it’s decendants.</p>
<p>Each tip forms a clade, and where two tips join, we have a clade, and where that joins we have a clade, etc, all the way to the root. Thus, we can think of the entire tree structure in terms of clades (and <code>Bio.Phylo</code> does this).</p>
</section>
<section id="we-want-average-distance-between-the-tips-of-this-tree" class="level1">
<h1>We want average distance between the tips of this tree</h1>
<p>How should we do this?</p>
</section>
<section id="what-to-do-with-the-tree" class="level1">
<h1>What to do with the tree</h1>
<p>Strategy:</p>
<ul>
<li>Find all tree tips</li>
<li>For each pair of distinct tree tips:
<ul>
<li>Find the distance through the tree between those tree tips</li>
</ul></li>
<li>Average all such distances</li>
</ul>
</section>
<section id="repl-driven-development" class="level1">
<h1>REPL driven development</h1>
<p>Switch to the bash pane (<code>Ctrl-a →</code>), then open a new <em>vertical</em> pane with <code>Ctrl-a -</code>, and <code>cd ~/bioinfclass</code>. Next, start a python REPL (<code>python</code>):</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">from</span> Bio <span class="im">import</span> Phylo
<span class="co"># This returns an generator of trees, so we'll use next to get the first (and only)</span>
trees <span class="op">=</span> Phylo.parse(<span class="st">'output/tree.nw'</span>, <span class="st">'newick'</span>)
t <span class="op">=</span> trees.<span class="bu">next</span>()
<span class="co"># Inspect attributes</span>
<span class="bu">dir</span>(t)</code></pre></div>
<p>Question: How do we get the tips of the tree?</p>
</section>
<section id="what-do-the-terminals-look-like" class="level1">
<h1>What do the terminals look like?</h1>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">terminals <span class="op">=</span> t.get_terminals()
<span class="bu">print</span> terminals
<span class="co"># Let's see what the first of these looks like</span>
tip <span class="op">=</span> terminals[<span class="dv">0</span>]
<span class="bu">dir</span>(tip)</code></pre></div>
<p>How do we get the name?</p>
</section>
<section id="getting-the-name" class="level1">
<h1>Getting the name</h1>
<p>Yup, you guessed it…</p>
<pre class="pythyon"><code>tip.name</code></pre>
</section>
<section id="how-do-we-get-a-list-of-all-names" class="level1">
<h1>How do we get a list of all names?</h1>
<p>We have a list of tips.</p>
<p>How do we get a list of all the tips names?</p>
</section>
<section id="list-of-all-names" class="level1">
<h1>List of all names</h1>
<p>Let’s use list comprehension:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">tipnames <span class="op">=</span> [tip.name <span class="cf">for</span> tip <span class="kw">in</span> terminals]</code></pre></div>
</section>
<section id="what-about-all-pairs-of-names" class="level1">
<h1>What about all pairs of names?</h1>
<p>(Note that we don’t want duplicate pairs like <code>["seq1", "seq2"]</code> and <code>["seq2", "seq1"]</code>, and also don’t need to count <code>["seq1", "seq1"]</code>)</p>
</section>
<section id="all-pairs-of-names" class="level1">
<h1>All pairs of names</h1>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">name_pairs <span class="op">=</span> <span class="bu">set</span>()
<span class="cf">for</span> tip1 <span class="kw">in</span> tipnames:
<span class="cf">for</span> tip2 <span class="kw">in</span> tipnames:
<span class="cf">if</span> tip1 <span class="op">!=</span> tip2:
pair <span class="op">=</span> [tip1, tip2]
<span class="co"># So we don't have duplicates</span>
sorted_pair <span class="op">=</span> <span class="bu">sorted</span>(pair)
tuple_pair <span class="op">=</span> <span class="bu">tuple</span>(sorted_pair)
name_pairs.add(tuple_pair)
<span class="bu">list</span>(name_pairs)[<span class="dv">0</span>:<span class="dv">10</span>]
<span class="bu">len</span>(name_pairs)</code></pre></div>
</section>
<section id="lets-make-a-function-of-this" class="level1">
<h1>Let’s make a function of this!</h1>
<section id="section-2" class="level2">
<h2></h2>
<p>This is a good idea when you have a bunch of modular logic.</p>
<p>Back in our file (<code>Ctrl-a →</code>):</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> unique_pairs(xs):
pairs <span class="op">=</span> <span class="bu">set</span>()
<span class="cf">for</span> x1 <span class="kw">in</span> xs:
<span class="cf">for</span> x2 <span class="kw">in</span> xs:
<span class="cf">if</span> x1 <span class="op">!=</span> x2:
pair <span class="op">=</span> [x1, x2]
pairs.add(<span class="bu">tuple</span>(<span class="bu">sorted</span>(pair)))
<span class="cf">return</span> <span class="bu">list</span>(pairs)</code></pre></div>
</section>
<section id="whole-file-2" class="level2">
<h2>Whole file</h2>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">import</span> argparse
<span class="im">from</span> Bio <span class="im">import</span> Phylo
<span class="kw">def</span> get_args():
parser <span class="op">=</span> argparse.ArgumentParser()
<span class="co"># add parsing rules</span>
parser.add_argument(<span class="st">"treefile"</span>)
<span class="cf">return</span> parser.parse_args()
<span class="kw">def</span> unique_pairs(xs):
pairs <span class="op">=</span> <span class="bu">set</span>()
<span class="cf">for</span> x1 <span class="kw">in</span> xs:
<span class="cf">for</span> x2 <span class="kw">in</span> xs:
<span class="cf">if</span> x1 <span class="op">!=</span> x2:
pair <span class="op">=</span> [x1, x2]
pairs.add(<span class="bu">tuple</span>(<span class="bu">sorted</span>(pair)))
<span class="cf">return</span> <span class="bu">list</span>(pairs)
<span class="kw">def</span> main():
args <span class="op">=</span> get_args()
<span class="co"># parse the tree file and return a generator</span>
trees <span class="op">=</span> Phylo.parse(args.treefile, <span class="st">'newick'</span>)
tree <span class="op">=</span> trees.<span class="bu">next</span>()
<span class="co"># For now, just print</span>
<span class="bu">print</span> tree
<span class="co"># This only gets run if the file is being run as a script</span>
<span class="cf">if</span> <span class="va">__name__</span> <span class="op">==</span> <span class="st">'__main__'</span>:
main()</code></pre></div>
</section>
</section>
<section id="how-about-finding-distance" class="level1">
<h1>How about finding distance?</h1>
<p>Back to the REPL (<code>Ctrl-a →</code> for over, and <code>Ctrl-a ↑</code> for up/down):</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># What function should we try here?</span>
<span class="bu">dir</span>(t)</code></pre></div>
</section>
<section id="finding-distance" class="level1">
<h1>Finding distance</h1>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">name_pair <span class="op">=</span> <span class="bu">list</span>(name_pairs)[<span class="dv">0</span>]
<span class="co"># We can assign multiple variables at once :-)</span>
<span class="co"># This is called "destructuring"</span>
t1, t2 <span class="op">=</span> name_pair
t.distance(t1, t2)</code></pre></div>
</section>
<section id="putting-it-all-together" class="level1">
<h1>Putting it all together</h1>
<section id="section-3" class="level2">
<h2></h2>
<p>Back in our <code>treedist.py</code> file:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> average_distance(tree):
tipnames <span class="op">=</span> [tip.name <span class="cf">for</span> tip <span class="kw">in</span> tree.get_terminals()]
tipname_pairs <span class="op">=</span> unique_pairs(tipnames)
tip_dists <span class="op">=</span> [tree.distance(t1, t2) <span class="cf">for</span> t1, t2 <span class="kw">in</span> tipname_pairs]
<span class="cf">return</span> <span class="bu">sum</span>(tip_dists) <span class="op">/</span> <span class="bu">len</span>(tip_dists)</code></pre></div>
</section>
<section id="whole-file-3" class="level2">
<h2>Whole file</h2>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">import</span> argparse
<span class="im">from</span> Bio <span class="im">import</span> Phylo
<span class="kw">def</span> get_args():
parser <span class="op">=</span> argparse.ArgumentParser()
<span class="co"># add parsing rules</span>
parser.add_argument(<span class="st">"treefile"</span>)
<span class="cf">return</span> parser.parse_args()
<span class="kw">def</span> unique_pairs(xs):
pairs <span class="op">=</span> <span class="bu">set</span>()
<span class="cf">for</span> x1 <span class="kw">in</span> xs:
<span class="cf">for</span> x2 <span class="kw">in</span> xs:
<span class="cf">if</span> x1 <span class="op">!=</span> x2:
pair <span class="op">=</span> [x1, x2]
pairs.add(<span class="bu">tuple</span>(<span class="bu">sorted</span>(pair)))
<span class="cf">return</span> <span class="bu">list</span>(pairs)
<span class="kw">def</span> average_distance(tree):
tipnames <span class="op">=</span> [tip.name <span class="cf">for</span> tip <span class="kw">in</span> tree.get_terminals()]
tipname_pairs <span class="op">=</span> unique_pairs(tipnames)
tip_dists <span class="op">=</span> [tree.distance(t1, t2) <span class="cf">for</span> t1, t2 <span class="kw">in</span> tipname_pairs]
<span class="cf">return</span> <span class="bu">sum</span>(tip_dists) <span class="op">/</span> <span class="bu">len</span>(tip_dists)
<span class="kw">def</span> main():
args <span class="op">=</span> get_args()
<span class="co"># parse the tree file and return a generator</span>
trees <span class="op">=</span> Phylo.parse(args.treefile, <span class="st">'newick'</span>)
tree <span class="op">=</span> trees.<span class="bu">next</span>()
<span class="co"># For now, just print</span>
<span class="bu">print</span> tree
<span class="co"># This only gets run if the file is being run as a script</span>
<span class="cf">if</span> <span class="va">__name__</span> <span class="op">==</span> <span class="st">'__main__'</span>:
main()</code></pre></div>
</section>
</section>
<section id="hooking-this-up-in-main" class="level1">
<h1>Hooking this up in <code>main</code></h1>
<section id="section-4" class="level2">
<h2></h2>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> main():
args <span class="op">=</span> get_args()
trees <span class="op">=</span> Phylo.parse(args.treefile, <span class="st">'newick'</span>)
tree <span class="op">=</span> trees.<span class="bu">next</span>()
<span class="bu">print</span> <span class="st">"Average distance:"</span>, average_distance(tree)</code></pre></div>
</section>
<section id="whole-file-4" class="level2">
<h2>Whole file</h2>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">import</span> argparse
<span class="im">from</span> Bio <span class="im">import</span> Phylo
<span class="kw">def</span> get_args():
parser <span class="op">=</span> argparse.ArgumentParser()
<span class="co"># add parsing rules</span>
parser.add_argument(<span class="st">"treefile"</span>)
<span class="cf">return</span> parser.parse_args()
<span class="kw">def</span> unique_pairs(xs):
pairs <span class="op">=</span> <span class="bu">set</span>()
<span class="cf">for</span> x1 <span class="kw">in</span> xs:
<span class="cf">for</span> x2 <span class="kw">in</span> xs:
<span class="cf">if</span> x1 <span class="op">!=</span> x2:
pair <span class="op">=</span> [x1, x2]
pairs.add(<span class="bu">tuple</span>(<span class="bu">sorted</span>(pair)))
<span class="cf">return</span> <span class="bu">list</span>(pairs)
<span class="kw">def</span> average_distance(tree):
tipnames <span class="op">=</span> [tip.name <span class="cf">for</span> tip <span class="kw">in</span> tree.get_terminals()]
tipname_pairs <span class="op">=</span> unique_pairs(tipnames)
tip_dists <span class="op">=</span> [tree.distance(t1, t2) <span class="cf">for</span> t1, t2 <span class="kw">in</span> tipname_pairs]
<span class="cf">return</span> <span class="bu">sum</span>(tip_dists) <span class="op">/</span> <span class="bu">len</span>(tip_dists)
<span class="kw">def</span> main():
args <span class="op">=</span> get_args()
<span class="co"># parse the tree file and return a generator</span>
trees <span class="op">=</span> Phylo.parse(args.treefile, <span class="st">'newick'</span>)
tree <span class="op">=</span> trees.<span class="bu">next</span>()
<span class="co"># For now, just print</span>
<span class="bu">print</span> <span class="st">"Average distance:"</span>, average_distance(tree)
<span class="co"># This only gets run if the file is being run as a script</span>
<span class="cf">if</span> <span class="va">__name__</span> <span class="op">==</span> <span class="st">'__main__'</span>:
main()</code></pre></div>
</section>
</section>
<section id="lets-test" class="level1">
<h1>Let’s test</h1>
<p>Save, then go back to the bash pane and try running again:</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="ex">./scripts/treedists.py</span> output/Karamjal/tree.nw</code></pre></div>
</section>
<section id="adding-a---csv-output-option" class="level1">
<h1>Adding a <code>--csv</code> output option</h1>
<section id="section-5" class="level2">
<h2></h2>
<p>This will let us do a <code>csvstack</code> to get a single data set.</p>
<p>First the arguments (back in <code>treedists.py</code> file):</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> get_args():
parser <span class="op">=</span> argparse.ArgumentParser()
parser.add_argument(<span class="st">"treefile"</span>)
<span class="co"># Add optional args; store_true makes this a toggle</span>
parser.add_argument(<span class="st">"--csv"</span>, action<span class="op">=</span><span class="st">"store_true"</span>)
<span class="cf">return</span> parser.parse_args()</code></pre></div>
</section>
<section id="whole-file-5" class="level2">
<h2>Whole file</h2>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">import</span> argparse
<span class="im">from</span> Bio <span class="im">import</span> Phylo
<span class="kw">def</span> get_args():
parser <span class="op">=</span> argparse.ArgumentParser()
<span class="co"># add parsing rules</span>
parser.add_argument(<span class="st">"treefile"</span>)
parser.add_argument(<span class="st">"--csv"</span>, action<span class="op">=</span><span class="st">"store_true"</span>)
<span class="cf">return</span> parser.parse_args()
<span class="kw">def</span> unique_pairs(xs):
pairs <span class="op">=</span> <span class="bu">set</span>()
<span class="cf">for</span> x1 <span class="kw">in</span> xs:
<span class="cf">for</span> x2 <span class="kw">in</span> xs:
<span class="cf">if</span> x1 <span class="op">!=</span> x2:
pair <span class="op">=</span> [x1, x2]
pairs.add(<span class="bu">tuple</span>(<span class="bu">sorted</span>(pair)))
<span class="cf">return</span> <span class="bu">list</span>(pairs)
<span class="kw">def</span> average_distance(tree):
tipnames <span class="op">=</span> [tip.name <span class="cf">for</span> tip <span class="kw">in</span> tree.get_terminals()]
tipname_pairs <span class="op">=</span> unique_pairs(tipnames)
tip_dists <span class="op">=</span> [tree.distance(t1, t2) <span class="cf">for</span> t1, t2 <span class="kw">in</span> tipname_pairs]
<span class="cf">return</span> <span class="bu">sum</span>(tip_dists) <span class="op">/</span> <span class="bu">len</span>(tip_dists)
<span class="kw">def</span> main():
args <span class="op">=</span> get_args()
<span class="co"># parse the tree file and return a generator</span>
trees <span class="op">=</span> Phylo.parse(args.treefile, <span class="st">'newick'</span>)
tree <span class="op">=</span> trees.<span class="bu">next</span>()
<span class="co"># For now, just print</span>
<span class="bu">print</span> <span class="st">"Average distance:"</span>, average_distance(tree)
<span class="co"># This only gets run if the file is being run as a script</span>
<span class="cf">if</span> <span class="va">__name__</span> <span class="op">==</span> <span class="st">'__main__'</span>:
main()</code></pre></div>
</section>
</section>
<section id="and-in-main" class="level1">
<h1>And in <code>main</code>:</h1>
<section id="section-6" class="level2">
<h2></h2>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> main():
args <span class="op">=</span> get_args()
trees <span class="op">=</span> Phylo.parse(args.treefile, <span class="st">'newick'</span>)
tree <span class="op">=</span> trees.<span class="bu">next</span>()
result <span class="op">=</span> average_distance(tree)
<span class="cf">if</span> args.csv:
<span class="co"># We'll define this later</span>
write_csv(result)
<span class="cf">else</span>:
<span class="bu">print</span> <span class="st">"Average distance:"</span>, result</code></pre></div>
</section>
<section id="whole-file-6" class="level2">
<h2>Whole file</h2>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">import</span> argparse
<span class="im">from</span> Bio <span class="im">import</span> Phylo
<span class="kw">def</span> get_args():
parser <span class="op">=</span> argparse.ArgumentParser()
<span class="co"># add parsing rules</span>
parser.add_argument(<span class="st">"treefile"</span>)
parser.add_argument(<span class="st">"--csv"</span>, action<span class="op">=</span><span class="st">"store_true"</span>)
<span class="cf">return</span> parser.parse_args()
<span class="kw">def</span> unique_pairs(xs):
pairs <span class="op">=</span> <span class="bu">set</span>()
<span class="cf">for</span> x1 <span class="kw">in</span> xs:
<span class="cf">for</span> x2 <span class="kw">in</span> xs:
<span class="cf">if</span> x1 <span class="op">!=</span> x2:
pair <span class="op">=</span> [x1, x2]
pairs.add(<span class="bu">tuple</span>(<span class="bu">sorted</span>(pair)))
<span class="cf">return</span> <span class="bu">list</span>(pairs)
<span class="kw">def</span> average_distance(tree):
tipnames <span class="op">=</span> [tip.name <span class="cf">for</span> tip <span class="kw">in</span> tree.get_terminals()]
tipname_pairs <span class="op">=</span> unique_pairs(tipnames)
tip_dists <span class="op">=</span> [tree.distance(t1, t2) <span class="cf">for</span> t1, t2 <span class="kw">in</span> tipname_pairs]
<span class="cf">return</span> <span class="bu">sum</span>(tip_dists) <span class="op">/</span> <span class="bu">len</span>(tip_dists)
<span class="kw">def</span> main():
args <span class="op">=</span> get_args()
<span class="co"># parse the tree file and return a generator</span>
trees <span class="op">=</span> Phylo.parse(args.treefile, <span class="st">'newick'</span>)
tree <span class="op">=</span> trees.<span class="bu">next</span>()
result <span class="op">=</span> average_distance(tree)
<span class="cf">if</span> args.csv:
write_csv(result)
<span class="cf">else</span>:
<span class="bu">print</span> <span class="st">"Average distance:"</span>, result
<span class="co"># This only gets run if the file is being run as a script</span>
<span class="cf">if</span> <span class="va">__name__</span> <span class="op">==</span> <span class="st">'__main__'</span>:
main()</code></pre></div>
</section>
</section>
<section id="filling-out-write_csv" class="level1">
<h1>Filling out <code>write_csv</code>:</h1>
<section id="section-7" class="level2">
<h2></h2>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">import</span> csv
<span class="im">import</span> sys
<span class="kw">def</span> write_csv(result):
writer <span class="op">=</span> csv.writer(sys.stdout)
writer.writerow([<span class="st">"average_distance"</span>])
writer.writerow([result])</code></pre></div>
</section>
<section id="whole-file-7" class="level2">
<h2>Whole file</h2>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">import</span> csv
<span class="im">import</span> sys
<span class="im">import</span> argparse
<span class="im">from</span> Bio <span class="im">import</span> Phylo
<span class="kw">def</span> get_args():
parser <span class="op">=</span> argparse.ArgumentParser()
<span class="co"># add parsing rules</span>
parser.add_argument(<span class="st">"treefile"</span>)
parser.add_argument(<span class="st">"--csv"</span>, action<span class="op">=</span><span class="st">"store_true"</span>)
<span class="cf">return</span> parser.parse_args()
<span class="kw">def</span> unique_pairs(xs):
pairs <span class="op">=</span> <span class="bu">set</span>()
<span class="cf">for</span> x1 <span class="kw">in</span> xs:
<span class="cf">for</span> x2 <span class="kw">in</span> xs:
<span class="cf">if</span> x1 <span class="op">!=</span> x2:
pair <span class="op">=</span> [x1, x2]
pairs.add(<span class="bu">tuple</span>(<span class="bu">sorted</span>(pair)))
<span class="cf">return</span> <span class="bu">list</span>(pairs)
<span class="kw">def</span> average_distance(tree):
tipnames <span class="op">=</span> [tip.name <span class="cf">for</span> tip <span class="kw">in</span> tree.get_terminals()]
tipname_pairs <span class="op">=</span> unique_pairs(tipnames)
tip_dists <span class="op">=</span> [tree.distance(t1, t2) <span class="cf">for</span> t1, t2 <span class="kw">in</span> tipname_pairs]
<span class="cf">return</span> <span class="bu">sum</span>(tip_dists) <span class="op">/</span> <span class="bu">len</span>(tip_dists)
<span class="kw">def</span> write_csv(result):
writer <span class="op">=</span> csv.writer(sys.stdout)
writer.writerow([<span class="st">"average_distance"</span>])
writer.writerow([result])
<span class="kw">def</span> main():
args <span class="op">=</span> get_args()
<span class="co"># parse the tree file and return a generator</span>
trees <span class="op">=</span> Phylo.parse(args.treefile, <span class="st">'newick'</span>)
tree <span class="op">=</span> trees.<span class="bu">next</span>()
result <span class="op">=</span> average_distance(tree)
<span class="cf">if</span> args.csv:
write_csv(result)
<span class="cf">else</span>:
<span class="bu">print</span> <span class="st">"Average distance:"</span>, result
<span class="co"># This only gets run if the file is being run as a script</span>
<span class="cf">if</span> <span class="va">__name__</span> <span class="op">==</span> <span class="st">'__main__'</span>:
main()</code></pre></div>
</section>
</section>
<section id="testing-again" class="level1">
<h1>Testing again</h1>
<p>From bash:</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="co"># Argparse is awesome!</span>
<span class="ex">./scripts/treedists.py</span> --help
<span class="co"># Now let's try running</span>
<span class="ex">./scripts/treedists.py</span> --csv output/Karamjal/tree.nw
<span class="co"># Old behaviour still works</span>
<span class="ex">./scripts/treedists.py</span> output/Karamjal/tree.nw</code></pre></div>
</section>
<section id="adding-location" class="level1">
<h1>Adding location</h1>
<section id="section-8" class="level2">
<h2></h2>
<p>Save, then back in <code>treedists.py</code>:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> get_args():
<span class="co"># ...</span>
<span class="co"># Add the argument</span>
parser.add_argument(<span class="st">"-l"</span>, <span class="st">"--location"</span>)
<span class="co"># ...</span>
<span class="kw">def</span> main():
<span class="co"># ...</span>
<span class="co"># Next pass the location to write_csv</span>
<span class="cf">if</span> args.csv:
write_csv(result, args.location)
<span class="co"># ...</span></code></pre></div>
</section>
<section id="whole-file-8" class="level2">
<h2>Whole file</h2>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">import</span> csv
<span class="im">import</span> sys
<span class="im">import</span> argparse
<span class="im">from</span> Bio <span class="im">import</span> Phylo
<span class="kw">def</span> get_args():
parser <span class="op">=</span> argparse.ArgumentParser()
<span class="co"># add parsing rules</span>
parser.add_argument(<span class="st">"treefile"</span>)
parser.add_argument(<span class="st">"--csv"</span>, action<span class="op">=</span><span class="st">"store_true"</span>)
parser.add_argument(<span class="st">"-l"</span>, <span class="st">"--location"</span>)
<span class="cf">return</span> parser.parse_args()
<span class="kw">def</span> unique_pairs(xs):
pairs <span class="op">=</span> <span class="bu">set</span>()
<span class="cf">for</span> x1 <span class="kw">in</span> xs:
<span class="cf">for</span> x2 <span class="kw">in</span> xs:
<span class="cf">if</span> x1 <span class="op">!=</span> x2:
pair <span class="op">=</span> [x1, x2]
pairs.add(<span class="bu">tuple</span>(<span class="bu">sorted</span>(pair)))
<span class="cf">return</span> <span class="bu">list</span>(pairs)
<span class="kw">def</span> average_distance(tree):
tipnames <span class="op">=</span> [tip.name <span class="cf">for</span> tip <span class="kw">in</span> tree.get_terminals()]
tipname_pairs <span class="op">=</span> unique_pairs(tipnames)
tip_dists <span class="op">=</span> [tree.distance(t1, t2) <span class="cf">for</span> t1, t2 <span class="kw">in</span> tipname_pairs]
<span class="cf">return</span> <span class="bu">sum</span>(tip_dists) <span class="op">/</span> <span class="bu">len</span>(tip_dists)
<span class="kw">def</span> write_csv(result):
writer <span class="op">=</span> csv.writer(sys.stdout)
writer.writerow([<span class="st">"average_distance"</span>])
writer.writerow([result])
<span class="kw">def</span> main():
args <span class="op">=</span> get_args()
<span class="co"># parse the tree file and return a generator</span>
trees <span class="op">=</span> Phylo.parse(args.treefile, <span class="st">'newick'</span>)
tree <span class="op">=</span> trees.<span class="bu">next</span>()
result <span class="op">=</span> average_distance(tree)
<span class="cf">if</span> args.csv:
write_csv(result, args.location)
<span class="cf">else</span>:
<span class="bu">print</span> <span class="st">"Average distance:"</span>, result
<span class="co"># This only gets run if the file is being run as a script</span>
<span class="cf">if</span> <span class="va">__name__</span> <span class="op">==</span> <span class="st">'__main__'</span>:
main()</code></pre></div>
</section>
</section>
<section id="adding-location-to-write_csv" class="level1">
<h1>Adding location (to <code>write_csv</code>)</h1>
<section id="section-9" class="level2">
<h2></h2>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> write_csv(result, location):
writer <span class="op">=</span> csv.writer(sys.stdout)
<span class="co"># Note the inline if/else assignment</span>
header <span class="op">=</span> [<span class="st">"location"</span>, <span class="st">"average_distance"</span>] <span class="cf">if</span> location <span class="cf">else</span> [<span class="st">"average_distance"</span>]
main_row <span class="op">=</span> [location, result] <span class="cf">if</span> location <span class="cf">else</span> [result]
writer.writerows([header, main_row])</code></pre></div>
</section>
<section id="whole-file-9" class="level2">
<h2>Whole file</h2>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">import</span> csv
<span class="im">import</span> sys
<span class="im">import</span> argparse
<span class="im">from</span> Bio <span class="im">import</span> Phylo
<span class="kw">def</span> get_args():
parser <span class="op">=</span> argparse.ArgumentParser()
<span class="co"># add parsing rules</span>
parser.add_argument(<span class="st">"treefile"</span>)
parser.add_argument(<span class="st">"--csv"</span>, action<span class="op">=</span><span class="st">"store_true"</span>)
parser.add_argument(<span class="st">"-l"</span>, <span class="st">"--location"</span>)
<span class="cf">return</span> parser.parse_args()
<span class="kw">def</span> unique_pairs(xs):
pairs <span class="op">=</span> <span class="bu">set</span>()
<span class="cf">for</span> x1 <span class="kw">in</span> xs:
<span class="cf">for</span> x2 <span class="kw">in</span> xs:
<span class="cf">if</span> x1 <span class="op">!=</span> x2:
pair <span class="op">=</span> [x1, x2]
pairs.add(<span class="bu">tuple</span>(<span class="bu">sorted</span>(pair)))
<span class="cf">return</span> <span class="bu">list</span>(pairs)
<span class="kw">def</span> average_distance(tree):
tipnames <span class="op">=</span> [tip.name <span class="cf">for</span> tip <span class="kw">in</span> tree.get_terminals()]
tipname_pairs <span class="op">=</span> unique_pairs(tipnames)
tip_dists <span class="op">=</span> [tree.distance(t1, t2) <span class="cf">for</span> t1, t2 <span class="kw">in</span> tipname_pairs]
<span class="cf">return</span> <span class="bu">sum</span>(tip_dists) <span class="op">/</span> <span class="bu">len</span>(tip_dists)
<span class="kw">def</span> write_csv(result, location):
writer <span class="op">=</span> csv.writer(sys.stdout)
<span class="co"># Note the inline if/else assignment</span>
header <span class="op">=</span> [<span class="st">"location"</span>, <span class="st">"average_distance"</span>] <span class="cf">if</span> location <span class="cf">else</span> [<span class="st">"average_distance"</span>]
main_row <span class="op">=</span> [location, result] <span class="cf">if</span> location <span class="cf">else</span> [result]
writer.writerows([header, main_row])
<span class="kw">def</span> main():
args <span class="op">=</span> get_args()
<span class="co"># parse the tree file and return a generator</span>
trees <span class="op">=</span> Phylo.parse(args.treefile, <span class="st">'newick'</span>)
tree <span class="op">=</span> trees.<span class="bu">next</span>()
result <span class="op">=</span> average_distance(tree)
<span class="cf">if</span> args.csv:
write_csv(result, args.location)
<span class="cf">else</span>:
<span class="bu">print</span> <span class="st">"Average distance:"</span>, result
<span class="co"># This only gets run if the file is being run as a script</span>
<span class="cf">if</span> <span class="va">__name__</span> <span class="op">==</span> <span class="st">'__main__'</span>:
main()</code></pre></div>
</section>
</section>
<section id="testing-out" class="level1">
<h1>Testing out</h1>
<p>Save, then back in bash:</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="co"># Look at new help</span>
<span class="ex">./scripts/treedists.py</span> --help
<span class="co"># Try out new option</span>
<span class="ex">./scripts/treedists.py</span> --csv output/Karamjal/tree.nw -l karamjal
<span class="co"># Old behaviour still works</span>
<span class="ex">./scripts/treedists.py</span> --csv output/Karamjal/tree.nw</code></pre></div>
</section>
<section id="hooking-this-up-in-build.sh" class="level1">
<h1>Hooking this up in <code>build.sh</code></h1>
<p>Switch to the <code>vim</code> pane, and close <code>treedists.py</code>. Then <code>vim build.sh</code> and enter:</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="co"># At the top make sure scripts is in our PATH</span>
<span class="va">PATH=</span>./scripts/:<span class="va">$PATH</span></code></pre></div>
</section>
<section id="hooking-this-up-ctnd" class="level1">
<h1>Hooking this up (ctnd)</h1>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="va">treedist_files=()</span>
<span class="kw">for</span> <span class="ex">location</span> in <span class="va">${locations[*]}</span>
<span class="kw">do</span>
<span class="co"># ...</span>
<span class="co"># Build a location tree</span>
<span class="va">loc_tree=</span><span class="st">"</span><span class="va">$loc_outdir</span><span class="st">/tree.nw"</span>
<span class="ex">FastTree</span> -seed 1234 -nt <span class="va">$loc_alignment</span> <span class="op">></span> <span class="va">$loc_tree</span>
<span class="va">treedists=</span><span class="st">"</span><span class="va">$loc_outdir</span><span class="st">/treedist.csv"</span>
<span class="ex">treedists.py</span> <span class="va">$loc_tree</span> --csv -l <span class="va">$location</span> <span class="op">></span> <span class="va">$treedists</span>
<span class="va">treedist_files+=($treedists)</span>
<span class="kw">done</span></code></pre></div>
</section>
<section id="hooking-this-up-final" class="level1">
<h1>Hooking this up (final)</h1>
<pre><code> # ...
treedist_files+=($treedists)
done
# Aggregate into single CSV
all_treedists="$outdir/all_treedists.csv"
csvstack ${treedist_files[*]} | csvsort -c average_distance > $all_treedists</code></pre>
<p>Save, then try running with <code>./build.sh</code> (but first comment out all <code>FastTree</code> and <code>muscle</code> lines to save time). When done run <code>csvlook output/all_treedists.csv</code>.</p>
</section>
<section id="other-programming-things" class="level1">
<h1>Other programming things</h1>
</section>
<section id="importing-our-code-from-another-namespace" class="level1">
<h1>Importing our code from another namespace</h1>
<p>To import code from this namespace, we have to create an empty <code>__init__.py</code> file in the directory in which it resides. You can do this in bash with <code>touch scripts/__init__.py</code>.</p>
<p>Now back in the python REPL:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># note this does't run the main function</span>
<span class="im">from</span> scripts <span class="im">import</span> treedists
<span class="bu">dir</span>(treedists)
treedists.unique_pairs([<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>,<span class="dv">4</span>])</code></pre></div>
</section>
<section id="a-bit-about-debugging" class="level1">
<h1>A bit about debugging</h1>
<p>You’ll probably spend 2x the time you spent writing your software debugging it.</p>
<p>This is as much of an art as a science. Think of yourself as a detective.</p>
<ul>
<li>Put the clues together</li>
<li>Investigate by printing things out as the program runs</li>
<li>Try <code>import pdb; pdb.set_trace()</code></li>
</ul>
</section>
<section id="be-careful-with-state" class="level1">
<h1>Be careful with state</h1>
<p>When possible, emphasize functions that don’t modify data in place, but rather return new data. (See <code>import copy; help(copy.copy)</code> for copying mutable data)</p>
<p>Upside: Your programs will be a lot easier to analyze and think about in the portions that aren’t stateful.</p>
<p>Downside: Performance can hit you in some situations.</p>
</section>
<section id="back-to-the-high-level" class="level1">
<h1>Back to the high level</h1>
<p>Stop and think about everything we’ve learned:</p>
<ul>
<li>Plain text data is awesome</li>
<li>Unix for gluing things together</li>
<li>Git for keeping track of what you do</li>
<li>Markdown README for explaining what you do</li>
<li>Python for more advanced logic</li>
<li>R for plotting & canned stats</li>
<li>Package up reusable things and make scripts/libraries/programs of them</li>
</ul>
</section>
<section id="above-all" class="level1">
<h1>Above all</h1>
<ul>
<li>Explore</li>
<li>Hypothesize</li>
<li>Verify</li>
</ul>
</section>
<section id="questions" class="level1">
<h1>Questions?</h1>
</section>
<section id="excercise-1" class="level1">
<h1>Excercise 1</h1>
<p>Add an argument to <code>treedists.py</code> that let’s you specify the output file.</p>
</section>
<section id="excercise-2" class="level1">
<h1>Excercise 2</h1>
<p>Pick a problem and write a little python program for yourself!</p>
</section>
<section id="resources" class="level1">
<h1>Resources</h1>
<ul>
<li><a href="https://packaging.python.org/en/latest/distributing.html">Building python packages/modules</a> - For when you want to build bigger python programs, or share/release small/big programs.</li>
<li><a href="https://virtualenv.pypa.io/en/latest/">Virtualenv</a> - Managing python packages can be a pain, because you can’t install two versions of the same package 😒. Virtualenv is a tool that let’s you create and switch between different environments to avoid conflicts. (Also good for switching between python 2/3).</li>
<li><a href="http://nhoffman.github.io/bioscons/bioscons.html#module-bioscons.slurm">Bioscons</a> - If you check out my post on using <a href="http://www.metasoarous.com/scons-for-data-science-and-compbio/">SCons for bioinformatics</a>, you might be interested in checking out the <a href="http://nhoffman.github.io/bioscons/bioscons.html#module-bioscons.slurm"><code>slurm</code> module of bioscons</a>. This let’s you distribute computation across a SLURM cluster (such as the one FHCRC has).</li>
</ul>
<p>(More next slide)</p>
</section>
<section id="resources-ctnd" class="level1">
<h1>Resources (ctnd)</h1>
<ul>
<li><a href="http://biopython.org/DIST/docs/tutorial/Tutorial.html">Biopython Tutorial and Cookbook</a> - For further help using biopython (the <a href="http://biopython.org/wiki/Category:Wiki_Documentation">biopython wiki documentation</a> is also quite good).</li>
<li><a href="http://pandas.pydata.org/">Pandas</a> - R-like <code>data.frame</code>s in python, complete with plyr and reshape like semantics.</li>
<li><a href="http://www.scipy.org/">Scipi / Numpi</a> - Tools for doing more computationally intensive work (scientific/math functions, stats, matrix algebra, etc.)</li>
<li><a href="http://www.codewars.com/">Codewars</a> - Haven’t tried it, but looks like a fun set of programming challenges in multiple languages.</li>
</ul>
</section>
<section id="thats-all-folks" class="level1">
<h1>That’s all folks!</h1>
<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>