-
Notifications
You must be signed in to change notification settings - Fork 2
/
eQTLHap.R
1526 lines (1255 loc) · 42.6 KB
/
eQTLHap.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
rm(list = ls())
#!/usr/bin/envRscript
library("optparse")
cat("\t\t\t****************************************\n")
cat("\t\t\t* eQTLHap *\n")
cat("\t\t\t* V0.1 *\n")
cat("\t\t\t* author: Ziad Al Bkhetan *\n")
cat("\t\t\t* [email protected] *\n")
cat("\t\t\t* https://github.com/ziadbkh/eQTLHap *\n")
cat("\t\t\t****************************************\n")
option_list <- list(
make_option(
c("-f", "--haps"),
type = "character",
default = NULL,
help = "Haplotype/Genotype file path (.haps or .vcf).
\t\tIt can be compressed (.gz).",
metavar = "character"
),
make_option(
c("-g", "--genes"),
type = "character",
default = NULL,
help = "Gene Expression file path.\n\t\tIt should be in bed format.
\t\tFirst four columns should be (Chr, gene_name, start, end).
\t\tIt can be compressed (.gz). See the sample files for format.",
metavar = "character"
),
make_option(
c("-c", "--cov"),
type = "character",
default = "",
help = "Covariates file path.
\t\tSee the sample files for format.",
metavar = "character"
),
make_option(
c("-b", "--blocks"),
type = "character",
default = NULL,
help = "Haplotype block file.",
metavar = "character"
),
make_option(
c("-o", "--out"),
type = "character",
default = NULL,
help = "output file path.",
metavar = "character"
),
make_option(
c("", "--chrm"),
type = "numeric",
default = -1,
help = "chromosome number (1 to 22, X, Y).
\t\tIf not provided the first chromosome in gene expression file will be used.",
metavar = "numeric"
),
make_option(
c("", "--mtc"),
type = "character",
default = "BH",
help = "Multiple test correction method [default=%default].
\t\tAccepts any methods from p.adjust.methods",
metavar = "character"
),
make_option(
c("-p", "--permutation"),
type = "numeric",
default = 1000,
help = "Permutation count [default=%default].",
metavar = "numeric"
),
make_option(
c("-w", "--window"),
type = "numeric",
default = 1000000,
help = "Scanning window [default=%default] in bp to be applied up/downstream of the gene start.",
metavar = "numeric"
),
make_option(
c("-a", "--assessment"),
type = "character",
default = "HSG",
help = "eQTL assessment [default=%default].
\t\tAny combination of S, H and G where S: SNP assessment, G: block's genotype assessment, and H: block's haplotype assessment.",
metavar = "character"
),
make_option(
c("", "--vcf"),
action = "store_true",
default = FALSE,
help = "Phasing/unphased haplotypes input file is in VCF format.
\t\t[default=%default].
\t\tIf False, the input file is consdered .haps/.sample format."
),
make_option(
c("", "--smf"),
type = "numeric",
default = 0.01,
help = "SNP minimum frequency to be included in the analysis.
\t\t[default=%default].",
metavar = "numeric"
),
make_option(
c("", "--hmf"),
type = "numeric",
default = 0.02,
help = "Haplotype minimum frequency to be included in the analysis.
\t\t[default=%default].",
metavar = "numeric"
),
make_option(
c("", "--gmf"),
type = "numeric",
default = 0.02,
help = "Genotype minimum frequency to be included in the analysis.
\t\t[default=%default].",
metavar = "numeric"
),
make_option(
c("", "--maxPval4Perm"),
type = "numeric",
default = 0,
help = "Maximum pvalue in order to apply permutation multiple test correction.
\t\t[default=%default].
\t\tIf not 0, any association with a pvalue less than this threshold will be passed to permutation analysis.",
metavar = "numeric"
),
make_option(
c("", "--rmvIndividuals"),
action = "store_true",
default = FALSE,
help = "[default=%default].
\t\tIf provided, individuals with hablotype whose frequency are less than --hmf and --gmf
\t\twill be eliminated from the assessment.",
metavar = "numeric"
),
make_option(
c("", "--minIndividuals"),
type = "numeric",
default = 50,
help = "If the individual count is less than this threshold, skip the assessment.
\t\t[default=%default]."
),
make_option(
c("", "--outSignifcancePerm"),
type = "numeric",
default = 1,
help = "[default=%default].
\t\tKeep only the associations with permutation pvalue less than this threshold in the output results.",
metavar = "numeric"
),
make_option(
c("", "--outSignifcancePval"),
type = "numeric",
default = 0.05,
help = "[default=%default].
\t\tKeep only the associations with pvalue less than this threshold in the output results.",
metavar = "numeric"
),
make_option(
c("", "--outSignifcanceQval"),
type = "numeric",
default = 1,
help = "[default=%default].
\t\tKeep only the associations with qvalue less than this threshold in the output results.",
metavar = "numeric"
),
#make_option(
# c("", "--customBlockFile"),
# action = "store_true",
# default = FALSE,
# help = "[default=%default].
# \t\tProvide a custom block file.",
# metavar = "numeric"
#),
make_option(
c("", "--customBlocks"),
action = "store_true",
default = FALSE,
help = "[default=%default].
\t\tConsider a subset of the SNPs within each block as defined in teh column SNPS inside the block file instead of all SNPs betwenn the start/end SNPs.",
metavar = "numeric"
),
make_option(
c("", "--unphased"),
action = "store_true",
default = FALSE,
help = "[default=%default].
\t\tReading unphased genotype file",
metavar = "numeric"
)
)
opt_parser <- OptionParser(option_list = option_list)
opt <- parse_args(opt_parser)
start_time <- Sys.time()
print(paste("Analysis started at:", start_time), quote = F)
if (is.null(opt$haps) |
is.null(opt$genes) | is.null(opt$out)) {
print(
"Phased/unphased genotypes and gene expression and output files are mandatory!",
quote = FALSE
)
stop("Exiting..")
}
snp_assessment <- FALSE
block_genotype_assessment <- FALSE
block_haplotype_assessment <- FALSE
if (grepl("S", toupper(opt$assessment), fixed = TRUE))
snp_assessment <- TRUE
if (grepl("G", toupper(opt$assessment), fixed = TRUE))
block_genotype_assessment <- TRUE
if (grepl("H", toupper(opt$assessment), fixed = TRUE))
block_haplotype_assessment <- TRUE
if ((block_haplotype_assessment | block_genotype_assessment) & is.null(opt$blocks))
{
print(
"Block file is mandatory for eQTL based on block's haplotype/genotype!",
quote = FALSE
)
stop("Exiting..")
}
##### Validation ####
gene_expression_file_path <- opt$genes
haplotype_file_path <- opt$haps
haplotype_blocks_file_path <- opt$block
covariate_file_path <- opt$cov
out_put_file_path <- opt$out
if (file.access(gene_expression_file_path, mode = 4) != 0) {
stop("Gene expression file can not be accessed !", call. = FALSE)
}
if (file.access(haplotype_file_path, mode = 4) != 0) {
stop("Haplotype/Genotype file can not be accessed !", call. = FALSE)
}
if (block_haplotype_assessment | block_genotype_assessment)
{
if (file.access(haplotype_blocks_file_path, mode = 4) != 0) {
stop("Haplotype block file can not be accessed!", call. = FALSE)
}
}
if (covariate_file_path != "")
if (file.access(covariate_file_path, mode = 4) != 0) {
stop("covariate file does not exist!", call. = FALSE)
}
##### Libraries ####
suppressMessages(library(dplyr))
#####Parameter configuration#####
chrom <- opt$chrm
chrom <- gsub("chr", "", chrom)
correction_method <- opt$mtc
scanning_window <- opt$window
vcf_input <- opt$vcf
snp_minimal_frequency <- opt$smf
haplotype_minimal_frequency <- opt$hmf
genotype_minimal_frequency <- opt$gmf
remove_individual_rare <- opt$rmvIndividuals
significance_threshold_perm <- opt$outSignifcancePerm
significance_threshold_pval <- opt$outSignifcancePval
significance_threshold_qval <- opt$outSignifcanceQval
minimum_individuals <- opt$minIndividuals
permutation_calculation_threshold <- opt$maxPval4Perm
permutations_num <- opt$permutation
if (permutation_calculation_threshold == 0 | permutations_num == 0)
{
permutation_calculation_threshold <- 0
permutations_num <- 0
}
complete_block <- !opt$customBlocks
unphased_data <- opt$unphased
genes_output <- opt$genesOut
#haplotype file configuration:
gene_expression_info_columns <- c(3, 1, 4, 2)
gene_expression_info_columns <- c(1, 2, 3, 4)
gene_expression_info_columns_name <-
c("Chr", "gene_nm", "start", "end")
vcf_start_snps <- 10
snps_per_iteration <- 1000
########## Functions ######
eQTLHap_block_encoding <- function(fun_block_haplotype_1,
fun_block_haplotype_2,
fun_haplotype_minimal_frequency,
fun_genotype_minimal_frequency,
fun_remove_individual_opt,
fun_minimum_individuals,
fun_block_haplotype_assessment,
fun_block_genotype_assessment) {
fun_bag_of_haplotypes <- list()
fun_bag_of_genotypes <- list()
fun_genotype_individuals <- 1:length(fun_block_haplotype_1)
fun_haplotype_individuals <- 1:length(fun_block_haplotype_1)
if (fun_block_genotype_assessment)
{
fun_block_genotype <-
apply(fun_block_haplotype_1 + fun_block_haplotype_2, 2, function(x)
paste0(x, collapse = ""))
fun_genotype_frequency <-
table(fun_block_genotype) / length(fun_genotype_individuals)
fun_unique_frequent_genotypes <-
names(fun_genotype_frequency[fun_genotype_frequency >= fun_genotype_minimal_frequency])
if (length(fun_unique_frequent_genotypes) >= 2)
{
if (fun_remove_individual_opt)
{
fun_rare_genotypes <-
setdiff(names(fun_genotype_frequency),
fun_unique_frequent_genotypes)
fun_removed_individuals <-
which(fun_block_genotype %in% fun_rare_genotypes)
fun_kept_individuals <-
setdiff(1:length(fun_block_genotype),
fun_removed_individuals)
fun_block_genotype <-
fun_block_genotype[fun_kept_individuals]
fun_genotype_individuals <- fun_kept_individuals
}
if (length(fun_genotype_individuals) > fun_minimum_individuals)
{
for (i in 2:length(fun_unique_frequent_genotypes))
{
fun_bag_of_genotypes[[i - 1]] <-
matrix(ifelse(
fun_block_genotype == fun_unique_frequent_genotypes[[i]],
1,
0
),
nrow = 1)
}
}
}
}
if (fun_block_haplotype_assessment)
{
fun_block_haplotype_1 <-
apply(fun_block_haplotype_1, 2, function(x)
paste0(x, collapse = ""))
fun_block_haplotype_2 <-
apply(fun_block_haplotype_2, 2, function(x)
paste0(x, collapse = ""))
fun_haplotype_frequncy <-
table(c(fun_block_haplotype_1, fun_block_haplotype_2)) / (2 * length(fun_haplotype_individuals))
fun_unique_frequent_haplotypes <-
names(fun_haplotype_frequncy[fun_haplotype_frequncy >= fun_haplotype_minimal_frequency])
if (length(fun_unique_frequent_haplotypes) >= 2)
{
if (fun_remove_individual_opt)
{
fun_rare_haplotypes <-
setdiff(names(fun_haplotype_frequncy),
fun_unique_frequent_haplotypes)
fun_removed_individuals <-
unique(c(
which(fun_block_haplotype_1 %in% fun_rare_haplotypes),
which(fun_block_haplotype_2 %in% fun_rare_haplotypes)
))
fun_kept_individuals <-
setdiff(1:length(fun_block_haplotype_1),
fun_removed_individuals)
fun_block_haplotype_1 <-
fun_block_haplotype_1[fun_kept_individuals]
fun_block_haplotype_2 <-
fun_block_haplotype_2[fun_kept_individuals]
fun_haplotype_individuals <- fun_kept_individuals
}
if (length(fun_haplotype_individuals) > fun_minimum_individuals)
{
for (i in 2:length(fun_unique_frequent_haplotypes))
{
fun_bag_of_haplotypes[[i - 1]] <-
matrix(
ifelse(
fun_block_haplotype_1 == fun_unique_frequent_haplotypes[[i]],
1,
0
) +
ifelse(
fun_block_haplotype_2 == fun_unique_frequent_haplotypes[[i]],
1,
0
),
nrow = 1
)
}
}
}
}
return(
list(
"haplotype" = fun_bag_of_haplotypes,
"genotype" = fun_bag_of_genotypes,
"genotype_individuals" = fun_genotype_individuals,
"haplotype_individuals" = fun_haplotype_individuals
)
)
}
eQTLHAP_block_orthogonalization <- function(fun_bag_of_things,
fun_covariates) {
#Orthogonalize haplotypes
i <- 1
for (i in 1:length(fun_bag_of_things))
{
row_sum_prev <- rowSums(fun_bag_of_things[[i]] ^ 2)
fun_bag_of_things[[i]] <-
fun_bag_of_things[[i]] - tcrossprod(fun_bag_of_things[[i]], fun_covariates) %*%
fun_covariates
if (i > 1)
for (j in 1:(i - 1))
fun_bag_of_things[[i]] <-
fun_bag_of_things[[i]] - rowSums(fun_bag_of_things[[i]] * fun_bag_of_things[[j]]) *
fun_bag_of_things[[j]]
row_sum <- rowSums(fun_bag_of_things[[i]] ^ 2)
delete_indx <- (row_sum <= row_sum_prev * .Machine$double.eps)
fun_bag_of_things[[i]][delete_indx, ] <- 0
div <- sqrt(rowSums(fun_bag_of_things[[i]] ^ 2))
div[delete_indx] <- 1
fun_bag_of_things[[i]] <- fun_bag_of_things[[i]] / div
}
return(fun_bag_of_things)
}
eQTLHAP_block_MR2 <- function(fun_bag_of_things,
fun_gene_expression,
fun_covariates) {
R_squared <- c()
for (i in 1:length(fun_bag_of_things)) {
R_squared[[i]] <-
tcrossprod(fun_gene_expression, fun_bag_of_things[[i]])
}
multiple_R_squared <- R_squared[[1]] ^ 2
if (length(fun_bag_of_things) > 1)
{
for (j in 2:length(R_squared))
multiple_R_squared <- multiple_R_squared + R_squared[[j]] ^ 2
}
return(multiple_R_squared)
}
eQTLHAP_block_pvalue <- function(fun_multiple_R_squared,
fun_predictor_cnt,
fun_covariates_cnt,
fun_individual_cnt) {
corrected_degrees_of_freedom <-
fun_individual_cnt - fun_covariates_cnt - fun_predictor_cnt
f_test <-
fun_multiple_R_squared / (1 - fun_multiple_R_squared) * (corrected_degrees_of_freedom /
fun_predictor_cnt)
pval <-
pf(f_test,
fun_predictor_cnt,
corrected_degrees_of_freedom,
lower.tail = FALSE)
return(pval)
}
eQTL_Block <- function(fun_bag_of_things,
fun_covariates,
fun_gene_expression,
fun_individuals,
fun_permutation_calculation_threshold)
{
fun_bag_of_things <-
eQTLHAP_block_orthogonalization(fun_bag_of_things, fun_covariates[, fun_individuals])
# original gene expression association
b_mr2 <- eQTLHAP_block_MR2(fun_bag_of_things,
fun_gene_expression[nrow(fun_gene_expression) , fun_individuals],
fun_covariates[, fun_individuals])
emprical_pval <-
eQTLHAP_block_pvalue(b_mr2,
length(fun_bag_of_things),
nrow(fun_covariates),
length(fun_individuals))
perm_pval <- 2
if (emprical_pval < fun_permutation_calculation_threshold)
{
mr2 <- eQTLHAP_block_MR2(fun_bag_of_things,
fun_gene_expression[1:(nrow(fun_gene_expression) - 1) , fun_individuals],
fun_covariates[, fun_individuals])
pval_ls <-
eQTLHAP_block_pvalue(mr2,
length(fun_bag_of_things),
nrow(fun_covariates),
length(fun_individuals))
perm_pval <-
length(which(pval_ls <= emprical_pval)) / length(pval_ls)
}
return(
list(
"predictor" = length(fun_bag_of_things),
"individuals" = length(fun_individuals),
"MR2" = b_mr2,
"emprical_pval" = emprical_pval,
"perm_pval" = perm_pval
)
)
}
eQTLHAP_SNP <-
function(fun_block_haplotype_1,
fun_block_haplotype_2,
fun_gene_expression,
fun_covariates,
fun_permutation_calculation_threshold) {
my_snps <- as.matrix(fun_block_haplotype_1 + fun_block_haplotype_2)
row_sum_prev <- rowSums(my_snps ^ 2)
my_snps <-
my_snps - tcrossprod(my_snps, fun_covariates) %*% fun_covariates
row_sum <- rowSums(my_snps ^ 2)
delete_indx <- (row_sum <= row_sum_prev * .Machine$double.eps)
my_snps[delete_indx, ] <- 0
div <- sqrt(row_sum)
div[delete_indx] <- 1
my_snps = my_snps / div
emp_R_st <-
tcrossprod(fun_gene_expression[nrow(fun_gene_expression), ], my_snps)
model_predictors <- 1
corrected_degrees_of_freedom <-
length(fun_block_haplotype_1) - nrow(fun_covariates) - model_predictors
emp_t_test <-
emp_R_st * sqrt(corrected_degrees_of_freedom / (1 - emp_R_st ^ 2))
emp_pval <-
pt(-abs(emp_t_test), corrected_degrees_of_freedom) * 2
kept_snps <-
which(emp_pval < fun_permutation_calculation_threshold)
kept_snps_nm <- row.names(my_snps)[kept_snps]
if (length(kept_snps) > 0)
{
my_snps <- my_snps[kept_snps, ]
if (length(kept_snps) > 1)
R_st <- abs(tcrossprod(fun_gene_expression, my_snps))
else
{
R_st <-
tcrossprod(fun_gene_expression, matrix(my_snps, nrow = 1))
colnames(R_st) <- kept_snps_nm
}
t_test <-
R_st * sqrt(corrected_degrees_of_freedom / (1 - R_st ^ 2))
pval <- pt(-abs(t_test), corrected_degrees_of_freedom) * 2
pval <- apply(pval, 2, function(x) {
(length(which(x <= x[[length(x)]])) - 1) / (length(x) - 1)
})
} else
{
pval <- c()
}
return(list(
"R2" = emp_R_st,
"ttest" = emp_t_test,
"pval" = emp_pval,
"perm_pval" = pval
))
}
########### Start Processing #############
if (vcf_input)
{
haplotype_header_row <- 1
haplotype_data <- file(haplotype_file_path, "r")
while (TRUE) {
line <- readLines(haplotype_data, n = 1)
if (startsWith(line, "#CHROM")) {
break
}
haplotype_header_row <- haplotype_header_row + 1
}
close(haplotype_data)
haplotype_data <-
read.table(
haplotype_file_path,
skip = haplotype_header_row - 1,
comment.char = "",
header = T,
stringsAsFactors = F
)
colnames(haplotype_data)[[1]] <- "CHROM"
if (chrom == -1)
chrom <- as.character(haplotype_data[1, "CHROM"])
haplotype_data <-
filter(haplotype_data, CHROM == chrom |
CHROM == paste0("chr", chrom))
if (nrow(haplotype_data) == 0)
stop (
paste0(
"No SNPs are left after filtration based on chromosome! (chromsome = ",
chrom,
")."
)
)
haplotype_data_h1 <- haplotype_data
phase_sep <- "[|]"
if (unphased_data)
phase_sep <- "/"
for (i in vcf_start_snps:length(haplotype_data_h1))
haplotype_data_h1[, i] <-
as.numeric(as.character(sapply(haplotype_data_h1[, i], function(x)
strsplit(x, phase_sep)[[1]][[1]])))
haplotype_data_h2 <- haplotype_data
rm(list = "haplotype_data")
for (i in vcf_start_snps:length(haplotype_data_h2))
haplotype_data_h2[, i] <-
as.numeric(as.character(sapply(haplotype_data_h2[, i], function(x)
strsplit(x, phase_sep)[[1]][[2]])))
haplotype_data_info <- haplotype_data_h1[, 1:(vcf_start_snps - 1)]
haplotype_data_h1 <-
haplotype_data_h1[, vcf_start_snps:length(haplotype_data_h1)]
haplotype_data_h2 <-
haplotype_data_h2[, vcf_start_snps:length(haplotype_data_h2)]
rownames(haplotype_data_h1) <- haplotype_data_info$ID
rownames(haplotype_data_h2) <- haplotype_data_info$ID
numeric_cols <- unlist(lapply(haplotype_data_h1, is.numeric))
if (length(which(numeric_cols)) != length(haplotype_data_h1))
{
stop("SNP columns are not numeric!")
}
} else
#read haplotypes from .haps and .sample files
{
haplotype_data <-
read.table(haplotype_file_path,
stringsAsFactors = F)
colnames(haplotype_data)[[1]] <- "CHROM"
if (chrom == -1)
chrom <- as.character(haplotype_data[1, 1])
haplotype_data <-
filter(haplotype_data, CHROM == chrom |
CHROM == paste0("chr", chrom))
if (nrow(haplotype_data) == 0)
stop (
paste0(
"No SNPs are left after filtration based on chromosome! (chromsome = ",
chrom,
")."
)
)
haplotype_data_info <- haplotype_data[, 1:5]
colnames(haplotype_data_info)[[2]] <- "ID"
colnames(haplotype_data_info)[[3]] <- "POS"
haplotype_data_h1 <-
haplotype_data[, seq(6, length(haplotype_data), 2)]
haplotype_data_h2 <-
haplotype_data[, seq(7, length(haplotype_data), 2)]
rm(list = "haplotype_data")
rownames(haplotype_data_h1) <- haplotype_data_info$ID
rownames(haplotype_data_h2) <- haplotype_data_info$ID
haplotype_data_sample <-
read.table(gsub(".haps", ".sample", gsub(".gz", "", haplotype_file_path)),
stringsAsFactors = F)
colnames(haplotype_data_h1) <-
haplotype_data_sample$V2[3:nrow(haplotype_data_sample)]
colnames(haplotype_data_h2) <-
haplotype_data_sample$V2[3:nrow(haplotype_data_sample)]
numeric_cols <- unlist(lapply(haplotype_data_h2, is.numeric))
if (length(which(numeric_cols)) != length(haplotype_data_h1))
{
stop("SNP columns are not numeric!")
}
}
haplotype_data_info$indx <- 1:nrow(haplotype_data_info)
haplotype_data_info$ID <- as.character(haplotype_data_info$ID)
print(paste(
nrow(haplotype_data_h1),
"SNPs for",
length(haplotype_data_h1),
"individuals are loaded."
),
quote = F)
#read gene expression file
gene_expression_data <-
read.table(
gene_expression_file_path,
header = T,
comment.char = "",
stringsAsFactors = F
)
print(paste(
nrow(gene_expression_data),
"Genes for",
length(gene_expression_data) - 4,
"individuals are loaded."
),
quote = F)
gene_expression_data <-
gene_expression_data[, c(gene_expression_info_columns,
5:length(gene_expression_data))]
colnames(gene_expression_data)[1:4] <-
gene_expression_info_columns_name
gene_expression_data <-
filter(gene_expression_data, Chr == chrom |
Chr == paste0("chr", chrom))
if (nrow(gene_expression_data) == 0)
stop("No genes left after chromosome filteration!")
gene_expression_data_info <- gene_expression_data[, 1:4]
gene_expression_data <-
gene_expression_data[, 5:length(gene_expression_data)]
row.names(gene_expression_data) <-
gene_expression_data_info$gene_nm
common_individuals <-
intersect(colnames(haplotype_data_h1),
colnames(gene_expression_data))
if (length(common_individuals) == 0)
stop (
"Please make sure that individuals' names are consistent across phased haplytpes, gene expression and covariates files."
)
###Covariants
if (covariate_file_path != "")
{
covariates_data <-
read.table(covariate_file_path, header = T)
covariates_data_indiv <- covariates_data$ID
covariates_data <- covariates_data[, 2:length(covariates_data)]
print(paste(
nrow(covariates_data),
"covariates for",
length(covariates_data),
"individuals are loaded."
),
quote = F)
row.names(covariates_data) <- covariates_data_indiv
common_individuals <-
intersect(common_individuals, colnames(covariates_data))
if (length(common_individuals) == 0)
stop (
"Please make sure that individuals' names are consistent across phased haplytpes, gene expression and covariates files."
)
covariates_data <-
as.matrix(covariates_data[, common_individuals], ncol = length(common_individuals))
cvrt_cnt <- nrow(covariates_data)
} else
{
cvrt_cnt <- 0
}
haplotype_data_h1 <- haplotype_data_h1[, common_individuals]
haplotype_data_h2 <- haplotype_data_h2[, common_individuals]
gene_expression_data <- gene_expression_data[, common_individuals]
for (i in 1:length(haplotype_data_h1))
{
if (colnames(haplotype_data_h1)[[i]] != colnames(gene_expression_data)[[i]])
stop("Individualsarenotsorted!")
if (cvrt_cnt > 0)
if (colnames(haplotype_data_h1)[[i]] != colnames(covariates_data)[[i]])
stop("Individualsarenotsorted!")
}
if (block_haplotype_assessment | block_genotype_assessment)
{
haplotype_blocks <-
read.table(haplotype_blocks_file_path,
header = TRUE,
stringsAsFactors = F)
haplotype_blocks <-
filter(haplotype_blocks, CHR == chrom |
CHR == paste0("chr", chrom))
if (nrow(haplotype_blocks) == 0)
stop (paste0(
"No SNPs are left after filtration based on chromosome! (chromsome = ",
chrom,
")."
))
haplotype_blocks$START_SNP <-
sapply(as.character(haplotype_blocks$SNPS), function(x)
strsplit(x, "[|]")[[1]][[1]])
haplotype_blocks$END_SNP <-
sapply(as.character(haplotype_blocks$SNPS), function(x)
tail(strsplit(x, "[|]")[[1]], n = 1))
haplotype_blocks <-
haplotype_blocks[, c("CHR", "START_SNP", "END_SNP", "SNPS")]
haplotype_blocks <-
left_join(haplotype_blocks,
haplotype_data_info[, c("ID", "POS", "indx")],
by = c("START_SNP" = "ID"))
colnames(haplotype_blocks)[[length(haplotype_blocks) - 1]] <-
"start_pos"
colnames(haplotype_blocks)[[length(haplotype_blocks)]] <- "START_ID"
haplotype_blocks <-
left_join(haplotype_blocks,
haplotype_data_info[, c("ID", "POS", "indx")],
by = c("END_SNP" = "ID"))
colnames(haplotype_blocks)[[length(haplotype_blocks) - 1]] <-
"end_pos"
colnames(haplotype_blocks)[[length(haplotype_blocks)]] <- "END_ID"
haplotype_blocks$original_id <- 1:nrow(haplotype_blocks)
}else
{
haplotype_blocks <- data.frame()
}
print(
paste(
nrow(haplotype_data_h1),
"SNPs,",
nrow(haplotype_blocks),
"Blocks",
nrow(gene_expression_data),
"Genes and",
cvrt_cnt,
"covariates for",
length(haplotype_data_h1),
"individuals are passed filtration and will be included in the analysis"
),
quote = F
)
if (permutation_calculation_threshold > 0 &
permutations_num > 0)
{
print(
paste(
"Permutation based p-value will be caluclated for associations with p-value less than ",
permutation_calculation_threshold,
"(based on",
1000 * ceiling(permutations_num / 1000),
"permutations)"
),
quote = F
)
}
if (remove_individual_rare & block_haplotype_assessment)
{
print(
paste0(
"Individuals with rare haplotypes (freq < ", haplotype_minimal_frequency ,") will be eleiminated from the assessment.",
" If the remaining individuals are less than ", minimum_individuals ," , the block will be skipped."),
quote = F
)
}
if (remove_individual_rare & block_genotype_assessment)
{
print(
paste0(
"Individuals with rare haplotypes (freq < ", genotype_minimal_frequency ,") will be eleiminated from the assessment.",
" If the remaining individuals are less than ", minimum_individuals ," , the block will be skipped."),
quote = F
)
}
###########################
individual_cnt <- length(haplotype_data_h1)
if (cvrt_cnt > 0) {
covariates_data <-
rbind(matrix(1, 1, individual_cnt), covariates_data)
} else{
covariates_data <- matrix(1, 1, individual_cnt)
}