-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.xml
10471 lines (10471 loc) · 832 KB
/
index.xml
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
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>natverse</title><link>https://natverse.github.io/</link><description>Recent content on natverse</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Wed, 25 Sep 2019 00:00:00 +0000</lastBuildDate><atom:link href="https://natverse.github.io/index.xml" rel="self" type="application/rss+xml"/><item><title>Introduction to neurons and neuronlists</title><link>https://natverse.github.io/news/2019/09/nat/</link><pubDate>Wed, 25 Sep 2019 00:00:00 +0000</pubDate><guid>https://natverse.github.io/news/2019/09/nat/</guid><description>
<h2 id="preface">Preface</h2>
<p>This vignette is designed to give you a good introduction to some of the key
features of nat by teaching you about how it handles individual neurons and
collections of neurons.</p>
<p>The source code for this vignette is available at
<a href="https://github.com/natverse/nat/blob/master/vignettes/neurons-intro.Rmd" target="_blank">https://github.com/natverse/nat/blob/master/vignettes/neurons-intro.Rmd</a>.
If you find something unclear or notice a typo, I would be very happy if you
would click on the Pencil Icon on that page or follow
<a href="https://github.com/natverse/nat/edit/master/vignettes/neurons-intro.Rmd" target="_blank">this link to edit</a>
and suggest an alternative wording. Don&rsquo;t be shy about doing this; I have to
review any change and even if your suggestion is not perfect it will still
be a prompt for me to improve this document. Thank you!</p>
<h2 id="startup">Startup</h2>
<p>Install the package if required</p>
<pre><code class="language-r">install.packages('nat', dependencies = TRUE)
</code></pre>
<p>Load the package</p>
<pre><code class="language-r">library(nat)
</code></pre>
<h2 id="neurons-and-neuronlists">Neurons and Neuronlists</h2>
<p>There are a number of basic built in types of data in R such as vectors and matrices,
but it is also very easy to define new data types with specialised features to
enable powerful and efficient analysis of particular kinds of data. These specialised
data types are called <em>classes</em> and a specific instance of a class is an <em>object</em>
(a neuroscience analogy: this is the difference between talking about the
general class of mitral cells and the specific cell that you just sealed onto
with your patch clamp electrode.)</p>
<p>We will look at two key classes that <strong>nat</strong> defines for handling 3D structures of neurons</p>
<ul>
<li><code>neuron</code></li>
<li><code>neuronlist</code></li>
</ul>
<h3 id="sample-data">Sample Data</h3>
<p>As an example we&rsquo;re going to look at a data set of olfactory projection neurons originally
published as Supplemental Information to accompany
<a href="http://dx.doi.org/10.1016/j.cell.2007.01.040" target="_blank">Jefferis, Potter et al, Cell (2007)</a>.
A subset of the original data are distributed as a sample data object with the
<strong>nat</strong> package, which we can load like so.</p>
<pre><code class="language-r">data(&quot;Cell07PNs&quot;)
</code></pre>
<p>For more information about these data see <a href="https://natverse.github.io/nat/reference/Cell07PNs.html" target="_blank">Cell07PNs</a>.</p>
<p>This <code>Cell07PNs</code> object has two classes <code>neuronlist</code> and the base class <code>list</code></p>
<pre><code class="language-r">class(Cell07PNs)
</code></pre>
<pre><code>## [1] &quot;neuronlist&quot; &quot;list&quot;
</code></pre>
<p>Many R objects inherit from the base class <code>list</code> (objects can have multiple
classes in the same way that a mitral cell is also neuron) because this is a
convenient container class into which you can put a variety of different kinds
of data. In this case the <code>Cell07PNs</code> <code>neuronlist</code> contains 40 objects:</p>
<pre><code class="language-r">length(Cell07PNs)
</code></pre>
<pre><code>## [1] 40
</code></pre>
<pre><code class="language-r"># access the first neuron in the neuronlist
class(Cell07PNs[[1]])
</code></pre>
<pre><code>## [1] &quot;neuron&quot; &quot;list&quot;
</code></pre>
<p>each representing a traced neuron. We will now look in detail at these two classes.</p>
<h2 id="neurons">Neurons</h2>
<p>We start by extracting the first neuron in Cell07PNs, and assigning it to
a variable. We can then plot the neuron so that we have an image of what we are
talking about.</p>
<pre><code class="language-r">n1=Cell07PNs[[1]]
plot(n1)
</code></pre>
<p><img src="https://natverse.github.io/articles/2019-09-neurons/index_files/figure-html/unnamed-chunk-6-1.png" width="480" /></p>
<h3 id="neuron-internal-structure">Neuron internal structure</h3>
<p>The <code>str</code> function (short for structure) allows us to take a look at the internal
structure of this neuron object.</p>
<pre><code class="language-r"># Use =1 so that we don't show complete structure
# for objects inside n1
str(n1, max.level=1)
</code></pre>
<pre><code>## List of 24
## $ CellType : chr &quot;DA1&quot;
## $ NeuronName : chr &quot;EBH11R&quot;
## $ InputFileName: 'AsIs' chr &quot;/GD/projects/PN2/TransformedTraces/DA1/EBH11R.tasc&quot;
## $ CreatedAt : POSIXt[1:1], format: &quot;2006-01-17 15:21:14&quot;
## $ NodeName : Named chr &quot;jefferis.joh.cam.ac.uk&quot;
## ..- attr(*, &quot;names&quot;)= chr &quot;nodename&quot;
## $ InputFileStat:'data.frame': 1 obs. of 10 variables:
## $ InputFileMD5 : Named chr &quot;fcacee3f874cbe2c6ad96214e6fee337&quot;
## ..- attr(*, &quot;names&quot;)= 'AsIs' chr &quot;/GD/projects/PN2/TransformedTraces/DA1/EBH11R.tasc&quot;
## $ NumPoints : int 180
## $ StartPoint : num 1
## $ BranchPoints : num [1:16] 34 48 51 75 78 95 98 99 108 109 ...
## $ EndPoints : num [1:18] 1 42 59 62 80 85 96 100 102 112 ...
## $ NumSegs : int 33
## $ SegList :List of 33
## $ d :'data.frame': 180 obs. of 7 variables:
## $ OrientInfo :List of 5
## $ SegOrders : num [1:33] 1 2 2 3 4 4 3 4 5 5 ...
## $ MBPoints : int [1:2] 34 48
## $ LHBranchPoint: int 75
## $ SegTypes : num [1:33] 1 3 1 3 3 3 1 2 2 2 ...
## $ AxonSegNos :List of 3
## $ LHSegNos : num [1:26] 8 9 10 11 12 13 14 15 16 17 ...
## $ MBSegNos :List of 2
## $ NumMBBranches: num 2
## $ AxonLHEP : num 72
## - attr(*, &quot;class&quot;)= chr [1:2] &quot;neuron&quot; &quot;list&quot;
</code></pre>
<p>From this you can see that there are a large number of fields inside the neuron.
Each field can be accessed using the <code>$</code> or <code>[[</code> operators</p>
<pre><code class="language-r">n1$NumPoints
</code></pre>
<pre><code>## [1] 180
</code></pre>
<pre><code class="language-r">n1[[&quot;NumPoints&quot;]]
</code></pre>
<pre><code>## [1] 180
</code></pre>
<p>There are a set of core fields (described in <code>?neuron</code> documentation); the key
ones will be described shortly. However there are also quite a few user fields
in this neuron and you can safely add any field you like so long as its name
does not clash with an existing field. For example:</p>
<pre><code class="language-r">n1$Comment='The sex of this specimen is uncertain'
</code></pre>
<p>The single most important field in a neuron is the <code>$d</code> <code>data.frame</code>. This
contains a block of data closely reminiscent of the SWC data format for traced
neurons:</p>
<pre><code class="language-r">str(n1$d)
</code></pre>
<pre><code>## 'data.frame': 180 obs. of 7 variables:
## $ PointNo: int 1 2 3 4 5 6 7 8 9 10 ...
## $ Label : num 2 2 2 2 2 2 2 2 2 2 ...
## $ X : num 187 187 188 188 188 ...
## $ Y : num 133 131 130 129 129 ...
## $ Z : num 88.2 90.6 93.1 95 97.5 ...
## $ W : num 1.01 1.27 1.14 1.27 1.27 1.27 1.27 1.27 1.27 1.27 ...
## $ Parent : num -1 1 2 3 4 5 6 7 8 9 ...
</code></pre>
<p>Each row defines a node in a branched tree with a unique integer identifier,
<code>PointNo</code>, normally but not always sequentially numbered from 1. The <code>X,Y,Z</code>
columns encode the position of each vertex and the <code>W</code> column encodes the
diameter of the neurite at that position.</p>
<h3 id="methods-for-neurons">Methods for neurons</h3>
<p>R&rsquo;s simple <a href="http://adv-r.had.co.nz/OO-essentials.html" target="_blank">S3 object oriented system</a>
allows specialised functions for particular classes to be defined. These functions
tailored to a particular class are called <em>methods</em>. Methods can be provided for
pre-existing functions supplied with base R as well as new user-written functions.
The system is quite simple. If a function <code>foo</code> is defined as a <em>generic</em> function
then you can define new functions called <code>foo.bar</code> that will be called when
you write <code>foo(x)</code> and <code>x</code> has class `bar.</p>
<p>We already used one such method without comment, <code>plot.neuron</code>. There is a base
R function called <code>plot</code>. nat defines a new function called <code>plot.neuron</code>, which
R interprets as a <code>plot</code> method specialised for <code>neuron</code> objects. When the base
R <code>plot</code> function is called, it looks at the class of its first object to see
whether a specialised method exists. If there is none, it will use a fallback
method called <code>plot.default</code>.</p>
<p>The <code>plot.neuron</code> method interprets the branching structure of a neuron and
draws line segments joining up the connected nodes at the appropriate 2D positions.
You can compare it with what you would get if you just plotted the XY position
of all nodes joined together by a single line:</p>
<pre><code class="language-r">plot(n1$d$X, n1$d$Y, type = 'l')
</code></pre>
<p><img src="https://natverse.github.io/articles/2019-09-neurons/index_files/figure-html/unnamed-chunk-11-1.png" width="480" /></p>
<p>You can find out what methods are available for a particular class like so:</p>
<pre><code class="language-r">methods(class = 'neuron')
</code></pre>
<pre><code>## [1] all.equal as.neuron as.ngraph
## [4] as.seglist branchpoints dotprops
## [7] endpoints intersect_plane ndigest
## [10] nvertices Ops plot
## [13] plot3d potential_synapses prune
## [16] resample rootpoints scale
## [19] subset summary write.vtk
## [22] xform xyzmatrix xyzmatrix&lt;-
## see '?methods' for accessing help and source code
</code></pre>
<p>You can then find the help page for any method in the console with <code>?plot.neuron</code>.
Note that if you write <code>?plot</code> you will get the documentation for the basic
<code>plot</code> function supplied with R.</p>
<p>It is also good idea to look at
<a href="https://natverse.github.io/nat/reference/index.html" target="_blank">nat&rsquo;s function reference page</a>
which groups available functions into categories that often reflect the class
of object they can work on.</p>
<p>In R, operators such as <code>*</code> or <code>+</code> are actually special functions with two
arguments, so one can add methods for these in the same way. In the following
example we use this arithmetic to shift a neuron by a small amount:</p>
<pre><code class="language-r">plot(n1, col = 'black', WithNodes = F, main=&quot;Shifting neurons&quot;)
# shift by 3 microns in x,y,z
plot(n1+3, col = 'red', add = TRUE, WithNodes = F)
# shift by -5 microns in y
plot(n1+c(0,-5,0), col = 'blue', add = TRUE, WithNodes = F)
</code></pre>
<p><img src="https://natverse.github.io/articles/2019-09-neurons/index_files/figure-html/unnamed-chunk-13-1.png" width="480" /></p>
<p>Another specialised method is <code>subset.neuron</code> which we can use to extract part
of a neuron into a new object. For example, a simple spatial criterion,
X location must be &gt;240, is used to extract the axon terminal arborisation field
in this example</p>
<pre><code class="language-r">plot(n1, col = 'black', WithAllPoints = T, main=&quot;Subsetting a neuron&quot;)
# keep only nodes with X position &gt;20
n1.lh=subset(n1, X&gt;240)
# plot the selected part of the neuron in blue
plot(n1.lh, col='blue', lwd=3, add=TRUE)
# add a line illustrating the cut point
abline(v=240, lty=2)
</code></pre>
<p><img src="https://natverse.github.io/articles/2019-09-neurons/index_files/figure-html/unnamed-chunk-14-1.png" width="480" /></p>
<p>We can also summarise the morphological properties of a neuron using the
<code>summary.neuron</code> method, which allows us to compare measurements for the original
neuron and the axon arbour that we just cut out above.</p>
<pre><code class="language-r">summary(n1)
</code></pre>
<pre><code>## root nodes segments branchpoints endpoints cable.length
## 1 1 180 33 16 18 297.1763
</code></pre>
<pre><code class="language-r">summary(n1.lh)
</code></pre>
<pre><code>## root nodes segments branchpoints endpoints cable.length
## 1 1 111 27 13 15 156.7654
</code></pre>
<p>See <a href="https://natverse.github.io/nat/reference/index.html" target="_blank">nat&rsquo;s function reference page</a>
for additional functions for working with neurons.</p>
<h2 id="neuronlists">Neuronlists</h2>
<p>The <code>data</code> statement above loaded a <code>neuronlist</code> object called <code>Cell07PNs</code>
containing 40 <code>neuron</code> objects. <code>neuronlist</code> objects are key
data structures in nat for convenient handling of collections of neurons.</p>
<p><img src="fig/neuronlist-01.png" alt="Structure of neuronlist objects" /></p>
<p>The diagram above presents the structure of a neuronlist, <code>x</code>, which contains
5 neurons. The main structure is an R <code>list</code> object, which in this case contains
5 slots. Each slot contains a neuron with a unique name. In addition to this
list of neurons there is an optional attached <code>data.frame</code> (another standard R class).
When present, each neuron in the main list must have a matching row in this <code>data.frame</code>.</p>
<p>Neuronlists can be manipulated use the square bracket subscript operator to
extract or replace a subset of their elements. Crucially when this happens the
corresponding rows of the attached metadata are also selected as diagrammed for
the result of x<a href="right hand side of figure above" target="_blank">1:3</a>.</p>
<p>When subscripting any list in R, it is very important to understand the difference
between using the single and double bracket operator. We can illustrate this
difference as follows:</p>
<p><img src="fig/neuronlist-subsets-01.png" alt="The difference between single and double bracket subscript operators" /></p>
<p>The single bracket operator makes a new list containing a specified subset of
elements in the original list, while the double bracket operator extracts the
object at the given position. So in the figure <code>x[1]</code> is a <code>neuronlist</code> object
of length 1, whereas <code>x[[1]]</code> is the <code>neuron</code> object at the first position in
the list.</p>
<h3 id="worked-examples">Worked Examples</h3>
<p>Let us start by extracting the first five neurons from our sample data.</p>
<pre><code class="language-r"># we can select neurons by indexing
first5=Cell07PNs[1:5]
summary(first5)
</code></pre>
<pre><code>## root nodes segments branchpoints endpoints cable.length
## EBH11R 1 180 33 16 18 297.1763
## EBH20L 1 200 26 12 15 327.0929
## EBH20R 1 199 25 12 14 347.6153
## EBI12L 1 169 23 11 13 294.4680
## EBI22R 1 160 27 13 15 303.0150
</code></pre>
<p>Each neuron in the neuronlist has an associated name which can be used to select
it. We can get the names of all neurons using the <code>names</code> function.</p>
<pre><code class="language-r">names(Cell07PNs)
</code></pre>
<pre><code>## [1] &quot;EBH11R&quot; &quot;EBH20L&quot; &quot;EBH20R&quot; &quot;EBI12L&quot; &quot;EBI22R&quot; &quot;EBJ23L&quot; &quot;EBJ3R&quot;
## [8] &quot;EBN19L&quot; &quot;EBO15L&quot; &quot;EBO53L&quot; &quot;ECA34L&quot; &quot;ECB3L&quot; &quot;LI23L&quot; &quot;LIC2R&quot;
## [15] &quot;LJ5L&quot; &quot;MC3B&quot; &quot;MH16L&quot; &quot;MM14L&quot; &quot;NA7L&quot; &quot;NH15L&quot; &quot;NH29B&quot;
## [22] &quot;NI16L&quot; &quot;NIA8L&quot; &quot;NIA8R&quot; &quot;NNA9L&quot; &quot;NNC4R&quot; &quot;NNE1L&quot; &quot;OFD2L&quot;
## [29] &quot;OKC9R&quot; &quot;SDD8L&quot; &quot;SH21L&quot; &quot;SL20L&quot; &quot;TKC8R&quot; &quot;TL4R&quot; &quot;TS7L&quot;
## [36] &quot;TT27R&quot; &quot;VA15R&quot; &quot;VA20R&quot; &quot;VB37L&quot; &quot;VB58L&quot;
</code></pre>
<pre><code class="language-r">nl3=Cell07PNs[c(&quot;EBH11R&quot;, &quot;EBH20L&quot;, &quot;EBH20R&quot;)]
all.equal(nl3, Cell07PNs[1:3])
</code></pre>
<pre><code>## [1] TRUE
</code></pre>
<p>You can also use the <code>$</code> operator to access a single neuron e.g. <code>Cell07PNs$EBH11R</code>.
This can be quite useful when working interactively in the console because
RStudio will offer to autocomplete the neuron name when you start typing past the
<code>$</code> symbol, but is not recommended for scripts.</p>
<h3 id="metadata">Metadata</h3>
<p>The names of neurons are also used to index a <code>data.frame</code> object attached to the
neuronlist with one row for each neuron. You can use the <code>head</code> function to give
a summary of the attached metadata in this neuronlist. Using the <code>as.data.frame</code> method on
a neuronlist allows you to extract this attached metadata to a separate object.</p>
<pre><code class="language-r">head(Cell07PNs)
</code></pre>
<pre><code>## Brain RegistrationScore Notes Glomerulus Batch Directory Traced
## EBH11R EBH11R 4 DA1 PN2 unsure yes
## EBH20L EBH20L 4 DL3 PN2 unsure2 yes
## EBH20R EBH20R 4 DA1 PN2 unsure2 yes
## EBI12L EBI12L 4 DA1 PN2 unsure3 yes
## EBI22R EBI22R 4 DL3 PN2 unsure3 yes
## EBJ23L EBJ23L 4 DL3 PN2 unsure4 yes
## Scored.By Sex Include ID HaveWarp HaveAsc
## EBH11R ACH M EBH11R TRUE TRUE
## EBH20L ACH EBH20L TRUE TRUE
## EBH20R ACH M EBH20R TRUE TRUE
## EBI12L ACH F EBI12L TRUE TRUE
## EBI22R ACH EBI22R TRUE TRUE
## EBJ23L ACH EBJ23L TRUE TRUE
## TraceFile AscBatch Status
## EBH11R /GD/projects/PN2/TransformedTraces/DA1/EBH11R.tasc New
## EBH20L /GD/projects/PN2/TransformedTraces/DL3/EBH20L.tasc New
## EBH20R /GD/projects/PN2/TransformedTraces/DA1/EBH20R.tasc New
## EBI12L /GD/projects/PN2/TransformedTraces/DA1/EBI12L.tasc New
## EBI22R /GD/projects/PN2/TransformedTraces/DL3/EBI22R.tasc New
## EBJ23L /GD/projects/PN2/TransformedTraces/DL3/EBJ23L.tasc New
## GlomSeq NumNAs MBP1 MBP2 LHBP PNType Seq nTrees StartPoint
## EBH11R 1 0 34 48 75 iPN 13 1 1
## EBH20L 1 0 29 29 77 iPN 15 1 1
## EBH20R 2 0 24 80 147 iPN 16 1 1
## EBI12L 3 0 29 59 89 iPN 18 1 1
## EBI22R 2 0 34 34 56 iPN 20 1 1
## EBJ23L 3 0 23 23 52 iPN 21 1 1
## CreatedAt
## EBH11R 2006-01-17 15:21:14
## EBH20L 2006-01-17 15:21:14
## EBH20R 2006-01-17 15:21:14
## EBI12L 2006-01-17 15:21:15
## EBI22R 2006-01-17 15:21:15
## EBJ23L 2006-01-17 15:21:15
## WarpFile
## EBH11R /GD/projects/PN2/allreg/warp/unsure/average-goodbrains_EBH11R101_warp_m0g40c4e1e-1x16r3.list
## EBH20L /GD/projects/PN2/allreg/warp/unsure2/average-goodbrains_EBH20L101_warp_m0g40c4e1e-1x16r3.list
## EBH20R /GD/projects/PN2/allreg/warp/unsure2/average-goodbrains_EBH20R101_warp_m0g40c4e1e-1x16r3.list
## EBI12L /GD/projects/PN2/allreg/warp/unsure3/average-goodbrains_EBI12L101_warp_m0g40c4e1e-1x16r3.list
## EBI22R /GD/projects/PN2/allreg/warp/unsure3/average-goodbrains_EBI22R101_warp_m0g40c4e1e-1x16r3.list
## EBJ23L /GD/projects/PN2/allreg/warp/unsure4/average-goodbrains_EBJ23L101_warp_m0g40c4e1e-1x16r3.list
</code></pre>
<pre><code class="language-r">df=as.data.frame(Cell07PNs)
nrow(df)
</code></pre>
<pre><code>## [1] 40
</code></pre>
<pre><code class="language-r"># rownames of data.frame are the same as names of Cell07PNs list.
all.equal(rownames(df), names(Cell07PNs))
</code></pre>
<pre><code>## [1] TRUE
</code></pre>
<p>You can use columns in the attached metadata in expressions that select or
operate on the neurons in the list. In the next two examples we</p>
<ul>
<li><code>subset</code> the neurons in a neuronlist based on a metadata column</li>
<li>use the <code>with</code> function to carry out a calculation using column(s)
of metadata.</li>
</ul>
<pre><code class="language-r"># subset.neuronlist method (which you call using the subset function)
da1n=subset(Cell07PNs, Glomerulus==&quot;DA1&quot;)
# with.neuronlist method
with(Cell07PNs, table(Glomerulus))
</code></pre>
<pre><code>## Glomerulus
## DA1 DL3 DP1m VA1d
## 11 10 8 11
</code></pre>
<p>There is a second approach to accessing this attached metadata data.frame, using
the square bracket operator with two arguments as used for subscripting (2D)
matrices or <code>data.frames</code>. For example we can access the <code>Glomerulus</code> column
like so:</p>
<pre><code class="language-r">Cell07PNs[, 'Glomerulus']
</code></pre>
<pre><code>## [1] DA1 DL3 DA1 DA1 DL3 DL3 VA1d VA1d VA1d VA1d DP1m DP1m DA1 DL3
## [15] VA1d VA1d DL3 DA1 DA1 VA1d VA1d VA1d DL3 VA1d DP1m DP1m DP1m DP1m
## [29] DP1m DP1m DA1 DL3 DL3 DL3 VA1d DA1 DA1 DA1 DL3 DA1
## Levels: DA1 DL3 DP1m VA1d
</code></pre>
<p>This approach has some features that make for additional flexibility. For example
you can access the whole data.frame like so:</p>
<pre><code class="language-r">summary(Cell07PNs[,])
</code></pre>
<p>The form <code>[,]</code> which includes a comma implies two (missing) arguments.
<strong>nat</strong> therefore interprets this as request for the 2D data.frame
attached to Cell07PNs rather than the neuronlist itself. You can also use this
notation to add new columns or modify existing columns in place.</p>
<pre><code class="language-r">Cell07PNs[, 'NewColumn'] = somevariable
Cell07PNs[, &quot;Sex&quot;] = sub(&quot;F&quot;, &quot;female&quot;, Cell07PNs[, &quot;Sex&quot;])
Cell07PNs[, &quot;Sex&quot;] = sub(&quot;M&quot;, &quot;male&quot;, Cell07PNs[, &quot;Sex&quot;])
</code></pre>
<p>You can also access selected rows of the data.frame in the normal way:</p>
<pre><code class="language-r">Cell07PNs[1:5, &quot;Glomerulus&quot;]
Cell07PNs[Cell07PNs[, &quot;Sex&quot;]==&quot;F&quot;, &quot;Glomerulus&quot;]
</code></pre>
<h3 id="working-on-all-the-neurons-in-a-neuronlist">Working on all the neurons in a neuronlist</h3>
<p>There are quite a few methods defined for the <code>neuronlist</code> class:</p>
<pre><code class="language-r">methods(class = &quot;neuronlist&quot;)
</code></pre>
<pre><code>## [1] - [ [&lt;-
## [4] * / +
## [7] as.data.frame as.neuronlistfh c
## [10] data.frame&lt;- dimnames dotprops
## [13] droplevels head intersect
## [16] mirror nvertices plot
## [19] plot3d potential_synapses prune
## [22] setdiff subset summary
## [25] tail union with
## [28] xform xyzmatrix xyzmatrix&lt;-
## see '?methods' for accessing help and source code
</code></pre>
<p>For example we can scale the position (and diameter) of all of the neurons in a
neuronlist using the <code>*</code> operator:</p>
<pre><code class="language-r"># convert from microns to nm
Cell07PNs.nm = Cell07PNs*1e3
plot(Cell07PNs.nm)
</code></pre>
<p><img src="https://natverse.github.io/articles/2019-09-neurons/index_files/figure-html/unnamed-chunk-25-1.png" width="480" /></p>
<p>There is also a more powerful <code>xform</code> function that allows arbitrary
transformations to be applied. For example here we apply a 3D rotation expressed
as a homogeneous affine matrix:</p>
<pre><code class="language-r"># define a 180 degree rotation around the x axis
rotm=cmtkparams2affmat(rx=180)
# remove tiny values due to rounding errors
rotm=zapsmall(rotm)
plot(xform(Cell07PNs, rotm))
</code></pre>
<p><img src="https://natverse.github.io/articles/2019-09-neurons/index_files/figure-html/unnamed-chunk-26-1.png" width="480" /></p>
<p>More generally any function that works on a neuron can be applied to a
neuronlist by using the function <code>nlapply</code>. This is an analogue to base R&rsquo;s
<code>lapply</code> function. This will take care of returning a new neuronlist with the
attached metadata and includes additional features such as error tolerance,
progress bars and parallelisation for multi-core machines. It is worth reviewing
the help and examples for this function carefully if you start to work regularly with
neuronlists.</p>
<h2 id="plots">Plots</h2>
<p>We have already seen that we can plot a single neuron (the first) like so:</p>
<pre><code class="language-r">plot(Cell07PNs[[1]])
</code></pre>
<p><img src="https://natverse.github.io/articles/2019-09-neurons/index_files/figure-html/unnamed-chunk-27-1.png" width="768" /></p>
<p>The purple node highlights the root or soma of the neuron, red nodes are branch
points, green nodes are end points. We can also label each node with its <code>PointNo</code> index.</p>
<pre><code class="language-r">plot(Cell07PNs[[1]], col='red', WithText=T)
</code></pre>
<p><img src="https://natverse.github.io/articles/2019-09-neurons/index_files/figure-html/unnamed-chunk-28-1.png" width="768" /></p>
<p>Multiple neurons can be plotted by passing a whole neuronlist or indexing it</p>
<pre><code class="language-r">plot(Cell07PNs[1:3])
</code></pre>
<p><img src="https://natverse.github.io/articles/2019-09-neurons/index_files/figure-html/unnamed-chunk-29-1.png" width="768" /></p>
<p>More complex subsets are possible by using <code>plot.neuronlist</code> subset argument
(which works in the same way as the <code>subset.neuronlist</code> function). For example
here we select neurons by which olfactory glomeruli their dendrites occupy:</p>
<pre><code class="language-r">plot(Cell07PNs, subset=Glomerulus!=&quot;DA1&quot;, col='grey', WithNodes=F, main=&quot;DA1 neurons&quot;)
plot(Cell07PNs, subset=Glomerulus==&quot;DA1&quot;, add=TRUE)
</code></pre>
<p><img src="https://natverse.github.io/articles/2019-09-neurons/index_files/figure-html/unnamed-chunk-30-1.png" width="480" /></p>
<h3 id="3d-plots">3D plots</h3>
<p>So far we have only used 2D plots in this document, but especially for
interactive analysis and exploration, it is much more helpful to have
3D plots that can be rotated, zoomed etc. <strong>nat</strong> provides numerous functions
for <a href="https://natverse.github.io/nat/reference/index.html#section--d-plotting-of-objects" target="_blank">3D plots</a>
based on the <a href="https://cran.r-project.org/package=rgl" target="_blank">rgl package</a>. It is
actually possible to embed fully interactive 3d figures in <a href="http://rmarkdown.rstudio.com/" target="_blank">rmarkdown</a> reports
like this one by setting the <a href="https://CRAN.R-project.org/package=rgl/vignettes/WebGL.html" target="_blank">webgl chunk option</a>.</p>
<pre><code class="language-r">clear3d()
plot3d(Cell07PNs[[1]], col='red')
# set a grey background so it's easier to see extent of the webgl canvas
bg3d(col='lightgrey')
</code></pre>
<script>/*
* Copyright (C) 2009 Apple Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* Copyright (2016) Duncan Murdoch - fixed CanvasMatrix4.ortho,
* cleaned up.
*/
/*
CanvasMatrix4 class
This class implements a 4x4 matrix. It has functions which
duplicate the functionality of the OpenGL matrix stack and
glut functions.
IDL:
[
Constructor(in CanvasMatrix4 matrix), // copy passed matrix into new CanvasMatrix4
Constructor(in sequence<float> array) // create new CanvasMatrix4 with 16 floats (row major)
Constructor() // create new CanvasMatrix4 with identity matrix
]
interface CanvasMatrix4 {
attribute float m11;
attribute float m12;
attribute float m13;
attribute float m14;
attribute float m21;
attribute float m22;
attribute float m23;
attribute float m24;
attribute float m31;
attribute float m32;
attribute float m33;
attribute float m34;
attribute float m41;
attribute float m42;
attribute float m43;
attribute float m44;
void load(in CanvasMatrix4 matrix); // copy the values from the passed matrix
void load(in sequence<float> array); // copy 16 floats into the matrix
sequence<float> getAsArray(); // return the matrix as an array of 16 floats
WebGLFloatArray getAsCanvasFloatArray(); // return the matrix as a WebGLFloatArray with 16 values
void makeIdentity(); // replace the matrix with identity
void transpose(); // replace the matrix with its transpose
void invert(); // replace the matrix with its inverse
void translate(in float x, in float y, in float z); // multiply the matrix by passed translation values on the right
void scale(in float x, in float y, in float z); // multiply the matrix by passed scale values on the right
void rotate(in float angle, // multiply the matrix by passed rotation values on the right
in float x, in float y, in float z); // (angle is in degrees)
void multRight(in CanvasMatrix matrix); // multiply the matrix by the passed matrix on the right
void multLeft(in CanvasMatrix matrix); // multiply the matrix by the passed matrix on the left
void ortho(in float left, in float right, // multiply the matrix by the passed ortho values on the right
in float bottom, in float top,
in float near, in float far);
void frustum(in float left, in float right, // multiply the matrix by the passed frustum values on the right
in float bottom, in float top,
in float near, in float far);
void perspective(in float fovy, in float aspect, // multiply the matrix by the passed perspective values on the right
in float zNear, in float zFar);
void lookat(in float eyex, in float eyey, in float eyez, // multiply the matrix by the passed lookat
in float ctrx, in float ctry, in float ctrz, // values on the right
in float upx, in float upy, in float upz);
}
*/
CanvasMatrix4 = function(m)
{
if (typeof m == 'object') {
if ("length" in m && m.length >= 16) {
this.load(m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8], m[9], m[10], m[11], m[12], m[13], m[14], m[15]);
return;
}
else if (m instanceof CanvasMatrix4) {
this.load(m);
return;
}
}
this.makeIdentity();
};
CanvasMatrix4.prototype.load = function()
{
if (arguments.length == 1 && typeof arguments[0] == 'object') {
var matrix = arguments[0];
if ("length" in matrix && matrix.length == 16) {
this.m11 = matrix[0];
this.m12 = matrix[1];
this.m13 = matrix[2];
this.m14 = matrix[3];
this.m21 = matrix[4];
this.m22 = matrix[5];
this.m23 = matrix[6];
this.m24 = matrix[7];
this.m31 = matrix[8];
this.m32 = matrix[9];
this.m33 = matrix[10];
this.m34 = matrix[11];
this.m41 = matrix[12];
this.m42 = matrix[13];
this.m43 = matrix[14];
this.m44 = matrix[15];
return;
}
if (arguments[0] instanceof CanvasMatrix4) {
this.m11 = matrix.m11;
this.m12 = matrix.m12;
this.m13 = matrix.m13;
this.m14 = matrix.m14;
this.m21 = matrix.m21;
this.m22 = matrix.m22;
this.m23 = matrix.m23;
this.m24 = matrix.m24;
this.m31 = matrix.m31;
this.m32 = matrix.m32;
this.m33 = matrix.m33;
this.m34 = matrix.m34;
this.m41 = matrix.m41;
this.m42 = matrix.m42;
this.m43 = matrix.m43;
this.m44 = matrix.m44;
return;
}
}
this.makeIdentity();
};
CanvasMatrix4.prototype.getAsArray = function()
{
return [
this.m11, this.m12, this.m13, this.m14,
this.m21, this.m22, this.m23, this.m24,
this.m31, this.m32, this.m33, this.m34,
this.m41, this.m42, this.m43, this.m44
];
};
CanvasMatrix4.prototype.getAsWebGLFloatArray = function()
{
return new WebGLFloatArray(this.getAsArray());
};
CanvasMatrix4.prototype.makeIdentity = function()
{
this.m11 = 1;
this.m12 = 0;
this.m13 = 0;
this.m14 = 0;
this.m21 = 0;
this.m22 = 1;
this.m23 = 0;
this.m24 = 0;
this.m31 = 0;
this.m32 = 0;
this.m33 = 1;
this.m34 = 0;
this.m41 = 0;
this.m42 = 0;
this.m43 = 0;
this.m44 = 1;
};
CanvasMatrix4.prototype.transpose = function()
{
var tmp = this.m12;
this.m12 = this.m21;
this.m21 = tmp;
tmp = this.m13;
this.m13 = this.m31;
this.m31 = tmp;
tmp = this.m14;
this.m14 = this.m41;
this.m41 = tmp;
tmp = this.m23;
this.m23 = this.m32;
this.m32 = tmp;
tmp = this.m24;
this.m24 = this.m42;
this.m42 = tmp;
tmp = this.m34;
this.m34 = this.m43;
this.m43 = tmp;
};
CanvasMatrix4.prototype.invert = function()
{
// Calculate the 4x4 determinant
// If the determinant is zero,
// then the inverse matrix is not unique.
var det = this._determinant4x4();
if (Math.abs(det) < 1e-8)
return null;
this._makeAdjoint();
// Scale the adjoint matrix to get the inverse
this.m11 /= det;
this.m12 /= det;
this.m13 /= det;
this.m14 /= det;
this.m21 /= det;
this.m22 /= det;
this.m23 /= det;
this.m24 /= det;
this.m31 /= det;
this.m32 /= det;
this.m33 /= det;
this.m34 /= det;
this.m41 /= det;
this.m42 /= det;
this.m43 /= det;
this.m44 /= det;
};
CanvasMatrix4.prototype.translate = function(x,y,z)
{
if (x === undefined)
x = 0;
if (y === undefined)
y = 0;
if (z === undefined)
z = 0;
var matrix = new CanvasMatrix4();
matrix.m41 = x;
matrix.m42 = y;
matrix.m43 = z;
this.multRight(matrix);
};
CanvasMatrix4.prototype.scale = function(x,y,z)
{
if (x === undefined)
x = 1;
if (z === undefined) {
if (y === undefined) {
y = x;
z = x;
}
else
z = 1;
}
else if (y === undefined)
y = x;
var matrix = new CanvasMatrix4();
matrix.m11 = x;
matrix.m22 = y;
matrix.m33 = z;
this.multRight(matrix);
};
CanvasMatrix4.prototype.rotate = function(angle,x,y,z)
{
// angles are in degrees. Switch to radians
angle = angle / 180 * Math.PI;
angle /= 2;
var sinA = Math.sin(angle);
var cosA = Math.cos(angle);
var sinA2 = sinA * sinA;
// normalize
var length = Math.sqrt(x * x + y * y + z * z);
if (length === 0) {
// bad vector, just use something reasonable
x = 0;
y = 0;
z = 1;
} else if (length != 1) {
x /= length;
y /= length;
z /= length;
}
var mat = new CanvasMatrix4();
// optimize case where axis is along major axis
if (x == 1 && y === 0 && z === 0) {
mat.m11 = 1;
mat.m12 = 0;
mat.m13 = 0;
mat.m21 = 0;
mat.m22 = 1 - 2 * sinA2;
mat.m23 = 2 * sinA * cosA;
mat.m31 = 0;
mat.m32 = -2 * sinA * cosA;
mat.m33 = 1 - 2 * sinA2;
mat.m14 = mat.m24 = mat.m34 = 0;
mat.m41 = mat.m42 = mat.m43 = 0;
mat.m44 = 1;
} else if (x === 0 && y == 1 && z === 0) {
mat.m11 = 1 - 2 * sinA2;
mat.m12 = 0;
mat.m13 = -2 * sinA * cosA;
mat.m21 = 0;
mat.m22 = 1;
mat.m23 = 0;
mat.m31 = 2 * sinA * cosA;
mat.m32 = 0;
mat.m33 = 1 - 2 * sinA2;
mat.m14 = mat.m24 = mat.m34 = 0;
mat.m41 = mat.m42 = mat.m43 = 0;
mat.m44 = 1;
} else if (x === 0 && y === 0 && z == 1) {
mat.m11 = 1 - 2 * sinA2;
mat.m12 = 2 * sinA * cosA;
mat.m13 = 0;
mat.m21 = -2 * sinA * cosA;
mat.m22 = 1 - 2 * sinA2;
mat.m23 = 0;
mat.m31 = 0;
mat.m32 = 0;
mat.m33 = 1;
mat.m14 = mat.m24 = mat.m34 = 0;
mat.m41 = mat.m42 = mat.m43 = 0;
mat.m44 = 1;
} else {
var x2 = x*x;
var y2 = y*y;
var z2 = z*z;
mat.m11 = 1 - 2 * (y2 + z2) * sinA2;
mat.m12 = 2 * (x * y * sinA2 + z * sinA * cosA);
mat.m13 = 2 * (x * z * sinA2 - y * sinA * cosA);
mat.m21 = 2 * (y * x * sinA2 - z * sinA * cosA);
mat.m22 = 1 - 2 * (z2 + x2) * sinA2;
mat.m23 = 2 * (y * z * sinA2 + x * sinA * cosA);
mat.m31 = 2 * (z * x * sinA2 + y * sinA * cosA);
mat.m32 = 2 * (z * y * sinA2 - x * sinA * cosA);
mat.m33 = 1 - 2 * (x2 + y2) * sinA2;
mat.m14 = mat.m24 = mat.m34 = 0;
mat.m41 = mat.m42 = mat.m43 = 0;
mat.m44 = 1;
}
this.multRight(mat);
};
CanvasMatrix4.prototype.multRight = function(mat)
{
var m11 = (this.m11 * mat.m11 + this.m12 * mat.m21 +
this.m13 * mat.m31 + this.m14 * mat.m41);
var m12 = (this.m11 * mat.m12 + this.m12 * mat.m22 +
this.m13 * mat.m32 + this.m14 * mat.m42);
var m13 = (this.m11 * mat.m13 + this.m12 * mat.m23 +
this.m13 * mat.m33 + this.m14 * mat.m43);
var m14 = (this.m11 * mat.m14 + this.m12 * mat.m24 +
this.m13 * mat.m34 + this.m14 * mat.m44);
var m21 = (this.m21 * mat.m11 + this.m22 * mat.m21 +
this.m23 * mat.m31 + this.m24 * mat.m41);
var m22 = (this.m21 * mat.m12 + this.m22 * mat.m22 +
this.m23 * mat.m32 + this.m24 * mat.m42);
var m23 = (this.m21 * mat.m13 + this.m22 * mat.m23 +
this.m23 * mat.m33 + this.m24 * mat.m43);
var m24 = (this.m21 * mat.m14 + this.m22 * mat.m24 +
this.m23 * mat.m34 + this.m24 * mat.m44);
var m31 = (this.m31 * mat.m11 + this.m32 * mat.m21 +
this.m33 * mat.m31 + this.m34 * mat.m41);
var m32 = (this.m31 * mat.m12 + this.m32 * mat.m22 +
this.m33 * mat.m32 + this.m34 * mat.m42);
var m33 = (this.m31 * mat.m13 + this.m32 * mat.m23 +
this.m33 * mat.m33 + this.m34 * mat.m43);
var m34 = (this.m31 * mat.m14 + this.m32 * mat.m24 +
this.m33 * mat.m34 + this.m34 * mat.m44);
var m41 = (this.m41 * mat.m11 + this.m42 * mat.m21 +
this.m43 * mat.m31 + this.m44 * mat.m41);
var m42 = (this.m41 * mat.m12 + this.m42 * mat.m22 +
this.m43 * mat.m32 + this.m44 * mat.m42);
var m43 = (this.m41 * mat.m13 + this.m42 * mat.m23 +
this.m43 * mat.m33 + this.m44 * mat.m43);
var m44 = (this.m41 * mat.m14 + this.m42 * mat.m24 +
this.m43 * mat.m34 + this.m44 * mat.m44);
this.m11 = m11;
this.m12 = m12;
this.m13 = m13;
this.m14 = m14;
this.m21 = m21;
this.m22 = m22;
this.m23 = m23;
this.m24 = m24;
this.m31 = m31;
this.m32 = m32;
this.m33 = m33;
this.m34 = m34;
this.m41 = m41;
this.m42 = m42;
this.m43 = m43;
this.m44 = m44;
};
CanvasMatrix4.prototype.multLeft = function(mat)
{
var m11 = (mat.m11 * this.m11 + mat.m12 * this.m21 +
mat.m13 * this.m31 + mat.m14 * this.m41);
var m12 = (mat.m11 * this.m12 + mat.m12 * this.m22 +
mat.m13 * this.m32 + mat.m14 * this.m42);
var m13 = (mat.m11 * this.m13 + mat.m12 * this.m23 +
mat.m13 * this.m33 + mat.m14 * this.m43);
var m14 = (mat.m11 * this.m14 + mat.m12 * this.m24 +
mat.m13 * this.m34 + mat.m14 * this.m44);
var m21 = (mat.m21 * this.m11 + mat.m22 * this.m21 +
mat.m23 * this.m31 + mat.m24 * this.m41);
var m22 = (mat.m21 * this.m12 + mat.m22 * this.m22 +
mat.m23 * this.m32 + mat.m24 * this.m42);
var m23 = (mat.m21 * this.m13 + mat.m22 * this.m23 +
mat.m23 * this.m33 + mat.m24 * this.m43);
var m24 = (mat.m21 * this.m14 + mat.m22 * this.m24 +
mat.m23 * this.m34 + mat.m24 * this.m44);
var m31 = (mat.m31 * this.m11 + mat.m32 * this.m21 +
mat.m33 * this.m31 + mat.m34 * this.m41);
var m32 = (mat.m31 * this.m12 + mat.m32 * this.m22 +
mat.m33 * this.m32 + mat.m34 * this.m42);
var m33 = (mat.m31 * this.m13 + mat.m32 * this.m23 +
mat.m33 * this.m33 + mat.m34 * this.m43);
var m34 = (mat.m31 * this.m14 + mat.m32 * this.m24 +
mat.m33 * this.m34 + mat.m34 * this.m44);
var m41 = (mat.m41 * this.m11 + mat.m42 * this.m21 +
mat.m43 * this.m31 + mat.m44 * this.m41);
var m42 = (mat.m41 * this.m12 + mat.m42 * this.m22 +
mat.m43 * this.m32 + mat.m44 * this.m42);
var m43 = (mat.m41 * this.m13 + mat.m42 * this.m23 +
mat.m43 * this.m33 + mat.m44 * this.m43);
var m44 = (mat.m41 * this.m14 + mat.m42 * this.m24 +
mat.m43 * this.m34 + mat.m44 * this.m44);
this.m11 = m11;
this.m12 = m12;
this.m13 = m13;
this.m14 = m14;
this.m21 = m21;
this.m22 = m22;
this.m23 = m23;
this.m24 = m24;
this.m31 = m31;
this.m32 = m32;
this.m33 = m33;
this.m34 = m34;
this.m41 = m41;
this.m42 = m42;
this.m43 = m43;
this.m44 = m44;
};
CanvasMatrix4.prototype.ortho = function(left, right, bottom, top, near, far)
{
var tx = (left + right) / (left - right);
var ty = (top + bottom) / (bottom - top);
var tz = (far + near) / (near - far);
var matrix = new CanvasMatrix4();
matrix.m11 = 2 / (right - left);
matrix.m12 = 0;
matrix.m13 = 0;
matrix.m14 = 0;
matrix.m21 = 0;
matrix.m22 = 2 / (top - bottom);
matrix.m23 = 0;
matrix.m24 = 0;
matrix.m31 = 0;
matrix.m32 = 0;
matrix.m33 = -2 / (far - near);
matrix.m34 = 0;
matrix.m41 = tx;
matrix.m42 = ty;
matrix.m43 = tz;
matrix.m44 = 1;
this.multRight(matrix);
};
CanvasMatrix4.prototype.frustum = function(left, right, bottom, top, near, far)
{
var matrix = new CanvasMatrix4();
var A = (right + left) / (right - left);
var B = (top + bottom) / (top - bottom);
var C = -(far + near) / (far - near);
var D = -(2 * far * near) / (far - near);
matrix.m11 = (2 * near) / (right - left);
matrix.m12 = 0;
matrix.m13 = 0;
matrix.m14 = 0;
matrix.m21 = 0;
matrix.m22 = 2 * near / (top - bottom);
matrix.m23 = 0;
matrix.m24 = 0;
matrix.m31 = A;
matrix.m32 = B;
matrix.m33 = C;
matrix.m34 = -1;
matrix.m41 = 0;
matrix.m42 = 0;
matrix.m43 = D;
matrix.m44 = 0;
this.multRight(matrix);
};
CanvasMatrix4.prototype.perspective = function(fovy, aspect, zNear, zFar)
{
var top = Math.tan(fovy * Math.PI / 360) * zNear;
var bottom = -top;
var left = aspect * bottom;
var right = aspect * top;
this.frustum(left, right, bottom, top, zNear, zFar);
};
CanvasMatrix4.prototype.lookat = function(eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz)
{
var matrix = new CanvasMatrix4();
// Make rotation matrix
// Z vector
var zx = eyex - centerx;
var zy = eyey - centery;
var zz = eyez - centerz;
var mag = Math.sqrt(zx * zx + zy * zy + zz * zz);
if (mag) {
zx /= mag;
zy /= mag;
zz /= mag;
}
// Y vector
var yx = upx;
var yy = upy;
var yz = upz;
// X vector = Y cross Z
xx = yy * zz - yz * zy;
xy = -yx * zz + yz * zx;
xz = yx * zy - yy * zx;
// Recompute Y = Z cross X
yx = zy * xz - zz * xy;
yy = -zx * xz + zz * xx;
yx = zx * xy - zy * xx;
// cross product gives area of parallelogram, which is < 1.0 for
// non-perpendicular unit-length vectors; so normalize x, y here
mag = Math.sqrt(xx * xx + xy * xy + xz * xz);
if (mag) {
xx /= mag;
xy /= mag;
xz /= mag;
}
mag = Math.sqrt(yx * yx + yy * yy + yz * yz);
if (mag) {
yx /= mag;
yy /= mag;
yz /= mag;
}
matrix.m11 = xx;
matrix.m12 = xy;
matrix.m13 = xz;
matrix.m14 = 0;
matrix.m21 = yx;
matrix.m22 = yy;
matrix.m23 = yz;
matrix.m24 = 0;
matrix.m31 = zx;
matrix.m32 = zy;
matrix.m33 = zz;
matrix.m34 = 0;
matrix.m41 = 0;
matrix.m42 = 0;
matrix.m43 = 0;
matrix.m44 = 1;
matrix.translate(-eyex, -eyey, -eyez);