-
Notifications
You must be signed in to change notification settings - Fork 1
/
GO.step10.run_glm.R
2422 lines (1930 loc) · 170 KB
/
GO.step10.run_glm.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
# ################################################################################################################################################################################################################
# GO.step10.run_glm.R
#
# CC-BY Lee Edsall
# email: le49@duke.edu
# Twitter: @LeeEdsall
#
# This script was used to analyze data for Edsall et al. 2019 which compared DNase-seq data from 5 primates (human, chimpanzee, gorilla, orangutan, macaque)
#
# There are 3 main analysis sections
# 1. Run DSS on all of the DHS sites to get the dispersion and normalization parameters
# 2. Iterate through the DHS sites to fit the glm model and perform the gateway test
# 3. Iterate through the differential DHS sites to perform the constraint tests and determine the type of change
# Note: it is necessary to re-fit the glm in this step in order to extract the variance-covariance matrix
# Sections 2 and 3 could be combined, but conceptually they make more sense when split into two sections
#
# There are 10 sections
# SECTION 1 OF 10: SETUP
# SECTION 2 OF 10: READ THE SCORE FILE AND PREPARE IT FOR THE GLM FUNCTION
# SECTION 3 OF 10: RUN DSS TO GET THE DISPERSION AND NORMALIZATION PARAMETERS
# SECTION 4 OF 10: GENERATE THE GLM AND PERFORM THE GATEWAY TEST
# SECTION 5 OF 10: PERFORM CONSTRAINT TESTS AND DETERMINE TYPE OF CHANGE
# SECTION 6 OF 10: UPDATE AND FINALIZE THE "results_all_sites" DATA FRAME
# SECTION 7 OF 10: CALCULATE TOTALS AND CREATE TABLES CONTAINING SUBSETS OF THE RESULTS
# SECTION 8 OF 10: WRITE RESULTS TO FILES
# SECTION 9 OF 10: CALCULATE CHECKSUM AND PRINT STATS
# SECTION 10 OF 10: FINISH UP
#
# Input file information
# 1. The replicates are in the columns and the locations are in the rows
# 2. The scores are integers and have not been normalized
#
# ################################################################################################################################################################################################################
# ################################################################################################################################################################################################################
# SECTION 1 OF 10: SETUP
# 1. load libraries
# 2. set global options
# 3. set global variables
# 4. open connection to log file
# ################################################################################################################################################################################################################
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# DSS Dispersion shrinkage for sequencing data
# Citation: Wu H, Wang C, Wu Z. 2013. A new shrinkage estimator for dispersion improves differential expression detection in RNA-seq data. Biostatistics 14(2):232-243.
#
# MASS Contains the negative.binomial function needed for the GLM
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
library(DSS)
library(MASS)
options(scipen=0)
pvalue_threshold = .01
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Open the log file; write date and options
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
sink(file="LOG.step10.run_glm.R", append=FALSE, type="output", split=TRUE)
cat("-------------------------------------------------------------------------------------------------\n")
cat("BEGIN:", date(), "\n")
cat("-------------------------------------------------------------------------------------------------\n\n")
cat("Using the following settings\n")
cat("\tLog file: glm_analysis.log\n")
cat("\tp-value threshold:", pvalue_threshold, "\n\n")
# ################################################################################################################################################################################################################
# END SECTION 1 OF 10: SETUP
# ################################################################################################################################################################################################################
# ################################################################################################################################################################################################################
# SECTION 2 OF 10: READ THE SCORE FILE AND PREPARE IT FOR THE GLM FUNCTION
# Locations are in rows and scores are in columns
# Column information
# Column 1: chrom Chromosome
# Column 2: start Start
# Column 3: end End
# Column 4: h1 Human replicate 1 read counts
# Column 5: h2 Human replicate 2 read counts
# Column 6: h3 Human replicate 3 read counts
# Column 7: c1 Chimpanzee replicate 1 read counts
# Column 8: c2 Chimpanzee replicate 2 read counts
# Column 9: c3 Chimpanzee replicate 3 read counts
# Column 10: g1 Gorilla replicate 1 read counts
# Column 11: g2 Gorilla replicate 2 read counts
# Column 12: g3 Gorilla replicate 3 read counts
# Column 13: o1 Orangutan replicate 1 read counts
# Column 14: o2 Orangutan replicate 2 read counts
# Column 15: o3 Orangutan replicate 3 read counts
# Column 16: m1 Macaque replicate 1 read counts
# Column 17: m2 Macaque replicate 2 read counts
# Column 18: m3 Macaque replicate 3 read counts
# Column 19: PLoS_overlap Information about overlapping with the regions from Shibata et al. PLoS Genetics 2012
#
# Create a data frame to use in the GLM.
# 1. Remove the location information
# 2. Transpose the data frame so the scores are in the rows and the locations are in the columns
# ################################################################################################################################################################################################################
# ================================================================================================================================================================================================================
# Read the score file
# ================================================================================================================================================================================================================
cat("Reading score file (glm_analysis.input_file.txt) ... \n")
scores_with_locations <- as.data.frame(read.table("all_DHS_sites.passed_coverage_filter.with_non_normalized_scores.zero_filtered.txt.with_PLoS_overlap_information", sep="\t", header=FALSE))
colnames(scores_with_locations) <- c("chrom","start","end", "h1", "h2", "h3", "c1", "c2", "c3", "g1", "g2", "g3", "o1", "o2", "o3", "m1", "m2", "m3", "PLoS_overlap")
rownames(scores_with_locations) <- paste(scores_with_locations$chrom, scores_with_locations$start, scores_with_locations$end, scores_with_locations$PLoS_overlap, sep=":")
count_all_sites = nrow(scores_with_locations)
# ================================================================================================================================================================================================================
# Create a version of the score table to use in the GLM formula
# Remove 4 columns containing non-score information: chrom, start, end, PLoS_overlap
# Transpose the table so the locations are in the columns and the replicates are in the rows
# Change the row names (which used to be column names) to use the Y nomenclature
# ================================================================================================================================================================================================================
scores <- t(scores_with_locations[,4:18])
rownames(scores) <- c("Yh1", "Yh2", "Yh3", "Yc1", "Yc2", "Yc3", "Yg1", "Yg2", "Yg3", "Yo1", "Yo2", "Yo3", "Ym1", "Ym2", "Ym3")
# ================================================================================================================================================================================================================
# Finish up
# ================================================================================================================================================================================================================
cat("done.\n\n")
cat("There are", count_all_sites, "sites.\n\n")
# ################################################################################################################################################################################################################
# END SECTION 2 OF 10: READ THE SCORE FILE AND PREPARE IT FOR THE GLM FUNCTION
# ################################################################################################################################################################################################################
# ################################################################################################################################################################################################################
# SECTION 3 OF 10: RUN DSS TO GET THE DISPERSION AND NORMALIZATION PARAMETERS
# ################################################################################################################################################################################################################
cat("Running DSS to get dispersion and normalization parameters ... \n")
# ================================================================================================================================================================================================================
# Set up the read counts matrix
# Remove 4 columns containing non-score information: chrom, start, end, PLoS_overlap
# Convert from a data.frame to a matrix
# ================================================================================================================================================================================================================
dss_scores <- as.matrix(scores_with_locations[,4:18])
# ================================================================================================================================================================================================================
# Set up the design matrix used by DSS
# The species order is human, chimpanzee, gorilla, orangutan, macaque
# The design matrix gets automatically sorted alphabetically, so to maintain the order we want, add numerals before the species name
# ================================================================================================================================================================================================================
dss_design=data.frame(species=c(rep("1human", 3), rep("2chimpanzee", 3), rep("3gorilla", 3), rep("4orangutan", 3), rep("0macaque", 3)))
rownames(dss_design) <- c("h1", "h2", "h3", "c1", "c2", "c3", "g1", "g2", "g3", "o1", "o2", "o3", "m1", "m2", "m3")
dss_X <- model.matrix(~species, data=dss_design)
# ================================================================================================================================================================================================================
# Run DSS
# 1. Create the seqData object
# 2. Calculate the normalization offsets (stored in library_size_offset_vector)
# 3. Estimate the dispersion parameter (stored in dispersion_parameter_vector)
# ================================================================================================================================================================================================================
dss_seqData=newSeqCountSet(dss_scores, as.data.frame(dss_X))
dss_seqData=estNormFactors(dss_seqData, method="total")
dss_seqData=estDispersion(dss_seqData)
library_size_offset_vector <- normalizationFactor(dss_seqData)
dispersion_parameter_vector <- dispersion(dss_seqData)
cat("done.\n\n")
# ################################################################################################################################################################################################################
# END SECTION 3 OF 10: RUN DSS TO GET THE DISPERSION AND NORMALIZATION PARAMETERS
# ################################################################################################################################################################################################################
# ################################################################################################################################################################################################################
# SECTION 4 OF 10: GENERATE THE GLM AND PERFORM THE GATEWAY TEST
# Loop through the scores table and for each location, generate the GLM, perform the Chi-squared test, and store the results in a matrix called "results_all_sites"
#
# matrix results_all_sites
# Locations are in rows and results are in columns
# Column information
# Column 1: width DHS site width
# Column 2: dispersion Dispersion parameter (comes from DSS)
# Column 3: offset_h1 Normalization factor for human replicate 1 (comes from DSS)
# Column 4: offset_h2 Normalization factor for human replicate 2 (comes from DSS)
# Column 5: offset_h3 Normalization factor for human replicate 3 (comes from DSS)
# Column 6: offset_c1 Normalization factor for chimpanzee replicate 1 (comes from DSS)
# Column 7: offset_c2 Normalization factor for chimpanzee replicate 2 (comes from DSS)
# Column 8: offset_c3 Normalization factor for chimpanzee replicate 3 (comes from DSS)
# Column 9: offset_g1 Normalization factor for gorilla replicate 1 (comes from DSS)
# Column 10: offset_g2 Normalization factor for gorilla replicate 2 (comes from DSS)
# Column 11: offset_g3 Normalization factor for gorilla replicate 3 (comes from DSS)
# Column 12: offset_o1 Normalization factor for orangutan replicate 1 (comes from DSS)
# Column 13: offset_o2 Normalization factor for orangutan replicate 2 (comes from DSS)
# Column 14: offset_o3 Normalization factor for orangutan replicate 3 (comes from DSS)
# Column 15: offset_m1 Normalization factor for macaque replicate 1 (comes from DSS)
# Column 16: offset_m2 Normalization factor for macaque replicate 2 (comes from DSS)
# Column 17: offset_m3 Normalization factor for macaque replicate 3 (comes from DSS)
# Column 18: Bh Human beta value (comes from the GLM)
# Column 19: Bh_se Human beta standard error (comes from the GLM)
# Column 20: Bh_p Human beta p-value (comes from the GLM)
# Column 21: Bc himpanzee beta value (comes from the GLM)
# Column 22: Bc_se Chimpanzee beta standard error (comes from the GLM)
# Column 23: Bc_p Chimpanzee beta p-value (comes from the GLM)
# Column 24: Bg Gorilla beta value (comes from the GLM)
# Column 25: Bg_se Gorilla beta standard error (comes from the GLM)
# Column 26: Bg_p Gorilla beta p-value (comes from the GLM)
# Column 27: Bo Orangutan beta value (comes from the GLM)
# Column 28: Bo_se Orangutan beta standard error (comes from the GLM)
# Column 29: Bo_p Orangutan beta p-value (comes from the GLM)
# Column 30: Bm Macaque beta value (comes from the GLM)
# Column 31: Bm_se Macaque beta standard error (comes from the GLM)
# Column 32: Bm_p Macaque beta p-value (comes from the GLM)
# Column 33: gateway_original_pvalue Gateway original p-value (the p-value for the GLM, calculated using a Chi-squared test)
# Column 34: gateway_adjusted_pvalue Gateway adjusted p-value (adjusted using the Benjamini-Hochberg correction)
# Column 35: human_effect_size Human effect size (beta value divided by standard error)
# Column 36: chimpanzee_effect_size Chimpanzee effect size (beta value divided by standard error)
# Column 37: gorilla_effect_size Gorilla effect size (beta value divided by standard error)
# Column 38: orangutan_effect_size Orangutan effect size (beta value divided by standard error)
# Column 39: macaque_effect_size Macaque effect size (beta value divided by standard error)
# Column 40: h1 Human replicate 1 read counts (from input file)
# Column 41: h2 Human replicate 2 read counts (from input file)
# Column 42: h3 Human replicate 3 read counts (from input file)
# Column 43: c1 Chimpanzee replicate 1 read counts (from input file)
# Column 44: c2 Chimpanzee replicate 2 read counts (from input file)
# Column 45: c3 Chimpanzee replicate 3 read counts (from input file)
# Column 46: g1 Gorilla replicate 1 read counts (from input file)
# Column 47: g2 Gorilla replicate 2 read counts (from input file)
# Column 48: g3 Gorilla replicate 3 read counts (from input file)
# Column 49: o1 Orangutan replicate 1 read counts (from input file)
# Column 50: o2 Orangutan replicate 2 read counts (from input file)
# Column 51: o3 Orangutan replicate 3 read counts (from input file)
# Column 52: m1 Macaque replicate 1 read counts (from input file)
# Column 53: m2 Macaque replicate 2 read counts (from input file)
# Column 54: m3 Macaque replicate 3 read counts (from input file)
# ################################################################################################################################################################################################################
# ================================================================================================================================================================================================================
# Create the X vectors for the GLM function
# The X vectors associate the samples with the species
# Create one X vector for each non-macaque species
# Because we're using macaque as the outgroup ("response element"), we don't need to create a species vector for it
# ================================================================================================================================================================================================================
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Human
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Xh <- as.data.frame(c(1,1,1, 0,0,0, 0,0,0, 0,0,0, 0,0,0), nrow=15, ncol=1, byrow=TRUE)
rownames(Xh) <- c("h1", "h2", "h3", "c1", "c2", "c3", "g1", "g2", "g3", "o1", "o2", "o3", "m1", "m2", "m3")
colnames(Xh) <- "Xh"
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Chimpanzee
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Xc <- as.data.frame(c(0,0,0, 1,1,1, 0,0,0, 0,0,0, 0,0,0), nrow=15, ncol=1, byrow=TRUE)
rownames(Xc) <- c("h1", "h2", "h3", "c1", "c2", "c3", "g1", "g2", "g3", "o1", "o2", "o3", "m1", "m2", "m3")
colnames(Xc) <- "Xc"
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Gorilla
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Xg <- as.data.frame(c(0,0,0, 0,0,0, 1,1,1, 0,0,0, 0,0,0), nrow=15, ncol=1, byrow=TRUE)
rownames(Xg) <- c("h1", "h2", "h3", "c1", "c2", "c3", "g1", "g2", "g3", "o1", "o2", "o3", "m1", "m2", "m3")
colnames(Xg) <- "Xg"
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Orangutan
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Xo <- as.data.frame(c(0,0,0, 0,0,0, 0,0,0, 1,1,1, 0,0,0), nrow=15, ncol=1, byrow=TRUE)
rownames(Xo) <- c("h1", "h2", "h3", "c1", "c2", "c3", "g1", "g2", "g3", "o1", "o2", "o3", "m1", "m2", "m3")
colnames(Xo) <- "Xo"
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Combine the individual X vectors into one matrix to use in the GLM
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
X <- as.matrix(cbind(Xh, Xc, Xg, Xo))
# ================================================================================================================================================================================================================
# Create the matrix that will hold the results
# ================================================================================================================================================================================================================
results_all_sites <- matrix(0, nrow=count_all_sites, ncol=54)
colnames(results_all_sites) = c(
"width", "dispersion",
"offset_h1", "offset_h2", "offset_h3", "offset_c1", "offset_c2", "offset_c3", "offset_g1", "offset_g2", "offset_g3", "offset_o1", "offset_o2", "offset_o3", "offset_m1", "offset_m2", "offset_m3",
"Bh", "Bh_se", "Bh_p", "Bc", "Bc_se", "Bc_p", "Bg", "Bg_se", "Bg_p", "Bo", "Bo_se", "Bo_p", "Bm", "Bm_se", "Bm_p",
"gateway_original_pvalue", "gateway_adjusted_pvalue",
"human_effect_size", "chimpanzee_effect_size", "gorilla_effect_size", "orangutan_effect_size", "macaque_effect_size",
"h1", "h2", "h3", "c1", "c2", "c3", "g1", "g2", "g3", "o1", "o2", "o3", "m1", "m2", "m3")
rownames(results_all_sites) <- colnames(scores)
# ================================================================================================================================================================================================================
# Create a matrix that will hold the flag for the gateway test
# Populate with "GNS" as the original value
# ================================================================================================================================================================================================================
flag_gateway_test <- matrix("GNS", nrow=count_all_sites, ncol=1)
colnames(flag_gateway_test) = "flag_gateway_test"
rownames(flag_gateway_test) <- colnames(scores)
# ================================================================================================================================================================================================================
# Create a matrix that will hold the flag for the type of change
# Populate with "ssss" as the original value
# ================================================================================================================================================================================================================
flag_change_type <- matrix("ssss", nrow=count_all_sites, ncol=1)
colnames(flag_change_type) = "flag_change_type"
rownames(flag_change_type) <- colnames(scores)
# ================================================================================================================================================================================================================
# Create a matrix that will hold the p-values for the constraint tests
# Populate with 1 for the original values
# ================================================================================================================================================================================================================
constraint_test_pvalues <- matrix(1, nrow=count_all_sites, ncol=15)
colnames(constraint_test_pvalues) <- c("ct_H_pvalue", "ct_C_pvalue", "ct_G_pvalue", "ct_O_pvalue", "ct_HCGO_pvalue",
"ct_HC_pvalue", "ct_HG_pvalue", "ct_HO_pvalue", "ct_CG_pvalue", "ct_CO_pvalue", "ct_GO_pvalue",
"ct_HCG_pvalue", "ct_HCO_pvalue", "ct_HGO_pvalue", "ct_CGO_pvalue")
rownames(constraint_test_pvalues) <- colnames(scores)
# ================================================================================================================================================================================================================
# Start the loop
# ================================================================================================================================================================================================================
cat("Performing gateway test ... \n")
cat("\tGateway test: analyzing site 1 of", count_all_sites, "\n")
for (lcv in 1:count_all_sites)
{
# ================================================================================================================================================================================================================
# Print periodic status messages
# ================================================================================================================================================================================================================
if ( (lcv %% 1000) == 0)
{
cat("\tGateway test: analyzing site", lcv, "of", count_all_sites, "\n")
}
# ================================================================================================================================================================================================================
# Run the glm function and calculate the p-value
# ================================================================================================================================================================================================================
score_data <- as.data.frame(cbind(scores[,lcv], X))
colnames(score_data) <- c("Y", "Xh", "Xc", "Xg", "Xo")
fit_glm <- glm(formula = Y ~ Xh + Xc + Xg + Xo + offset(log(library_size_offset_vector)), family = negative.binomial(theta = 1/dispersion_parameter_vector[lcv]), data=score_data)
gateway_test_statistic <- fit_glm$null.deviance - fit_glm$deviance
gateway_pvalue <- pchisq(gateway_test_statistic, df=4, lower=FALSE, log.p=FALSE)
# ================================================================================================================================================================================================================
# Update the results_all_sites matrix
# ================================================================================================================================================================================================================
# DHS site width
results_all_sites[lcv,1] = scores_with_locations[lcv,3] - scores_with_locations[lcv,2] + 1
# Dispersion parameter
results_all_sites[lcv,2] = dispersion_parameter_vector[lcv]
# Normalization factors for human
results_all_sites[lcv,3] = library_size_offset_vector[1]
results_all_sites[lcv,4] = library_size_offset_vector[2]
results_all_sites[lcv,5] = library_size_offset_vector[3]
# Normalization factors for chimpanzee
results_all_sites[lcv,6] = library_size_offset_vector[4]
results_all_sites[lcv,7] = library_size_offset_vector[5]
results_all_sites[lcv,8] = library_size_offset_vector[6]
# Normalization factors for gorilla
results_all_sites[lcv,9] = library_size_offset_vector[7]
results_all_sites[lcv,10] = library_size_offset_vector[8]
results_all_sites[lcv,11] = library_size_offset_vector[9]
# Normalization factors for orangutan
results_all_sites[lcv,12] = library_size_offset_vector[10]
results_all_sites[lcv,13] = library_size_offset_vector[11]
results_all_sites[lcv,14] = library_size_offset_vector[12]
# Normalization factors for macaque
results_all_sites[lcv,15] = library_size_offset_vector[13]
results_all_sites[lcv,16] = library_size_offset_vector[14]
results_all_sites[lcv,17] = library_size_offset_vector[15]
# Beta value, standard error, and p-value for human
results_all_sites[lcv,18] = summary(fit_glm)$coefficients[2,1]
results_all_sites[lcv,19] = summary(fit_glm)$coefficients[2,2]
results_all_sites[lcv,20] = summary(fit_glm)$coefficients[2,4]
# Beta value, standard error, and p-value for chimpanzee
results_all_sites[lcv,21] = summary(fit_glm)$coefficients[3,1]
results_all_sites[lcv,22] = summary(fit_glm)$coefficients[3,2]
results_all_sites[lcv,23] = summary(fit_glm)$coefficients[3,4]
# Beta value, standard error, and p-value for gorilla
results_all_sites[lcv,24] = summary(fit_glm)$coefficients[4,1]
results_all_sites[lcv,25] = summary(fit_glm)$coefficients[4,2]
results_all_sites[lcv,26] = summary(fit_glm)$coefficients[4,4]
# Beta value, standard error, and p-value for orangutan
results_all_sites[lcv,27] = summary(fit_glm)$coefficients[5,1]
results_all_sites[lcv,28] = summary(fit_glm)$coefficients[5,2]
results_all_sites[lcv,29] = summary(fit_glm)$coefficients[5,4]
# Beta value, standard error, and p-value for macaque
results_all_sites[lcv,30] = summary(fit_glm)$coefficients[1,1]
results_all_sites[lcv,31] = summary(fit_glm)$coefficients[1,2]
results_all_sites[lcv,32] = summary(fit_glm)$coefficients[1,4]
# Gateway p-value
results_all_sites[lcv,33] = gateway_pvalue
# Effect size for human (the beta value divided by the standard error)
results_all_sites[lcv,35] = summary(fit_glm)$coefficients[2,1] / summary(fit_glm)$coefficients[2,2]
# Effect size for chimpanzee (the beta value divided by the standard error)
results_all_sites[lcv,36] = summary(fit_glm)$coefficients[3,1] / summary(fit_glm)$coefficients[3,2]
# Effect size for gorilla (the beta value divided by the standard error)
results_all_sites[lcv,37] = summary(fit_glm)$coefficients[4,1] / summary(fit_glm)$coefficients[4,2]
# Effect size for orangutan (the beta value divided by the standard error)
results_all_sites[lcv,38] = summary(fit_glm)$coefficients[5,1] / summary(fit_glm)$coefficients[5,2]
# Effect size for macaque (the beta value divided by the standard error)
results_all_sites[lcv,39] = summary(fit_glm)$coefficients[1,1] / summary(fit_glm)$coefficients[1,2]
# Human scores
results_all_sites[lcv,40] = scores[1,lcv]
results_all_sites[lcv,41] = scores[2,lcv]
results_all_sites[lcv,42] = scores[3,lcv]
# Chimpanzee scores
results_all_sites[lcv,43] = scores[4,lcv]
results_all_sites[lcv,44] = scores[5,lcv]
results_all_sites[lcv,45] = scores[6,lcv]
# Gorilla scores
results_all_sites[lcv,46] = scores[7,lcv]
results_all_sites[lcv,47] = scores[8,lcv]
results_all_sites[lcv,48] = scores[9,lcv]
# Orangutan scores
results_all_sites[lcv,49] = scores[10,lcv]
results_all_sites[lcv,50] = scores[11,lcv]
results_all_sites[lcv,51] = scores[12,lcv]
# Macaque scores
results_all_sites[lcv,52] = scores[13,lcv]
results_all_sites[lcv,53] = scores[14,lcv]
results_all_sites[lcv,54] = scores[15,lcv]
} # END: for (lcv in 1:count_all_sites)
# ================================================================================================================================================================================================================
# Calculate the adjusted p-value using the Benjamini-Hochberg correction
# ================================================================================================================================================================================================================
results_all_sites[,34] <- p.adjust(results_all_sites[,33], "BH")
# ================================================================================================================================================================================================================
# Check for significance and update the flag for the gateway test
# ================================================================================================================================================================================================================
for (lcv in 1:count_all_sites)
{
if (results_all_sites[lcv,34] < pvalue_threshold)
{
flag_gateway_test[lcv,1] = "GD"
}
}
# ================================================================================================================================================================================================================
# Finish up
# ================================================================================================================================================================================================================
cat("done.\n\n")
# ################################################################################################################################################################################################################
# END SECTION 4 OF 10: GENERATE THE GLM AND PERFORM THE GATEWAY TEST
# ################################################################################################################################################################################################################
# ################################################################################################################################################################################################################
# SECTION 5 OF 10: PERFORM CONSTRAINT TESTS AND DETERMINE TYPE OF CHANGE
# ################################################################################################################################################################################################################
# ================================================================================================================================================================================================================
# Create the constraint matrices
# ================================================================================================================================================================================================================
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Changes in one species
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Human
H_matrix <- matrix(c(1, -1/4, -1/4, -1/4), nrow=1, ncol=4, byrow = TRUE)
colnames(H_matrix) = c("h","c","g","o")
# Chimpanzee
C_matrix <- matrix(c(-1/4, 1, -1/4, -1/4), nrow=1, ncol=4, byrow = TRUE)
colnames(C_matrix) = c("h","c","g","o")
# Gorilla
G_matrix <- matrix(c(-1/4, -1/4, 1, -1/4), nrow=1, ncol=4, byrow = TRUE)
colnames(G_matrix) = c("h","c","g","o")
# Orangutan
O_matrix <- matrix(c(-1/4, -1/4, -1/4, 1), nrow=1, ncol=4, byrow = TRUE)
colnames(O_matrix) = c("h","c","g","o")
# Macaque (also identifies changes in human/chimpanzee/gorilla/orangutan)
HCGO_matrix <- matrix(c(-1/4, -1/4, -1/4, -1/4), nrow=1, ncol=4, byrow = TRUE)
colnames(HCGO_matrix) = c("h","c","g","o")
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Changes in two species
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Human/chimpanzee
HC_matrix <- matrix(c(1/2, 1/2, -1/3, -1/3), nrow=1, ncol=4, byrow = TRUE)
colnames(HC_matrix) = c("h","c","g","o")
# Human/gorilla
HG_matrix <- matrix(c(1/2, -1/3, 1/2, -1/3), nrow=1, ncol=4, byrow = TRUE)
colnames(HG_matrix) = c("h","c","g","o")
# Human/orangutan
HO_matrix <- matrix(c(1/2, -1/3, -1/3, 1/2), nrow=1, ncol=4, byrow = TRUE)
colnames(HO_matrix) = c("h","c","g","o")
# Chimpanzee/gorilla
CG_matrix <- matrix(c(-1/3, 1/2, 1/2, -1/3), nrow=1, ncol=4, byrow = TRUE)
colnames(CG_matrix) = c("h","c","g","o")
# Chimpanzee/orangutan
CO_matrix <- matrix(c(-1/3, 1/2, -1/3, 1/2), nrow=1, ncol=4, byrow = TRUE)
colnames(CO_matrix) = c("h","c","g","o")
# Gorilla/orangutan
GO_matrix <- matrix(c(-1/3, -1/3, 1/2, 1/2), nrow=1, ncol=4, byrow = TRUE)
colnames(GO_matrix) = c("h","c","g","o")
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Changes in three species
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Human/chimpanzee/gorilla
HCG_matrix <- matrix(c(1/3, 1/3, 1/3, -1/2), nrow=1, ncol=4, byrow = TRUE)
colnames(HCG_matrix) = c("h","c","g","o")
# Human/chimpanzee/orangutan
HCO_matrix <- matrix(c(1/3, 1/3, -1/2, 1/3), nrow=1, ncol=4, byrow = TRUE)
colnames(HCO_matrix) = c("h","c","g","o")
# Human/gorilla/orangutan
HGO_matrix <- matrix(c(1/3, -1/2, 1/3, 1/3), nrow=1, ncol=4, byrow = TRUE)
colnames(HGO_matrix) = c("h","c","g","o")
# Chimpanzee/gorilla/orangutan
CGO_matrix <- matrix(c(-1/2, 1/3, 1/3, 1/3), nrow=1, ncol=4, byrow = TRUE)
colnames(CGO_matrix) = c("h","c","g","o")
# ================================================================================================================================================================================================================
# Start the loop
# ================================================================================================================================================================================================================
cat("Performing constraint tests to determine type of change ... \n")
cat("\tConstraint tests: analyzing site 1 of", count_all_sites, "\n")
for (lcv in 1:count_all_sites)
{
# ================================================================================================================================================================================================================
# Print periodic status messages
# ================================================================================================================================================================================================================
if ( (lcv %% 1000) == 0)
{
cat("\tConstraint tests: analyzing site", lcv, "of", count_all_sites, "\n")
}
# ================================================================================================================================================================================================================
# If the site is not significantly differential, skip it
# ================================================================================================================================================================================================================
if (flag_gateway_test[lcv,1] == "GNS")
{
next
}
# ================================================================================================================================================================================================================
# Re-run the glm to extract the beta values and the variance-covariance matrix
# ================================================================================================================================================================================================================
score_data <- as.data.frame(cbind(scores[,lcv], X))
colnames(score_data) <- c("Y", "Xh", "Xc", "Xg", "Xo")
fit_glm <- glm(formula = Y ~ Xh + Xc + Xg + Xo + offset(log(library_size_offset_vector)), family = negative.binomial(theta = 1/dispersion_parameter_vector[lcv]), data=score_data)
vcov_matrix <- vcov(fit_glm)[2:5,2:5]
beta_matrix <- summary(fit_glm)$coefficients[2:5,1]
# ================================================================================================================================================================================================================
# Calculate the constraint values
# ================================================================================================================================================================================================================
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Changes in one species
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Human change
H_pvalue <- pchisq( ( t(H_matrix %*% beta_matrix) %*% solve(H_matrix %*% vcov_matrix %*% t(H_matrix)) %*% (H_matrix %*% beta_matrix) ), df=1, lower=FALSE, log.p=FALSE)
# Chimpanzee change
C_pvalue <- pchisq( ( t(C_matrix %*% beta_matrix) %*% solve(C_matrix %*% vcov_matrix %*% t(C_matrix)) %*% (C_matrix %*% beta_matrix) ), df=1, lower=FALSE, log.p=FALSE)
# Gorilla change
G_pvalue <- pchisq( ( t(G_matrix %*% beta_matrix) %*% solve(G_matrix %*% vcov_matrix %*% t(G_matrix)) %*% (G_matrix %*% beta_matrix) ), df=1, lower=FALSE, log.p=FALSE)
# Orangutan change
O_pvalue <- pchisq( ( t(O_matrix %*% beta_matrix) %*% solve(O_matrix %*% vcov_matrix %*% t(O_matrix)) %*% (O_matrix %*% beta_matrix) ), df=1, lower=FALSE, log.p=FALSE)
# Macaque change (also identifies changes in human/chimpanzee/gorilla/orangutan)
HCGO_pvalue <- pchisq( ( t(HCGO_matrix %*% beta_matrix) %*% solve(HCGO_matrix %*% vcov_matrix %*% t(HCGO_matrix)) %*% (HCGO_matrix %*% beta_matrix) ), df=1, lower=FALSE, log.p=FALSE)
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Changes in two species
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Human/chimpanzee change
HC_pvalue <- pchisq( ( t(HC_matrix %*% beta_matrix) %*% solve(HC_matrix %*% vcov_matrix %*% t(HC_matrix)) %*% (HC_matrix %*% beta_matrix) ), df=1, lower=FALSE, log.p=FALSE)
# Human/gorilla change
HG_pvalue <- pchisq( ( t(HG_matrix %*% beta_matrix) %*% solve(HG_matrix %*% vcov_matrix %*% t(HG_matrix)) %*% (HG_matrix %*% beta_matrix) ), df=1, lower=FALSE, log.p=FALSE)
# Human/orangutan change
HO_pvalue <- pchisq( ( t(HO_matrix %*% beta_matrix) %*% solve(HO_matrix %*% vcov_matrix %*% t(HO_matrix)) %*% (HO_matrix %*% beta_matrix) ), df=1, lower=FALSE, log.p=FALSE)
# Chimpanzee/gorilla change
CG_pvalue <- pchisq( ( t(CG_matrix %*% beta_matrix) %*% solve(CG_matrix %*% vcov_matrix %*% t(CG_matrix)) %*% (CG_matrix %*% beta_matrix) ), df=1, lower=FALSE, log.p=FALSE)
# Chimpanzee/orangutan change
CO_pvalue <- pchisq( ( t(CO_matrix %*% beta_matrix) %*% solve(CO_matrix %*% vcov_matrix %*% t(CO_matrix)) %*% (CO_matrix %*% beta_matrix) ), df=1, lower=FALSE, log.p=FALSE)
# Gorilla/orangutan change
GO_pvalue <- pchisq( ( t(GO_matrix %*% beta_matrix) %*% solve(GO_matrix %*% vcov_matrix %*% t(GO_matrix)) %*% (GO_matrix %*% beta_matrix) ), df=1, lower=FALSE, log.p=FALSE)
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Changes in three species
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Human/chimpanzee/gorilla change
HCG_pvalue <- pchisq( ( t(HCG_matrix %*% beta_matrix) %*% solve(HCG_matrix %*% vcov_matrix %*% t(HCG_matrix)) %*% (HCG_matrix %*% beta_matrix) ), df=1, lower=FALSE, log.p=FALSE)
# Human/chimpanzee/orangutan change
HCO_pvalue <- pchisq( ( t(HCO_matrix %*% beta_matrix) %*% solve(HCO_matrix %*% vcov_matrix %*% t(HCO_matrix)) %*% (HCO_matrix %*% beta_matrix) ), df=1, lower=FALSE, log.p=FALSE)
# Human/gorilla/orangutan change
HGO_pvalue <- pchisq( ( t(HGO_matrix %*% beta_matrix) %*% solve(HGO_matrix %*% vcov_matrix %*% t(HGO_matrix)) %*% (HGO_matrix %*% beta_matrix) ), df=1, lower=FALSE, log.p=FALSE)
# Chimpanzee/gorilla/orangutan change
CGO_pvalue <- pchisq( ( t(CGO_matrix %*% beta_matrix) %*% solve(CGO_matrix %*% vcov_matrix %*% t(CGO_matrix)) %*% (CGO_matrix %*% beta_matrix) ), df=1, lower=FALSE, log.p=FALSE)
# ================================================================================================================================================================================================================
# Update the constraint test p-values matrix
# ================================================================================================================================================================================================================
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Changes in one species
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Human change
constraint_test_pvalues[lcv,1] <- H_pvalue
# Chimpanzee change
constraint_test_pvalues[lcv,2] <- C_pvalue
# Gorilla change
constraint_test_pvalues[lcv,3] <- G_pvalue
# Orangutan change
constraint_test_pvalues[lcv,4] <- O_pvalue
# Macaque change (also identifies human/chimpanzee/gorilla/orangutan change)
constraint_test_pvalues[lcv,5] <- HCGO_pvalue
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Changes in two species
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Human/chimpanzee change
constraint_test_pvalues[lcv,6] <- HC_pvalue
# Human/gorilla change
constraint_test_pvalues[lcv,7] <- HG_pvalue
# Human/orangutan change
constraint_test_pvalues[lcv,8] <- HO_pvalue
# Chimpanzee/gorilla change
constraint_test_pvalues[lcv,9] <- CG_pvalue
# Chimpanzee/orangutan change
constraint_test_pvalues[lcv,10] <- CO_pvalue
# Gorilla/orangutan change
constraint_test_pvalues[lcv,11] <- GO_pvalue
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Changes in three species
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Human/chimpanzee/gorilla change
constraint_test_pvalues[lcv,12] <- HCG_pvalue
# Human/chimpanzee/orangutan change
constraint_test_pvalues[lcv,13] <- HCO_pvalue
# Human/gorilla/orangutan change
constraint_test_pvalues[lcv,14] <- HGO_pvalue
# Chimpanzee/gorilla/orangutan change
constraint_test_pvalues[lcv,15] <- CGO_pvalue
# ================================================================================================================================================================================================================
# Identify the smallest p-value and update the flag for the type of change (flag_change_type)
# ================================================================================================================================================================================================================
minimum_pvalue <- min(H_pvalue, C_pvalue, G_pvalue, O_pvalue, HCGO_pvalue, HC_pvalue, HG_pvalue, HO_pvalue, CG_pvalue, CO_pvalue, GO_pvalue, HCG_pvalue, HCO_pvalue, HGO_pvalue, CGO_pvalue)
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Differential subset 1 of 16: change in one species - human
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if ( (H_pvalue < pvalue_threshold) & (H_pvalue == minimum_pvalue) )
{
if (results_all_sites[lcv,18] > 0)
{
flag_change_type[lcv,1] <- "Gsss"
} else {
flag_change_type[lcv,1] <- "Lsss"
}
}
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Differential subset 2 of 16: change in one species - chimpanzee
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if ( (C_pvalue < pvalue_threshold) & (C_pvalue == minimum_pvalue) )
{
if (results_all_sites[lcv,21] > 0)
{
flag_change_type[lcv,1] <- "sGss"
} else {
flag_change_type[lcv,1] <- "sLss"
}
}
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Differential subset 3 of 16: change in one species - gorilla
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if ( (G_pvalue < pvalue_threshold) & (G_pvalue == minimum_pvalue) )
{
if (results_all_sites[lcv,24] > 0)
{
flag_change_type[lcv,1] <- "ssGs"
} else {
flag_change_type[lcv,1] <- "ssLs"
}
}
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Differential subset 4 of 16: change in one species - orangutan
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if ( (O_pvalue < pvalue_threshold) & (O_pvalue == minimum_pvalue) )
{
if (results_all_sites[lcv,27] > 0)
{
flag_change_type[lcv,1] <- "sssG"
} else {
flag_change_type[lcv,1] <- "sssL"
}
}
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Differential subset 5 of 16: change in one species - macaque (also identifies changes in human/chimpanzee/gorilla/orangutan)
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if ( (HCGO_pvalue < pvalue_threshold) & (HCGO_pvalue == minimum_pvalue) )
{
if ( (results_all_sites[lcv,18] < 0) & (results_all_sites[lcv,21] < 0) & (results_all_sites[lcv,24] < 0) & (results_all_sites[lcv,27] < 0) )
{
flag_change_type[lcv,1] <- "LLLL"
} else {
flag_change_type[lcv,1] <- "GGGG"
}
}
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Differential subset 6 of 16: change in two species - human/chimpanzee
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if ( (HC_pvalue < pvalue_threshold) & (HC_pvalue == minimum_pvalue) )
{
if ( (results_all_sites[lcv,18] > 0) & (results_all_sites[lcv,21] > 0) )
{
flag_change_type[lcv,1] <- "GGss"
}
if ( (results_all_sites[lcv,18] > 0) & (results_all_sites[lcv,21] < 0) )
{
flag_change_type[lcv,1] <- "GLss"
}
if ( (results_all_sites[lcv,18] < 0) & (results_all_sites[lcv,21] > 0) )
{
flag_change_type[lcv,1] <- "LGss"
}
if ( (results_all_sites[lcv,18] < 0) & (results_all_sites[lcv,21] < 0) )
{
flag_change_type[lcv,1] <- "LLss"
}
} # END: if ( (HC_pvalue < pvalue_threshold) & (HC_pvalue == minimum_pvalue) )
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Differential subset 7 of 16: change in two species - human/gorilla
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if ( (HG_pvalue < pvalue_threshold) & (HG_pvalue == minimum_pvalue) )
{
if ( (results_all_sites[lcv,18] > 0) & (results_all_sites[lcv,24] > 0) )
{
flag_change_type[lcv,1] <- "GsGs"
}
if ( (results_all_sites[lcv,18] > 0) & (results_all_sites[lcv,24] < 0) )
{
flag_change_type[lcv,1] <- "GsLs"
}
if ( (results_all_sites[lcv,18] < 0) & (results_all_sites[lcv,24] > 0) )
{
flag_change_type[lcv,1] <- "LsGs"
}
if ( (results_all_sites[lcv,18] < 0) & (results_all_sites[lcv,24] < 0) )
{
flag_change_type[lcv,1] <- "LsLs"
}
} # END: if ( (HG_pvalue < pvalue_threshold) & (HG_pvalue == minimum_pvalue) )
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Differential subset 8 of 16: change in two species - human/orangutan
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if ( (HO_pvalue < pvalue_threshold) & (HO_pvalue == minimum_pvalue) )
{
if ( (results_all_sites[lcv,18] > 0) & (results_all_sites[lcv,27] > 0) )
{
flag_change_type[lcv,1] <- "GssG"
}
if ( (results_all_sites[lcv,18] > 0) & (results_all_sites[lcv,27] < 0) )
{
flag_change_type[lcv,1] <- "GssL"
}
if ( (results_all_sites[lcv,18] < 0) & (results_all_sites[lcv,27] > 0) )
{
flag_change_type[lcv,1] <- "LssG"
}
if ( (results_all_sites[lcv,18] < 0) & (results_all_sites[lcv,27] < 0) )
{
flag_change_type[lcv,1] <- "LssL"
}
} # END: if ( (HO_pvalue < pvalue_threshold) & (HO_pvalue == minimum_pvalue) )
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Differential subset 9 of 16: change in two species - chimpanzee/gorilla
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if ( (CG_pvalue < pvalue_threshold) & (CG_pvalue == minimum_pvalue) )
{
if ( (results_all_sites[lcv,21] > 0) & (results_all_sites[lcv,24] > 0) )
{
flag_change_type[lcv,1] <- "sGGs"
}
if ( (results_all_sites[lcv,21] > 0) & (results_all_sites[lcv,24] < 0) )
{
flag_change_type[lcv,1] <- "sGLs"
}
if ( (results_all_sites[lcv,21] < 0) & (results_all_sites[lcv,24] > 0) )
{
flag_change_type[lcv,1] <- "sLGs"
}
if ( (results_all_sites[lcv,21] < 0) & (results_all_sites[lcv,24] < 0) )
{
flag_change_type[lcv,1] <- "sLLs"
}
} # END: if ( (CG_pvalue < pvalue_threshold) & (CG_pvalue == minimum_pvalue) )
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Differential subset 10 of 16: change in two species - chimpanzee/orangutan
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if ( (CO_pvalue < pvalue_threshold) & (CO_pvalue == minimum_pvalue) )
{
if ( (results_all_sites[lcv,21] > 0) & (results_all_sites[lcv,27] > 0) )
{
flag_change_type[lcv,1] <- "sGsG"
}
if ( (results_all_sites[lcv,21] > 0) & (results_all_sites[lcv,27] < 0) )
{
flag_change_type[lcv,1] <- "sGsL"
}
if ( (results_all_sites[lcv,21] < 0) & (results_all_sites[lcv,27] > 0) )
{
flag_change_type[lcv,1] <- "sLsG"
}
if ( (results_all_sites[lcv,21] < 0) & (results_all_sites[lcv,27] < 0) )
{
flag_change_type[lcv,1] <- "sLsL"
}
} # END: if ( (CO_pvalue < pvalue_threshold) & (CO_pvalue == minimum_pvalue) )
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Differential subset 11 of 16: change in two species - gorilla/orangutan
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if ( (GO_pvalue < pvalue_threshold) & (GO_pvalue == minimum_pvalue) )
{
if ( (results_all_sites[lcv,24] > 0) & (results_all_sites[lcv,27] > 0) )
{
flag_change_type[lcv,1] <- "ssGG"
}
if ( (results_all_sites[lcv,24] > 0) & (results_all_sites[lcv,27] < 0) )
{
flag_change_type[lcv,1] <- "ssGL"
}
if ( (results_all_sites[lcv,24] < 0) & (results_all_sites[lcv,27] > 0) )
{
flag_change_type[lcv,1] <- "ssLG"
}
if ( (results_all_sites[lcv,24] < 0) & (results_all_sites[lcv,27] < 0) )
{
flag_change_type[lcv,1] <- "ssLL"
}
} # END: if ( (GO_pvalue < pvalue_threshold) & (GO_pvalue == minimum_pvalue) )
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Differential subset 12 of 16: change in three species - human/chimpanzee/gorilla
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if ( (HCG_pvalue < pvalue_threshold) & (HCG_pvalue == minimum_pvalue) )
{
if ( (results_all_sites[lcv,18] > 0) & (results_all_sites[lcv,21] > 0) & (results_all_sites[lcv,24] > 0) )
{
flag_change_type[lcv,1] <- "GGGs"
}
if ( (results_all_sites[lcv,18] > 0) & (results_all_sites[lcv,21] > 0) & (results_all_sites[lcv,24] < 0) )
{
flag_change_type[lcv,1] <- "GGLs"
}
if ( (results_all_sites[lcv,18] > 0) & (results_all_sites[lcv,21] < 0) & (results_all_sites[lcv,24] > 0) )
{
flag_change_type[lcv,1] <- "GLGs"
}
if ( (results_all_sites[lcv,18] > 0) & (results_all_sites[lcv,21] < 0) & (results_all_sites[lcv,24] < 0) )
{
flag_change_type[lcv,1] <- "GLLs"
}
if ( (results_all_sites[lcv,18] < 0) & (results_all_sites[lcv,21] > 0) & (results_all_sites[lcv,24] > 0) )
{
flag_change_type[lcv,1] <- "LGGs"
}
if ( (results_all_sites[lcv,18] < 0) & (results_all_sites[lcv,21] > 0) & (results_all_sites[lcv,24] < 0) )
{
flag_change_type[lcv,1] <- "LGLs"
}
if ( (results_all_sites[lcv,18] < 0) & (results_all_sites[lcv,21] < 0) & (results_all_sites[lcv,24] > 0) )
{
flag_change_type[lcv,1] <- "LLGs"
}
if ( (results_all_sites[lcv,18] < 0) & (results_all_sites[lcv,21] < 0) & (results_all_sites[lcv,24] < 0) )
{
flag_change_type[lcv,1] <- "LLLs"
}
} # END: if ( (HCG_pvalue < pvalue_threshold) & (HCG_pvalue == minimum_pvalue) )
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------