-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSiboglinidae_no_Fren.R
2315 lines (1872 loc) · 97 KB
/
Siboglinidae_no_Fren.R
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
#code for downloading sequences from genbank and cleaning up
#example code from
#http://www.jcsantosresearch.org/Class_2014_Spring_Comparative/pdf/week_2/Jan_13_15_2015_GenBank_part_2.pdf
setwd('~/jsb439@cornell.edu/amelia-bivalves/tubeworms')
require(ape)
require(seqinr)
require(plyr)
require(insect)
require(readxl)
require(phytools)
require(Rphylopars)
require(nlme)
require(geiger)
require(ggplot2)
require(gridExtra)
require(olsrr)
require(WGCNA)
require(evomap)
#setwd('~/jsb439@cornell.edu/amelia-bivalves')
source(file="/Users/cotinga/jsb439@cornell.edu/Code/deep_sea_evolution/functions.R")
#set up CO1
{
#read in the excel spreadsheet
data<-read_excel('~/jsb439@cornell.edu/amelia-bivalves/tubeworms/Tubeworms2.xlsx', sheet=2)
#extract only ingroup
ingroup <- data[data$is.ingroup=='1',]
ingroup_CO1_accession_numbers <- ingroup$CO1
CO1_accession_numbers <- ingroup_CO1_accession_numbers[!is.na(ingroup_CO1_accession_numbers)] #filter out NAs
ingroup_pruned <- ingroup[!is.na(ingroup$CO1),] #filter out rows without CO1 accession numbers (for species labels)
#extract only (outgroup)
outgroup <- data[data$is.outgroup==1,]
outgroup <- outgroup[!is.na(outgroup$CO1),]
outgroup_CO1_accession_numbers <- outgroup$CO1 #extract only CO1 accession numbers
CO1<-read.GenBank(c(CO1_accession_numbers, outgroup_CO1_accession_numbers), quiet=F)
SequenceNames<-paste(names(CO1), attr(CO1, "species"), sep ="_CO1_") #generate new vector of taxon IDs
#clean names
SequenceNames<-gsub(pattern = ".", "", SequenceNames,fixed = T)
SequenceNames<-gsub(pattern = "'", "", SequenceNames,fixed = T)
SequenceNames<-gsub(pattern = "-", "_", SequenceNames,fixed = T)
names(CO1) <- SequenceNames #assign new names
#write unaligned fasta out for alignment in MAAFT and then analysis in RAxML
#write.dna(CO1, format='fasta', file='unalignedtubeworms_2_19_22.fas')
}
## read in the tree generated by IQTREE, swap names
{
CO1_ML.unconstrained<-read.tree('~/jsb439@cornell.edu/amelia-bivalves/tubeworms/IQTREE/unconstrained/unconstrained.treefile')
# swaps<-c("AB088673_CO1_cf._Alaysia_sp._A4", "AB073491_CO1_Arcovestia_ivanovi", "AY129124_CO1_Lamellibrachia_cf._luymesi",
# "AY129132_CO1_Lamellibrachia_cf._luymesi", "EU046616_CO1_Lamellibrachia_anaximandri", "HQ154525_CO1_Lamellibrachia_sp._Lam1",
# "HQ154526_CO1_Lamellibrachia_sp._Lam2", "HQ396891_CO1_Lamellibrachia_sp._DBUA_01146", "MH670807_CO1_Lamellibrachia_donwalshi",
# "KJ566961_CO1_Lamellibrachia_sp._2_SP-2014", "D38030_CO1_Lamellibrachia_satsuma", "U74078_CO1_Basibranchia_sp.__Mariana_Trough_",
# "U74055_CO1_Lamellibrachia_barhami", "D38029_CO1_Lamellibrachia_sp.", "LC064365_CO1_Lamellibrachia_sp._GK-2015",
# "D50592_CO1_Lamellibrachia_sp.", "U74061_CO1_Lamellibrachia_columna", "AB055209_CO1_Lamellibrachia_sp._L4",
# "AB088674_CO1_Lamellibrachia_sp._L6", "AB055210_CO1_Lamellibrachia_sp._L5", "AB088675_CO1_Lamellibrachia_sp._L7",
# "AB264601_CO1_Lamellibrachia_juni", "AB242857_CO1_Oasisia_fujikurai", "FJ667533_CO1_Oasisia_sp._SBJ-2009",
# "U74069_CO1_Oasisia_alvinae", "FJ667534_CO1_Oasisia_sp._SBJ-2009", "FJ667535_CO1_Oasisia_sp._SBJ-2009",
# "U74056_CO1_Ridgeia_sp.__Southern_Explorer_Ridge_", "U74073_CO1_Ridgeia_piscesae", "U74075_CO1_Tevnia_jerichonana",
# "U74074_CO1_Riftia_pachyptila", "FM178480_CO1_Sclerolinum_contortum", "U74068_CO1__Loihi_Seamount__perviate_pogonophoran",
# "FJ347644_CO1_Sclerolinum_brattstromi", "FJ480347_CO1_Siboglinum_sp._1_SBJ-2009", "FJ480378_CO1_Siboglinum_sp._2_SBJ-2009",
# "FJ480394_CO1_Siboglinum_sp._3_SBJ-2009", "FJ480396_CO1_Siboglinum_sp._4_SBJ-2009", "FJ480358_CO1_Siboglinum_sp._5_SBJ-2009",
# "FJ480364_CO1_Siboglinum_sp._6_SBJ-2009", "FJ480360_CO1_Sibloglinidae_cf._Cyclobrachia_sp._SBJ-2009",
# "FJ480399_CO1_Siboglinum_poseidoni", "KJ789169_CO1_Siboglinum_ekmani", "FJ480389_CO1_Polybrachia_sp._2_SBJ-2009",
# "FJ480391_CO1_Polybrachia_sp._3_SBJ-2009", "FJ480393_CO1_Polybrachia_sp._1_SBJ-2009", "KJ789162_CO1_Galathealinum_brachiosum",
# "U74066_CO1_Galathealinum_brachiosum", "FJ480357_CO1_Bobmarleya_gadensis", "FJ480370_CO1_Spirobrachia_tripeira",
# "FJ483547_CO1_Spirobrachia_cf._grandis_SJ-2009", "KJ789171_CO1_Spirobrachia_sp._YL-2014", "FJ480376_CO1_Lamellisabella_denticulata",
# "KJ789170_CO1_Siboglinum_fiordicum", "FM178481_CO1_Oligobrachia_haakonmosbiensis", "MH619675_CO1_Oligobrachia_sp._1125-3",
# "FJ480388_CO1_Lamellisabella_sp._SBJ-2009", "AB259569_CO1_Osedax_japonicus", "DQ996629_CO1_Osedax_sp._3_SBJ-2006",
# "EU223340_CO1_Osedax_sp.__orange-collar_", "AY827562_CO1_Osedax_mucofloris", "FJ347615_CO1_Osedax_sp.__white-collar_",
# "FM998109_CO1_Osedax_sp._sagami-7", "JX280613_CO1_Osedax_sp.__MB16_", "AY586495_CO1_Osedax_frankpressi",
# "LC106303_CO1_Osedax_sp._1336_61_2", "LC381421_CO1_Osedax_braziliensis", "EU223307_CO1_Osedax_rubiplumus",
# "FM998082_CO1_Osedax_sp._sagami-4", "FM998081_CO1_Osedax_sp._sagami-3", "JF509949_CO1_Osedax_roseus",
# "JX280609_CO1_Osedax_sp.__MB17_", "EU223356_CO1_Osedax_sp.__nude-palp-A_", "EU236218_CO1_Osedax_sp.__nude-palp-B_",
# "KP119563_CO1_Osedax_sp._Nude-palp-G", "FJ347643_CO1_Osedax_sp.__nude-palp-F_", "KJ598040_CO1_Osedax_rogersi",
# "KF444422_CO1_Osedax_antarcticus", "KJ598038_CO1_Osedax_crouchi", "KJ598039_CO1_Osedax_nordenskjoeldi",
# "EU267675_CO1_Osedax_sp.__nude-palp-C_", "FM998107_CO1_Osedax_sp._sagami-6", "FJ347630_CO1_Osedax_sp.__nude-palp-D_",
# "FM998110_CO1_Osedax_sp._sagami-8", "FJ347632_CO1_Osedax_sp.__nude-palp-E_", "FJ347639_CO1_Osedax_sp.__green-palp_",
# "FM998083_CO1_Osedax_sp._sagami-5", "KT860548_CO1_Osedax_sp.__Mediterranea_", "KP119564_CO1_Osedax_priapus",
# "FJ347616_CO1_Osedax_sp.__yellow-patch_", "KT860545_CO1_Osedax_deceptionensis", "DQ996622_CO1_Osedax_sp._1_SBJ-2006",
# "JF317197_CO1_Hesiospina_aurantiaca", "MH202758_CO1_Amphisamytha_sp._n._1_YZ-2018", "MH202759_CO1_Amphisamytha_sp._n._2_YZ-2018",
# "KP144926_CO1_Nephtys_hombergii", "AY428839_CO1_Owenia_fusiformis", "MH202756_CO1_Hesiolyra_cf._bergi_YZ-2018",
# "AF317287_CO1_Seepiophila_jonesi", "AY129134_CO1_Escarpia_sp._GB425-GoM", "AY326303_CO1_Escarpia_southwardae",
# "U74065_CO1_Escarpia_spicata", "KJ566960_CO1_Escarpia_sp._SP-2014", "U74063_CO1_Escarpia_laminata", "JN021267_CO1_Escarpia_sp._1_JMP-2012",
# "KP203873_CO1_Escarpia_sp._MNHN_IU-2013-77", "KP203874_CO1_Escarpia_sp._MNHN_IU-2013-78", "D50594_CO1_Paraescarpia_cf._echinospica",
# "D50595_CO1_Paraescarpia_cf._echinospica", "AB088670_CO1_cf._Alaysia_sp._A1", "AB088671_CO1_cf._Alaysia_sp._A2",
# "AB088672_CO1_cf._Alaysia_sp._A3", "FJ667536_CO1_Alaysia_sp._SBJ-2009")
CO1_ML.unconstrained.boottrees <- read.tree('~/jsb439@cornell.edu/amelia-bivalves/tubeworms/IQTREE/unconstrained_fullboot/CP.txt.boottrees')
#CO1_ML.unconstrained$tip.label==unlist(lapply(strsplit(swaps, split="_"), `[`, 1))
#this looks ok
#CO1_ML.unconstrained$tip.label<-swaps
CO1_ML.constrained<-read.tree('~/jsb439@cornell.edu/amelia-bivalves/tubeworms/IQTREE/constrained/constrained.treefile')
# swaps<-c("AB088673_CO1_cf._Alaysia_sp._A4", "AB073491_CO1_Arcovestia_ivanovi", "AY129124_CO1_Lamellibrachia_cf._luymesi",
# "AY129132_CO1_Lamellibrachia_cf._luymesi", "EU046616_CO1_Lamellibrachia_anaximandri", "HQ154525_CO1_Lamellibrachia_sp._Lam1",
# "HQ154526_CO1_Lamellibrachia_sp._Lam2", "HQ396891_CO1_Lamellibrachia_sp._DBUA_01146", "MH670807_CO1_Lamellibrachia_donwalshi",
# "KJ566961_CO1_Lamellibrachia_sp._2_SP-2014", "D38030_CO1_Lamellibrachia_satsuma", "U74078_CO1_Basibranchia_sp.__Mariana_Trough_",
# "U74055_CO1_Lamellibrachia_barhami", "D38029_CO1_Lamellibrachia_sp.", "LC064365_CO1_Lamellibrachia_sp._GK-2015",
# "D50592_CO1_Lamellibrachia_sp.", "U74061_CO1_Lamellibrachia_columna", "AB055209_CO1_Lamellibrachia_sp._L4",
# "AB088674_CO1_Lamellibrachia_sp._L6", "AB055210_CO1_Lamellibrachia_sp._L5", "AB088675_CO1_Lamellibrachia_sp._L7",
# "AB264601_CO1_Lamellibrachia_juni", "FM178480_CO1_Sclerolinum_contortum", "FJ347644_CO1_Sclerolinum_brattstromi",
# "U74068_CO1__Loihi_Seamount__perviate_pogonophoran", "FJ480347_CO1_Siboglinum_sp._1_SBJ-2009",
# "FJ480378_CO1_Siboglinum_sp._2_SBJ-2009", "FJ480394_CO1_Siboglinum_sp._3_SBJ-2009", "FJ480396_CO1_Siboglinum_sp._4_SBJ-2009",
# "FJ480358_CO1_Siboglinum_sp._5_SBJ-2009", "FJ480364_CO1_Siboglinum_sp._6_SBJ-2009", "FJ480360_CO1_Sibloglinidae_cf._Cyclobrachia_sp._SBJ-2009",
# "FJ480399_CO1_Siboglinum_poseidoni", "KJ789169_CO1_Siboglinum_ekmani", "FJ480389_CO1_Polybrachia_sp._2_SBJ-2009",
# "FJ480391_CO1_Polybrachia_sp._3_SBJ-2009", "FJ480393_CO1_Polybrachia_sp._1_SBJ-2009", "KJ789162_CO1_Galathealinum_brachiosum",
# "U74066_CO1_Galathealinum_brachiosum", "FJ480357_CO1_Bobmarleya_gadensis", "FJ480370_CO1_Spirobrachia_tripeira",
# "FJ483547_CO1_Spirobrachia_cf._grandis_SJ-2009", "KJ789171_CO1_Spirobrachia_sp._YL-2014", "FJ480376_CO1_Lamellisabella_denticulata",
# "KJ789170_CO1_Siboglinum_fiordicum", "FM178481_CO1_Oligobrachia_haakonmosbiensis", "MH619675_CO1_Oligobrachia_sp._1125-3",
# "FJ480388_CO1_Lamellisabella_sp._SBJ-2009", "AB259569_CO1_Osedax_japonicus", "DQ996629_CO1_Osedax_sp._3_SBJ-2006",
# "EU223340_CO1_Osedax_sp.__orange-collar_", "AY827562_CO1_Osedax_mucofloris", "FJ347615_CO1_Osedax_sp.__white-collar_",
# "FM998109_CO1_Osedax_sp._sagami-7", "JX280613_CO1_Osedax_sp.__MB16_", "AY586495_CO1_Osedax_frankpressi",
# "LC106303_CO1_Osedax_sp._1336_61_2", "LC381421_CO1_Osedax_braziliensis", "EU223307_CO1_Osedax_rubiplumus",
# "FM998082_CO1_Osedax_sp._sagami-4", "FM998081_CO1_Osedax_sp._sagami-3", "JF509949_CO1_Osedax_roseus", "JX280609_CO1_Osedax_sp.__MB17_",
# "EU223356_CO1_Osedax_sp.__nude-palp-A_", "EU236218_CO1_Osedax_sp.__nude-palp-B_", "KP119563_CO1_Osedax_sp._Nude-palp-G",
# "FJ347643_CO1_Osedax_sp.__nude-palp-F_", "KJ598040_CO1_Osedax_rogersi", "KF444422_CO1_Osedax_antarcticus",
# "KJ598038_CO1_Osedax_crouchi", "KJ598039_CO1_Osedax_nordenskjoeldi", "EU267675_CO1_Osedax_sp.__nude-palp-C_",
# "FM998107_CO1_Osedax_sp._sagami-6", "FJ347630_CO1_Osedax_sp.__nude-palp-D_", "FM998110_CO1_Osedax_sp._sagami-8",
# "FJ347632_CO1_Osedax_sp.__nude-palp-E_", "FJ347639_CO1_Osedax_sp.__green-palp_", "FM998083_CO1_Osedax_sp._sagami-5",
# "KT860548_CO1_Osedax_sp.__Mediterranea_", "KP119564_CO1_Osedax_priapus", "FJ347616_CO1_Osedax_sp.__yellow-patch_",
# "KT860545_CO1_Osedax_deceptionensis", "DQ996622_CO1_Osedax_sp._1_SBJ-2006", "JF317197_CO1_Hesiospina_aurantiaca",
# "MH202758_CO1_Amphisamytha_sp._n._1_YZ-2018", "MH202759_CO1_Amphisamytha_sp._n._2_YZ-2018", "KP144926_CO1_Nephtys_hombergii",
# "AY428839_CO1_Owenia_fusiformis", "MH202756_CO1_Hesiolyra_cf._bergi_YZ-2018", "AB242857_CO1_Oasisia_fujikurai",
# "FJ667533_CO1_Oasisia_sp._SBJ-2009", "U74069_CO1_Oasisia_alvinae", "FJ667534_CO1_Oasisia_sp._SBJ-2009",
# "FJ667535_CO1_Oasisia_sp._SBJ-2009", "U74056_CO1_Ridgeia_sp.__Southern_Explorer_Ridge_", "U74073_CO1_Ridgeia_piscesae",
# "U74075_CO1_Tevnia_jerichonana", "U74074_CO1_Riftia_pachyptila", "AF317287_CO1_Seepiophila_jonesi",
# "AY129134_CO1_Escarpia_sp._GB425-GoM", "AY326303_CO1_Escarpia_southwardae", "KJ566960_CO1_Escarpia_sp._SP-2014",
# "U74065_CO1_Escarpia_spicata", "U74063_CO1_Escarpia_laminata", "JN021267_CO1_Escarpia_sp._1_JMP-2012",
# "KP203873_CO1_Escarpia_sp._MNHN_IU-2013-77", "KP203874_CO1_Escarpia_sp._MNHN_IU-2013-78", "D50594_CO1_Paraescarpia_cf._echinospica",
# "D50595_CO1_Paraescarpia_cf._echinospica", "AB088670_CO1_cf._Alaysia_sp._A1", "AB088671_CO1_cf._Alaysia_sp._A2",
# "AB088672_CO1_cf._Alaysia_sp._A3", "FJ667536_CO1_Alaysia_sp._SBJ-2009")
CO1_ML.constrained.boottrees <- read.tree('~/jsb439@cornell.edu/amelia-bivalves/tubeworms/IQTREE/constrained_fullboot/constrained.boottrees')
# CO1_ML.constrained$tip.label==unlist(lapply(strsplit(swaps, split="_"), `[`, 1))
# #this looks ok
# CO1_ML.constrained$tip.label<-swaps
}
# #testing
# assoc<-(cbind(CO1_ML.constrained$tip.label,CO1_ML.constrained$tip.label))
# obj<- cophylo(CO1_ML.unconstrained, CO1_ML.constrained, assoc=assoc, rotate=T )
# plot(obj,link.type="curved",link.lwd=3,link.lty="solid",link.col=make.transparent("blue",0.25),fsize=0.3)
#data checks
{
#checking that the tip names match the correct rows in the data table
sort(c(ingroup_pruned$TaxID, outgroup$TaxID))==sort(CO1_ML.unconstrained$tip.label)
#checking that the tip names match the correct rows in the data table
sort(c(ingroup_pruned$TaxID, outgroup$TaxID))==sort(CO1_ML.constrained$tip.label)
#CO1_ML.constrained$tip.label==CO1_ML.unconstrained$tip.label
}
#processing
{
#combined data for ingroup and outgorup
data<-rbind(ingroup_pruned,outgroup)
#convert data to data frame
data<-as.data.frame(data)
rownames(data)<-data$TaxID
}
#length(data$order)
# sort(rownames(data))==sort(CO1_ML.unconstrained$tip.label)
# sort(rownames(data))==sort(CO1_ML.constrained$tip.label)
#rerooting
{
root.unconstrained<-findMRCA(CO1_ML.unconstrained, tips=outgroup$TaxID) #untrimmed
root.constrained<-findMRCA(CO1_ML.constrained, tips=outgroup$TaxID) #untrimmed
#checking
plot(CO1_ML.unconstrained, cex=0.001)
nodelabels(node=root.unconstrained)
plot(CO1_ML.constrained, cex=0.001)
nodelabels(node=root.constrained)
#reroot on root
tree.unconstrained <- reroot(CO1_ML.unconstrained, node.number=root.unconstrained)
tree.constrained <- reroot(CO1_ML.constrained, node.number=root.constrained)
plot(tree.unconstrained)
plot(tree.constrained)
}
# par(mfrow=c(1,2))
# plot(ladderize(tree.unconstrained), cex=0.01)
# plot(ladderize(tree.constrained), cex=0.01)
#
# #checking
# assoc<-(cbind(tree.unconstrained$tip.label,tree.unconstrained$tip.label))
# obj<- cophylo(tree.unconstrained, tree.constrained, assoc=assoc, rotate=T )
# plot(obj,link.type="curved",link.lwd=3,link.lty="solid",link.col=make.transparent("blue",0.25),fsize=0.3)
#prune outgroups
{
#cut the outgroup tips
prunetree.constrained <- drop.tip(tree.constrained, tip=outgroup$TaxID)
prunetree.unconstrained <- drop.tip(tree.unconstrained, tip=outgroup$TaxID)
}
#
# #checking
# assoc<-(cbind(prunetree.constrained$tip.label,prunetree.constrained$tip.label))
# obj<- cophylo(prunetree.constrained, prunetree.unconstrained, assoc=assoc, rotate=T )
# plot(obj,link.type="curved",link.lwd=3,link.lty="solid",link.col=make.transparent("blue",0.25),fsize=0.3)
# #calculate root to tip lengths #commenting out for now
{
#root to tip length from the pruned tree as r2t
r2t.prunetree.constrained<-as.data.frame(diag(vcv.phylo(prunetree.constrained)))
#ensure they are the same order
r2t.prunetree.unconstrained <- (diag(vcv.phylo(prunetree.unconstrained)))
r2t.prunetree.unconstrained <- as.data.frame(r2t.prunetree.unconstrained[rownames(r2t.prunetree.constrained)])
}
#check constrained vs unconstrained bls
#plot(log(r2t.prunetree.constrained$`diag(vcv.phylo(prunetree.constrained))`)~ log(r2t.prunetree.unconstrained$`r2t.prunetree.unconstrained[rownames(r2t.prunetree.constrained)]`))
#constrained v unconstrained is mostly similar, some differences due to differences in root position
#data processing
{
#prune out outgroups
data<-data[!data$is.outgroup==1,]
#reorder the data frame to match r2t
#this also prunes out the outgroups from the dataset
data.constrained<-data[rownames(r2t.prunetree.constrained),]
data.unconstrained<-data[rownames(r2t.prunetree.unconstrained),]
#rownames(r2t.prunetree.constrained) == rownames(r2t.prunetree.unconstrained)
#checking names
#data.frame(sort(rownames(data.constrained)), sort(rownames(r2t.prunetree.constrained)), c(sort(rownames(data.constrained))== sort(rownames(r2t.prunetree.constrained))))
#data.frame(sort(rownames(data.unconstrained)), sort(rownames(r2t.prunetree.unconstrained)), c(sort(rownames(data.unconstrained))== sort(rownames(r2t.prunetree.unconstrained))))
#prune out rows with multiple habitat types
#get rid of Seep, OF
remove<-rownames(data.constrained[data.constrained$Habitat_Mod=='Seep, OF',]) #not being stored
data.constrained <- data.constrained[!data.constrained$Habitat_Mod=='Seep, OF',]
prunetree.constrained <- drop.tip(prunetree.constrained, tip=remove)
remove<-rownames(data.unconstrained[data.unconstrained$Habitat_Mod=='Seep, OF',]) #not being stored
data.unconstrained <- data.unconstrained[!data.unconstrained$Habitat_Mod=='Seep, OF',]
prunetree.unconstrained <- drop.tip(prunetree.unconstrained, tip=remove)
#get rid of vent, seep
remove<-rownames(data.constrained[data.constrained$Habitat_Mod=='Vent, Seep',])
data.constrained <- data.constrained[! data.constrained$Habitat_Mod=='Vent, Seep',]
prunetree.constrained<- drop.tip(prunetree.constrained, tip=remove)
remove<-rownames(data.unconstrained[data.unconstrained$Habitat_Mod=='Vent, Seep',])
data.unconstrained <- data.unconstrained[! data.unconstrained$Habitat_Mod=='Vent, Seep',]
prunetree.unconstrained<- drop.tip(prunetree.unconstrained, tip=remove)
#get rid of unknown
remove<-rownames(data.constrained[data.constrained$Habitat_Mod=='?',])
data.constrained <- data.constrained[!data.constrained$Habitat_Mod=='?',]
prunetree.constrained <- drop.tip(prunetree.constrained, tip=remove)
remove<-rownames(data.unconstrained[data.unconstrained$Habitat_Mod=='?',])
data.unconstrained <- data.unconstrained[!data.unconstrained$Habitat_Mod=='?',]
prunetree.unconstrained <- drop.tip(prunetree.unconstrained, tip=remove)
#get rid of Vent, Seep, OF
VSO<-rownames(data.constrained[data.constrained$Habitat_Mod=='Vent, Seep, OF',])
data.constrained <- data.constrained[!data.constrained$Habitat_Mod=='Vent, Seep, OF',]
prunetree.constrained <- drop.tip(prunetree.constrained, tip=VSO)
VSO<-rownames(data.unconstrained[data.unconstrained$Habitat_Mod=='Vent, Seep, OF',])
data.unconstrained <- data.unconstrained[!data.unconstrained$Habitat_Mod=='Vent, Seep, OF',]
prunetree.unconstrained <- drop.tip(prunetree.unconstrained, tip=VSO)
#get rid of Vent, Seep, Shallow
VSS<-rownames(data.constrained[data.constrained$Habitat_Mod=='Vent, Seep, Shallow',])
data.constrained <- data.constrained[!data.constrained$Habitat_Mod=='Vent, Seep, Shallow',]
prunetree.constrained <- drop.tip(prunetree.constrained, tip=VSS)
VSS<-rownames(data.unconstrained[data.unconstrained$Habitat_Mod=='Vent, Seep, Shallow',])
data.unconstrained <- data.unconstrained[!data.unconstrained$Habitat_Mod=='Vent, Seep, Shallow',]
prunetree.unconstrained <- drop.tip(prunetree.unconstrained, tip=VSS)
#get rid of Ambient
AMB<-rownames(data.constrained[data.constrained$Habitat_Mod=='Ambient',])
data.constrained <- data.constrained[!data.constrained$Habitat_Mod=='Ambient',]
prunetree.constrained <- drop.tip(prunetree.constrained, tip=AMB)
AMB<-rownames(data.unconstrained[data.unconstrained$Habitat_Mod=='Ambient',])
data.unconstrained <- data.unconstrained[!data.unconstrained$Habitat_Mod=='Ambient',]
prunetree.unconstrained <- drop.tip(prunetree.unconstrained, tip=AMB)
#get rid of Vent, Seep, Ambient
VSA<-rownames(data.constrained[data.constrained$Habitat_Mod=="Vent, Seep, Ambient",])
data.constrained <- data.constrained[!data.constrained$Habitat_Mod=="Vent, Seep, Ambient",]
prunetree.constrained <- drop.tip(prunetree.constrained, tip=VSA)
VSA<-rownames(data.unconstrained[data.unconstrained$Habitat_Mod=="Vent, Seep, Ambient",])
data.unconstrained <- data.unconstrained[!data.unconstrained$Habitat_Mod=="Vent, Seep, Ambient",]
prunetree.unconstrained <- drop.tip(prunetree.unconstrained, tip=VSA)
#get rid of Seep, Ambient
SA<-rownames(data.constrained[data.constrained$Habitat_Mod=="Seep, Ambient",])
data.constrained <- data.constrained[!data.constrained$Habitat_Mod=="Seep, Ambient",]
prunetree.constrained <- drop.tip(prunetree.constrained, tip=SA)
SA<-rownames(data.unconstrained[data.unconstrained$Habitat_Mod=="Seep, Ambient",])
data.unconstrained <- data.unconstrained[!data.unconstrained$Habitat_Mod=="Seep, Ambient",]
prunetree.unconstrained <- drop.tip(prunetree.unconstrained, tip=SA)
#remove frenulates
#get rid of Seep, Ambient
fren<-rownames(data.constrained[data.constrained$frenulate=="1",])
data.constrained <- data.constrained[!data.constrained$frenulate=="1",]
prunetree.constrained <- drop.tip(prunetree.constrained, tip=fren)
fren<-rownames(data.unconstrained[data.unconstrained$frenulate=="1",])
data.unconstrained <- data.unconstrained[!data.unconstrained$frenulate=="1",]
prunetree.unconstrained <- drop.tip(prunetree.unconstrained, tip=fren)
####
#get rid of everything that is not Vestimentifera
#NVES<-rownames(data[data$Lineage=='Vestimentifera',])
#data <- data[prunetree$tip.label %in% NVES,]
#prunetree<- drop.tip(prunetree, tip=prunetree$tip.label[!prunetree$tip.label %in% NVES])
####
#data.unconstrained$Lineage
}
#
# plot(prunetree.unconstrained, cex=0.1, no.margin=T)
# plot(prunetree.constrained, cex=0.1, no.margin=T)
# #checking
# assoc<-(cbind(prunetree.constrained$tip.label,prunetree.constrained$tip.label))
# obj<- cophylo(prunetree.constrained, prunetree.unconstrained, assoc=assoc, rotate=T)
# plot(obj,link.type="curved",link.lwd=3,link.lty="solid",link.col=make.transparent("blue",0.25),fsize=0.3)
#store data
{
#root to tip length from the pruned tree as r2t
r2t.constrained<-as.data.frame(diag(vcv.phylo(prunetree.constrained)))
#ensure they are the same order
r2t.unconstrained <- (diag(vcv.phylo(prunetree.unconstrained)))
r2t.unconstrained <- as.data.frame(r2t.unconstrained[rownames(r2t.constrained)])
#plot(log(r2t.unconstrained$`r2t.unconstrained[rownames(r2t.constrained)]`)~ log(r2t.constrained$`diag(vcv.phylo(prunetree.constrained))`))
#constrained v unconstrained is quite a large difference
}
# #plot(prunetree, cex=0.4, no.margin=T)
# length(data.constrained[,1]) #checks how many rows in the data
# prunetree.constrained #check how many tips and nodes
#
# length(data.unconstrained[,1]) #checks how many rows in the data
# prunetree.unconstrained #check how many tips and nodes
#store data
{
#store habitat data
x.constrained <- data.constrained$Habitat_Mod
names(x.constrained) <- rownames(data.constrained)
x.unconstrained <- data.unconstrained$Habitat_Mod
names(x.unconstrained) <- rownames(data.unconstrained)
#store r2t data
y.constrained <- r2t.constrained[,1]
names(y.constrained) <- rownames(r2t.constrained)
y.unconstrained <- r2t.unconstrained[,1]
names(y.unconstrained) <- rownames(r2t.unconstrained)
#store size data
z.unconstrained<-data.unconstrained$tube_mm
z.constrained<-data.constrained$tube_mm
names(z.unconstrained)<-rownames(r2t.unconstrained)
names(z.constrained)<-rownames(r2t.constrained)
##
#store diaphrahm data
dia.unconstrained<-data.unconstrained$`diaphragm_mm(vest or fore); "trunk" in osedax`
dia.constrained<-data.constrained$`diaphragm_mm(vest or fore); "trunk" in osedax`
names(dia.unconstrained)<-rownames(data.unconstrained)
names(dia.constrained)<-rownames(data.constrained)
###store size data
palp.unconstrained<-data.unconstrained$obt_palp_tent_12.28.22
palp.constrained<-data.constrained$obt_palp_tent_12.28.22
names(palp.unconstrained)<-rownames(data.unconstrained)
names(palp.constrained)<-rownames(data.unconstrained)
###
spread.unconstrained<-data.unconstrained$rate_cat
spread.constrained<-data.constrained$rate_cat
names(spread.unconstrained)<-rownames(data.unconstrained)
names(spread.constrained)<-rownames(data.constrained)
#checking names again
names(x.unconstrained) == names(y.unconstrained)
names(x.constrained) == names(y.constrained)
#####
#tmp<-data.frame(r2t=y, habitat=x, size=z, spread=spread)
#write.csv(tmp, file='r2t_habitat_size_spread.txt', row.names=T)
#####
}
#generate congruified tree
{
YuanningLe<-read.tree("~/jsb439@cornell.edu/amelia-bivalves/tubeworms/constraint_2_19_22.tre")
is.ultrametric(YuanningLe)
#YuanningLe<-drop.tip(YuanningLe)
#drop tips that have been pruned
YuanningLe <- drop.tip(YuanningLe, tip=c("FJ347644_CO1_Sclerolinum_brattstromi", "KJ789169_CO1_Siboglinum_ekmani", "KJ789170_CO1_Siboglinum_fiordicum", "U74065_CO1_Escarpia_spicata",
"HQ023477_CO1_Cirratulus_aff_cirratus_CMC02", "KY472732_CO1_Sabella_spallanzanii", "MN256592_CO1_Sternaspis_scutata", fren))
tax<-data.frame(row.names = YuanningLe$tip.label, taxonomy=YuanningLe$tip.label)
#YuanningLe$tip.label %in% prunetree.constrained$tip.label
#drop<-prunetree.constrained$tip.label[!prunetree.constrained$tip.label %in% YuanningLe$tip.label]
congruify.constrained<-congruify.phylo(YuanningLe, prunetree.constrained, taxonomy=tax, scale='treePL')
congruify.unconstrained<-congruify.phylo(YuanningLe, prunetree.unconstrained, taxonomy=tax, scale='treePL')
# par(mfrow=c(1,3))
# plot(congruify.constrained$phy, cex=0.5)
# axisPhylo()
# plot(prunetree.constrained, cex=0.5)
# axisPhylo()
# plot(YuanningLe, cex=0.8)
# axisPhylo()
#
# #testing
# assoc<-(cbind(YuanningLe$tip.label,YuanningLe$tip.label))
# obj<- cophylo(prunetree.unconstrained, YuanningLe, assoc=assoc, rotate=T )
# plot(obj,link.type="curved",link.lwd=3,link.lty="solid",link.col=make.transparent("blue",0.25),fsize=0.3)
#
# #testing
# assoc<-(cbind(YuanningLe$tip.label,YuanningLe$tip.label))
# obj<- cophylo(prunetree.constrained, YuanningLe, assoc=assoc, rotate=T )
# plot(obj,link.type="curved",link.lwd=3,link.lty="solid",link.col=make.transparent("blue",0.25),fsize=0.3)
#
# assoc<-(cbind(prunetree.constrained$tip.label,prunetree.constrained$tip.label))
# obj<- cophylo(prunetree.constrained, prunetree.unconstrained, assoc=assoc, rotate=T )
# plot(obj,link.type="curved",link.lwd=3,link.lty="solid",link.col=make.transparent("blue",0.25),fsize=0.3)
#
# assoc<-(cbind(sort(drop.tip(prunetree.constrained, drop)$tip.label),sort(drop.tip(prunetree.unconstrained, drop)$tip.label)))
# obj<- cophylo(drop.tip(prunetree.constrained, drop), drop.tip(prunetree.unconstrained, drop), assoc=assoc, rotate=T )
# plot(obj,link.type="curved",link.lwd=3,link.lty="solid",link.col=make.transparent("blue",0.25),fsize=0.3)
#
# #this looks OK, and it looks like the constraint worked (but it may not be very effective givein the limited sampling)
#
# #phytools r package anova
# #uses the raxml tree
# #nsim is the number of simulations, default is 1000
# #increasing the number seems to give more power
#
# #generating time tree for anova
# #prunetree.time<-chronos(prunetree, model='relaxed')
# #prunetree.time<-ladderize(prunetree.time, right=F)
# #plot(prunetree.time, cex=0.5)
# #axisPhylo()
}
#testFitchBeintema(prunetree.constrained)
#testFitchBeintema(prunetree.unconstrained)
#testFitchBeintemaNLME(prunetree.unconstrained)
#checking for node density artifact
path_lengths <- diag(vcv(prunetree.constrained))
node_paths <- nodepath(prunetree.constrained)
node_counts <- sapply(node_paths, length) - 1
node_data <- data.frame(
NodeCounts = node_counts, # x variable (speciation events from root)
TotalPathLength = path_lengths # y variable (genetic distance to root)
)
plot(node_data$TotalPathLength ~ node_data$NodeCounts)
abline(lm(node_data$TotalPathLength ~ node_data$NodeCounts))
summary(lm(node_data$TotalPathLength ~ node_data$NodeCounts))
# > summary(lm(node_data$TotalPathLength ~ node_data$NodeCounts))
#
# Call:
# lm(formula = node_data$TotalPathLength ~ node_data$NodeCounts)
#
# Residuals:
# Min 1Q Median 3Q Max
# -0.24030 -0.09607 0.01565 0.08947 0.24144
#
# Coefficients:
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) 0.621863 0.063597 9.778 7.56e-15 ***
# node_data$NodeCounts 0.017894 0.008603 2.080 0.0411 *
# ---
# Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#
# Residual standard error: 0.1188 on 72 degrees of freedom
# Multiple R-squared: 0.05668, Adjusted R-squared: 0.04358
# F-statistic: 4.326 on 1 and 72 DF, p-value: 0.04108
#####FOR RUNNING TRAITRATE#####
##setting up three comparisons ###
#vent vs OF, remove seep, comparison 1
#vent vs seep, remove OF, comparison 2
#seep vs OF, remove vent, comparison 3
#require(ips)
{
#set up the genetic datasets
fas<-read.FASTA("/Users/cotinga/jsb439@cornell.edu/amelia-bivalves/tubeworms/IQTREE/mafft_aligned_2_19_22_2-sequencher.fas")
#set up comparison 1
#prune out seep
remove1<-rownames(data[data$Habitat_Mod=='Seep',])
#remove seep tips from tree
traitRate1 <- drop.tip(congruify.constrained$phy, tip=remove1)
#write.tree(traitRate1, file="traitRate1.tre")
#create trait dataset
trim1 <- data.constrained[!data.constrained$Habitat_Mod=='Seep',]
trait1 <- as.numeric(as.factor(trim1$Habitat_Mod))-1
trait1 <- data.frame(trait= setNames(trait1,
rownames(trim1))[traitRate1$tip.label])
#ips:::write.fas(trait1,file="trait1.fas")
#extract the seqs
traitRate1.seq<-fas[traitRate1$tip.label]
#ips:::write.fas(traitRate1.seq, file="trait1.seq.fas")
#set up comparison 2
#prune out seep
remove2<-rownames(data.constrained[data.constrained$Habitat_Mod=='OF',])
#remove seep tips from tree
traitRate2 <- drop.tip(congruify.constrained$phy, tip=remove2)
#write.tree(traitRate2, file="traitRate2.tre")
#create trait dataset
trim2 <- data.constrained[!data.constrained$Habitat_Mod=='OF',]
trait2 <- as.numeric(as.factor(trim2$Habitat_Mod))-1
trait2<-data.frame(trait= setNames(trait2,
rownames(trim2))[traitRate2$tip.label])
#ips:::write.fas(trait2,file="trait2.fas")
#extract the seqs
traitRate2.seq<-fas[traitRate2$tip.label]
#ips:::write.fas(traitRate2.seq, file="trait2.seq.fas")
#set up comparison 3
#prune out vent
remove3<-rownames(data.constrained[data.constrained$Habitat_Mod=='Vent',])
#remove seep tips from tree
traitRate3 <- drop.tip(congruify.constrained$phy, tip=remove3)
#write.tree(traitRate3, file="traitRate3.tre")
#create trait dataset
trim3 <- data.constrained[!data.constrained$Habitat_Mod=='Vent',]
trait3 <- as.numeric(as.factor(trim3$Habitat_Mod))-1
trait3<-data.frame(trait= setNames(trait3,
rownames(trim3))[traitRate3$tip.label])
#ips:::write.fas(trait3,file="trait3.fas")
#extract the seqs
traitRate3.seq<-fas[traitRate3$tip.label]
#ips:::write.fas(traitRate3.seq, file="trait3.seq.fas")
#
# traitRate(phy=traitRate1, seq=as.matrix(traitRate1.seq), x=trait1,
# exec = "/Applications/Phylogenetics/TraitRateProp_source_20161221/programs/traitRateProp")
#
# traitRate(phy=traitRate2, seq=as.matrix(traitRate2.seq), x=trait2,
# exec = "/Applications/Phylogenetics/TraitRateProp_source_20161221/programs/traitRateProp")
#
}
#checking genetic distances
{
require(phangorn)
#dna.dist<-dist.ml(phyDat(fas, type='DNA') , model = "F81")
dna.dist<-dist.dna(fas, model = "F81")
#str(dna.dist)
tree.dist<-cophenetic.phylo(prunetree.constrained)
#str(tree.dist)
dist.comp<-generate_distance_dataframe(dna_dist=dna.dist, tree_dist=tree.dist)
plot(log(dist.comp$DNA_Distance)~log(dist.comp$Tree_Distance))
# Remove rows with NA, Inf, or undefined values
cleaned_dist_comp <- dist.comp[complete.cases(dist.comp) &
is.finite(log(dist.comp$DNA_Distance)) &
is.finite(log(dist.comp$Tree_Distance)), ]
# Retain only unique pairs of distances
unique_dist_comp <- cleaned_dist_comp[!duplicated(t(apply(cleaned_dist_comp[, c("DNA_Distance", "Tree_Distance")], 1, sort))), ]
# Fit the linear model using unique pairs
lm_model <- lm(log(DNA_Distance) ~ log(Tree_Distance), data = unique_dist_comp)
# Display the summary of the linear model
summary(lm_model)
# Plot the data
plot(log(unique_dist_comp$DNA_Distance) ~ log(unique_dist_comp$Tree_Distance),
main = "Log-Log Plot of DNA vs Tree Distances (Unique Pairs)",
xlab = "Log(Tree Distance)",
ylab = "Log(DNA Distance)")
abline(lm_model, col = "red", lwd = 2)
# Fit the linear model
lm_model <- lm(log(DNA_Distance) ~ log(Tree_Distance), data = cleaned_dist_comp)
summary(lm_model)
#0.9046
}
#processing traitrate output
{
#read in traitRate results
traitRateRes1<-read.csv(file="/Users/cotinga/jsb439@cornell.edu/amelia-bivalves/tubeworms/traitRate1_nofren/TraitRateProp_per_site_report.csv")
traitRateRes1.bayes<-traitRateRes1$bayes_factor
traitRateRes1.bayes[traitRateRes1.bayes < 3] <- -2
traitRateRes1.bayes[traitRateRes1.bayes > 20] <- 20
#append 42 blanks to get it in the right frame
#traitRateRes1.bayes<- c(rep(-10,42), traitRateRes1.bayes)
traitRateRes2<-read.csv(file="/Users/cotinga/jsb439@cornell.edu/amelia-bivalves/tubeworms/traitRate2_nofren/TraitRateProp_per_site_report.csv")
traitRateRes2.bayes <- traitRateRes2$bayes_factor
traitRateRes2.bayes[traitRateRes2.bayes < 3] <- -2
traitRateRes2.bayes[traitRateRes2.bayes > 20 ] <- 20
#append 42 blanks to get it in the right frame
#traitRateRes2.bayes<- c(rep(-10,42), traitRateRes2.bayes)
traitRateRes3<-read.csv(file="/Users/cotinga/jsb439@cornell.edu/amelia-bivalves/tubeworms/traitRate3_nofren/TraitRateProp_per_site_report.csv")
traitRateRes3.bayes <- traitRateRes3$bayes_factor
traitRateRes3.bayes[traitRateRes3.bayes < 3] <- -2
traitRateRes3.bayes[traitRateRes3.bayes > 20] <- 20
#append 42 blanks to get it in the right frame
#traitRateRes3.bayes<- c(rep(-10,42), traitRateRes3.bayes)
}
#set up simmap visualizations to show the trait distributions
{
traitRate1.simmap<-make.simmap(traitRate1,setNames(trait1$trait, rownames(trait1)),
model="ARD", nsim=100)
traitRate1.simmap.obj<-densityMap(traitRate1.simmap,states=levels(setNames(trait1$trait, rownames(trait1)))[2:1],plot=FALSE)
traitRate2.simmap<-make.simmap(traitRate2,setNames(trait2$trait, rownames(trait2)),
model="ARD", nsim=100)
traitRate2.simmap.obj<-densityMap(traitRate2.simmap,states=levels(setNames(trait2$trait, rownames(trait2)))[2:1],plot=FALSE)
traitRate3.simmap<-make.simmap(traitRate3,setNames(trait3$trait, rownames(trait3)),
model="ARD", nsim=100)
traitRate3.simmap.obj<-densityMap(traitRate3.simmap,states=levels(setNames(trait3$trait, rownames(trait3)))[2:1],plot=FALSE)
}
require('WGCNA')
#code to generate traitRate plots
pdf(file="traitRate_tubewormsCO1_no_frenulates.pdf", height=4, width = 8.5)
{
#set up the plotting region for traitrate
{
layout.matrix <- matrix(c(1,3,5,2,4,6), nrow = 3, ncol = 2)
layout(mat = layout.matrix,
heights = c(1, 1, 1), # Heights
widths = c(0.35, 2.5, 2.5)) # Widths of the two columns
}
#par(mfrow=c(3,1))
#par(mar=c(7.1, 4.1, 7.1, 2.1), xpd=F)
#labels reflect CO1 position in
#NC_056377.1:14737-16287_Mytella_strigata
#plot 1
{
plot(traitRate1.simmap.obj, legend=F, ftype="off", lwd=1,
mar=c(1.1, 1, 2, 0.5))
par(mar=c(1, 4.1, 2, 2.1), xpd=F)
plot(traitRateRes1.bayes,
ylab="BF Vent vs OF", ylim=c(2,20),
bty='n', xlim=c(0,1200),
pch=19,
col=rep(c("red", "red", "black"), 508),
xaxt="n", yaxt='n',
xlab="COX1 position", main="COX1 site position")
axis(1, at = c(1, 200, 400, 600, 800, 1000, 1200)-1,
labels = rep("", 7))#,c(1, 100, 200, 300, 400, 500, 600, 700))
axis(2, at = c(3,10,15,20), las=2, cex.axis=0.8)
#lines(traitRateRes1.bayes, col = "black")
labelPoints(
x=1:length(traitRateRes1.bayes), y=traitRateRes1.bayes,
labels=1:length(traitRateRes1.bayes),
cex = 0.6, offs = 0.05, xpd = F,
jiggle = 0, protectEdges = F,
doPlot = TRUE, col="black")
}
#plot 2
{
plot(traitRate2.simmap.obj, legend=F, ftype="off", lwd=1,
mar=c(1.1, 1, 2, 0.5))
par(mar=c(1, 4.1, 2, 2.1), xpd=F)
plot(traitRateRes2.bayes,
ylab="BF Vent vs Seep", ylim=c(2,20),
bty='n', xlim=c(0,1200),
pch=19,
col=rep(c("red", "red", "black"), 508),
xaxt="n", yaxt='n',
xlab="COX1 position")
axis(1, at = c(1, 200, 400, 600, 800, 1000, 1200)-1,
labels = rep("", 7))
axis(2, at = c(3,10,15,20), las=2, cex.axis=0.8)
labelPoints(
x=1:length(traitRateRes2.bayes), y=traitRateRes2.bayes,
labels=1:length(traitRateRes2.bayes),
cex = 0.6, offs = 0.05, xpd = F,
jiggle = 0, protectEdges = F,
doPlot = TRUE)
#lines(traitRateRes2.bayes, col = "black")
}
#plot 3
{
plot(traitRate3.simmap.obj, legend=F, ftype="off", lwd=1,
mar=c(2, 1, 1.5, 0.5))
par(mar=c(2, 4.1, 1, 2.1), xpd=F)
plot(traitRateRes3.bayes,
ylab="BF Seep vs OF", ylim=c(2,23),
bty='n', xlim=c(0,1200),
pch=19,
col=rep(c("red", "red", "black"), 508),
xaxt="n", yaxt='n',
xlab="COX1 position")
axis(1, at = c(1, 200, 400, 600, 800, 1000, 1200)-1,
labels = c(1, 200, 400, 600, 800, 1000, 1200))
axis(2, at = c(3,10,15,20), las=2, cex.axis=0.8)
labelPoints(
x=1:length(traitRateRes3.bayes), y=traitRateRes3.bayes,
labels=1:length(traitRateRes3.bayes),
cex = 0.6, offs = 0.05, xpd = F,
jiggle = 0, protectEdges = F,
doPlot = TRUE)
#lines(traitRateRes3.bayes, col = "black")
}
}
dev.off()
#what % of significant sites are first or second codon positions.
mean(c(
5/sum(traitRateRes1.bayes >= 1),
3/sum(traitRateRes2.bayes >= 1),
2/sum(traitRateRes3.bayes >= 1)
))
sd(c(
5/sum(traitRateRes1.bayes >= 1),
3/sum(traitRateRes2.bayes >= 1),
2/sum(traitRateRes3.bayes >= 1)
))
#####END FOR RUNNING TRAITRATE#####
boxplot((log(y.unconstrained))~x.constrained, varwidth=T)
boxplot((log(y.constrained))~x.constrained, varwidth=T)
#store data
{
prunetree.constrained.time<-congruify.constrained$phy
prunetree.unconstrained.time<-congruify.unconstrained$phy
#reorder
x.unconstrained<-x.unconstrained[prunetree.constrained.time$tip.label]
x.constrained<-x.constrained[prunetree.constrained.time$tip.label]
y.unconstrained<-y.unconstrained[prunetree.constrained.time$tip.label]
y.constrained<-y.constrained[prunetree.constrained.time$tip.label]
z.constrained<-z.constrained[prunetree.constrained.time$tip.label]
z.unconstrained<-z.unconstrained[prunetree.constrained.time$tip.label]
}
#uses the 'time calibrated tree'
#run phylogenetic anovas
{
phyl.aov.constrained <- phylANOVA(prunetree.constrained.time, x = x.unconstrained, y = log(y.constrained), nsim=10000, posthoc=T, p.adj = 'BH')
# ANOVA table: Phylogenetic ANOVA
#
# Response: y
# Sum Sq Mean Sq F value Pr(>F)
# x 1.032396 0.516198 36.534317 0.3148
# Residual 1.003168 0.014129
#
# P-value based on simulation.
# ---------
#
# Pairwise posthoc test using method = "BH"
#
# Pairwise t-values:
# OF Seep Vent
# OF 0.000000 7.616377 5.818119
# Seep -7.616377 0.000000 -1.996350
# Vent -5.818119 1.996350 0.000000
#
# Pairwise corrected P-values:
# OF Seep Vent
# OF 1.00000 0.33765 0.4111
# Seep 0.33765 1.00000 0.3261
# Vent 0.41110 0.32610 1.0000
# ---------
#phyl.aov.constrained <- phylANOVA(prunetree.constrained.time, x = x.unconstrained, y = log(y.constrained), nsim=10000, posthoc=T, p.adj = 'none')
# ANOVA table: Phylogenetic ANOVA
#
# Response: y
# Sum Sq Mean Sq F value Pr(>F)
# x 1.032396 0.516198 36.534317 0.3144
# Residual 1.003168 0.014129
#
# P-value based on simulation.
# ---------
#
# Pairwise posthoc test using method = "none"
#
# Pairwise t-values:
# OF Seep Vent
# OF 0.000000 7.616377 5.818119
# Seep -7.616377 0.000000 -1.996350
# Vent -5.818119 1.996350 0.000000
#
# Pairwise corrected P-values:
# OF Seep Vent
# OF 1.0000 0.2271 0.4153
# Seep 0.2271 1.0000 0.1045
# Vent 0.4153 0.1045 1.0000
# ---------
#
phyl.aov.unconstrained <- phylANOVA(prunetree.constrained.time, x = x.unconstrained, y = log(y.unconstrained), nsim=1000, posthoc=T, p.adj = 'BH')
# ANOVA table: Phylogenetic ANOVA
#
# Response: y
# Sum Sq Mean Sq F value Pr(>F)
# x 3.504810 1.752405 126.600607 0.023
# Residual 0.982782 0.013842
#
# P-value based on simulation.
# ---------
#
# Pairwise posthoc test using method = "BH"
#
# Pairwise t-values:
# OF Seep Vent
# OF 0.0000 12.851003 12.544301
# Seep -12.8510 0.000000 -1.212154
# Vent -12.5443 1.212154 0.000000
#
# Pairwise corrected P-values:
# OF Seep Vent
# OF 1.0000 0.036 0.0495
# Seep 0.0360 1.000 0.3280
# Vent 0.0495 0.328 1.0000
# ---------
#phyl.aov.unconstrained <- phylANOVA(prunetree.constrained.time, x = x.unconstrained, y = log(y.unconstrained), nsim=10000, posthoc=T, p.adj = 'none')
# ANOVA table: Phylogenetic ANOVA
#
# Response: y
# Sum Sq Mean Sq F value Pr(>F)
# x 3.504810 1.752405 126.600607 0.0279
# Residual 0.982782 0.013842
#
# P-value based on simulation.
# ---------
#
# Pairwise posthoc test using method = "none"
#
# Pairwise t-values:
# OF Seep Vent
# OF 0.0000 12.851003 12.544301
# Seep -12.8510 0.000000 -1.212154
# Vent -12.5443 1.212154 0.000000
#
# Pairwise corrected P-values:
# OF Seep Vent
# OF 1.0000 0.0167 0.0429
# Seep 0.0167 1.0000 0.3329
# Vent 0.0429 0.3329 1.0000
# ---------
#phyl.aov.unconstrained.alt <- phylANOVA(prunetree.constrained.time, x = x.unconstrained, y = log(y.unconstrained), nsim=10000, posthoc=T)
phyl.aov.unconstrained.alt <- phylANOVA(prunetree.unconstrained.time, x = x.unconstrained, y = log(y.unconstrained), nsim=10000, posthoc=T)
# ANOVA table: Phylogenetic ANOVA
#
# Response: y
# Sum Sq Mean Sq F value Pr(>F)
# x 3.504810 1.752405 126.600607 0.0216
# Residual 0.982782 0.013842
#
# P-value based on simulation.
# ---------
#
# Pairwise posthoc test using method = "holm"
#
# Pairwise t-values:
# OF Seep Vent
# OF 0.0000 12.851003 12.544301
# Seep -12.8510 0.000000 -1.212154
# Vent -12.5443 1.212154 0.000000
#
# Pairwise corrected P-values:
# OF Seep Vent
# OF 1.0000 0.0384 0.0714
# Seep 0.0384 1.0000 0.4475
# Vent 0.0714 0.4475 1.0000
# ---------
phyl.aov.constrained
phyl.aov.unconstrained
phyl.aov.unconstrained.alt ## this generates some interesting results
#phyl.aov.unconstrained.alt2
}
#phylogenetic anova from geiger r package
#run overall anova