-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoPlot_HUVEC_Interval_Geno.R
154 lines (116 loc) · 5.27 KB
/
autoPlot_HUVEC_Interval_Geno.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
# This script takes three arguments
# - Ensemble gene ID
# - rsID of SNP
# - SNP Coord
# This script outputs a hg38 version of the gwas summary statistics file
# Set global options and load libraries -----------------------------------
options(scipen = 999, "warnPartialMatchDollar"=TRUE)
# Load libraries
suppressPackageStartupMessages(library(optparse))
suppressPackageStartupMessages(library(data.table))
suppressPackageStartupMessages(library(tidyverse))
# List options
option_list = list(
make_option(c("-g", "--geneID"), type="character", default=NULL,
help="Ensemble gene ID. eg ENSG00000050820", metavar="character"),
make_option(c("-r", "--rsID"), type="character", default=NULL,
help="Rs ID of SNP. eg rs11648176", metavar="character"),
make_option(c("-p", "--peakType"), type="character", default=NULL,
help="Specify what peak to plot (any of: ATACseq, CHIPseq)", metavar="character"),
make_option(c("-c", "--Coord"), type="character", default = 5e-08,
help="pvalue threshold for selecting associations.", metavar="character")
);
opt_parser <- OptionParser(option_list=option_list);
opt <- parse_args(opt_parser);
# Data input error messages
if (is.null(opt$geneID)){
print_help(opt_parser)
stop("Please provide gene ID.", call.=FALSE)
}
if (is.null(opt$rsID)){
print_help(opt_parser)
stop("Please provide rs ID.", call.=FALSE)
}
if (is.null(opt$peakType)){
print_help(opt_parser)
stop("Please provide peak type.", call.=FALSE)
}
# Import interval count --------------------------------------------------
# Fetch rsID details
peakID <- paste0(opt$peakType)
#peakID <- c("ATACseq")
if (peakID == "ATACseq") {
interval <- data.frame(fread("/scratch/cellfunc/shared/HUVEC_ATACseq/eQTLs/eQTL_deseq2Norm_noMAF/norm_data/general/normalised_phenotype.tsv"))
# Harmonize names and samples in geno and interval ----------------------------
mapFile <- read.table("/scratch/cellfunc/shared/HUVEC_ATACseq/eQTLs/eQTL_deseq2Norm_noMAF/norm_data/general/mappings_handeling_repeats.tsv", header = T)
names(interval) <- mapFile$Genotype[match(names(interval), mapFile$RNA)]
colnames(interval) <- gsub(".*_S", "E", colnames(interval))
names(interval)[1] <- c("intervalID")
} else if (peakID == "CHIPseq") {
interval <- data.frame(fread("/scratch/cellfunc/shared/HUVEC_ChipSeq/eQTLs/eQTL_deseq2Norm_noMAF/norm_data/general/normalised_phenotype.tsv"))
# Harmonize names and samples in geno and interval ----------------------------
mapFile <- read.table("/scratch/cellfunc/shared/HUVEC_ChipSeq/eQTLs/eQTL_deseq2Norm_noMAF/norm_data/general/mappings_handeling_repeats.tsv", header = T)
names(interval) <- mapFile$Genotype[match(names(interval), mapFile$RNA)]
colnames(interval) <- gsub(".*_S", "E", colnames(interval))
names(interval)[1] <- c("intervalID")
} else {
cat("Please specify peak type")
}
# Extract interval for cand gene
cand <- paste0(opt$geneID)
#cand <- "Interval_22"
candInterval <- interval[interval$intervalID %in% cand, ]
candName <- candInterval$intervalID
# Fetch rsID details
rs <- paste0(opt$rsID)
#rs <- c("rs643058")
rsFile <- paste0("get_rsID_Coord.txt")
write.table(rs, file = rsFile, quote = F, row.names = F, col.names = F)
#system(paste0("grep -wFf ", rsFile, " /scratch/vasccell/cs806/colocalization/dbSNP/hg38.snp151_All.bed | cut -f4,3,10 > rsLookup.txt"))
rsCoord <- read.table("rsLookup.txt")
colnames(rsCoord) <- c("bp", "rsID", "coord")
# TODO
# I need some if commands around here.
#pos <- as.numeric(rsCoord$bp)
cd <- paste0("chr", rsCoord$coord)
#cd <- rsCoord$coord[2]
a1 <- unlist(strsplit(cd, split = ":"))[3]
a2 <- unlist(strsplit(cd, split = ":"))[4]
hom1 <- paste0(a1, a1)
het <- paste0(a1, a2)
hom2 <- paste0(a2, a2)
# Extract geno for cand SNP
exprLoc <- system(paste0("grep -n ", cd, " huvecGeno/huvec_Geno_snpsloc.txt"), intern = T)
toSkip <- as.numeric(gsub(":.*", "", exprLoc))
toSkip <- toSkip - 1
geno <- data.frame(fread("huvecGeno/huvec_Geno.txt", skip = toSkip, nrows = 1, col.names=scan('huvecGeno/huvecGenoHeader.txt', what ="", sep = "\t", quiet = TRUE)))
# Harmonize geno and expr
colnames(geno) <- gsub(".*_S", "E", colnames(geno))
genoSamps <- colnames(geno)[c(colnames(geno) %in% colnames(candInterval))]
candInterval <- candInterval[, genoSamps]
inteSamps <- colnames(candInterval)[c(colnames(candInterval) %in% colnames(geno))]
geno <- geno[, inteSamps]
candInterval <- data.frame(Gene = cand, candInterval)
geno <- data.frame(SNP = rs, geno)
df <- t(rbind(geno[-1], candInterval[-1]))
colnames(df) = c("snp", "gene_expr")
df <- data.frame(df)
df <- df[complete.cases(df), ]
df$snp = as.factor(df$snp)
dflm = lm(df[,"gene_expr"] ~ as.numeric(df[,"snp"]))
dfPlot <- ggplot(df, aes(snp, gene_expr)) +
geom_jitter(colour="darkgrey", position=position_jitter(width=0.25)) +
geom_boxplot(outlier.size=0, alpha=0.6, fill="lightgreen") +
labs(x = rs, y = candName)+
scale_x_discrete(labels=c(hom1,het,hom2))+
theme_bw()
# theme(axis.text.x = element_text(face="bold", color="#993333",
# size=14, angle=45))
ggsave(paste0(cand, "_vs_", rs, "_", peakID, "_Plot.png" ), dfPlot,
scale = 1,
path = paste0("exprGenoPlot/", peakID),
width = 80,
height = 70,
units = "mm",
dpi = 300,
limitsize = TRUE)