-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrunMBASED_1VCF.R
More file actions
214 lines (151 loc) · 6.8 KB
/
runMBASED_1VCF.R
File metadata and controls
214 lines (151 loc) · 6.8 KB
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
# Script reads annotated vcf and performs MBASED ASE analysis
#' title: "MBASED Allele-Specific Expression analysis report"
#' output:
#' html_document:
#' toc: true
#' ---
args <- commandArgs(trailingOnly = TRUE)
####----------------MBASED-------------------
suppressPackageStartupMessages({
library("knitr")
library("VariantAnnotation")
library("MBASED")
library("BiocParallel")
library(ggplot2)
library(plotly)
library(rtracklayer)
### devtools::install_github('hadley/ggplot2')
})
set.seed(988482)
fileInput=args[1]
fileOutput=args[2]
noSim=as.integer(args[3])
vcf = readVcf(fileInput)
vcf = readVcf('GSM1206240.decontaminated.genome.split.realigned.recalibrated.hc.vep_phased.vcf')
#------Filter SNPs
vcf <- vcf[isSNV(vcf)]
vcf <- vcf[as.vector(geno(vcf)$PH_info!='1|1')]
vcf <- vcf[lapply(info(vcf)$CSQ,length)>0]
#-------------Filter SNPs in exons
# Find position of GENE names in vep annotated vcf
vepD <- info(header(vcf))[c("CSQ"),]$Description
vepD <- strsplit(vepD,'Format:', fixed=TRUE)[[1]][2]
vepD <- trimws(vepD, ("b"))
vepD <- strsplit(vepD,'|', fixed=TRUE)[[1]]
pos <- match("SYMBOL", vepD)
posID <- match("Gene", vepD)
#vcf <- vcf[sapply(info(vcf)$CSQ, function(x) sum(grepl('exon',x)))>0]
mutInGene <- function(x, p){ strsplit(x, '|', fixed=TRUE)[[1]][p]}
geneName <- function(x, p){
l = as.character(lapply(x, mutInGene, p))
paste(unique(l), sep='',collapse = ';')
}
geneList <- sapply(info(vcf)$CSQ, geneName, posID)
geneNameList <- sapply(info(vcf)$CSQ, geneName, pos)
if(length(vcf)==0){
message ("No alleles in Genes")
quit()
}
##------------Phase SNVs-------------------
mamatata = data.frame(Ph1 = as.data.frame(rowRanges(vcf)$REF)$x,
Ph2 = as.data.frame(rowRanges(vcf)$ALT)$value,
Ph1_count = sapply(geno(vcf)$AD, "[[", 1),
Ph2_count = sapply(geno(vcf)$AD, "[[", 2),
phasing_info = as.vector(unname(geno(vcf)$PH_info)),
ranges=ranges(rowRanges(vcf)),
seqnames=as.character(seqnames(vcf)),
geneName=geneList)
rownames(mamatata) = names(rowRanges(vcf))
#Swap Ph1, Ph2, Ph1_count, Ph2_count for phasing_info = 1|0
mamatata[mamatata$phasing_info == "1|0", c("Ph1", "Ph2", "Ph1_count", "Ph2_count")] <-
mamatata[mamatata$phasing_info == "1|0", c("Ph2", "Ph1", "Ph2_count", "Ph1_count")]
##-----------MBASED--Prepare samples-------------------
mySNVs <- GRanges(seqnames=mamatata$seqnames,
ranges=IRanges(start=mamatata$ranges.start,
end = mamatata$ranges.end,
width = mamatata$ranges.width,
names = mamatata$ranges.names),
aseID=geneList,
allele1=as.character(mamatata$Ph1),
allele2=as.character(mamatata$Ph2))
mySample <- SummarizedExperiment(assays=list(
lociAllele1Counts=matrix(mamatata$Ph1_count, ncol=1, dimnames=list(names(mySNVs), 'mySample')),
lociAllele2Counts=matrix(mamatata$Ph2_count, ncol=1, dimnames=list(names(mySNVs), 'mySample'))),
rowRanges=mySNVs)
#----Run MBASED----PHASED--------
ASEres <- runMBASED(
ASESummarizedExperiment=mySample,
isPhased=TRUE,
numSim=noSim,
BPPARAM = MulticoreParam())
#------Write results------
df = data.frame(rownames(assays(ASEres)$majorAlleleFrequency),
assays(ASEres)$majorAlleleFrequency,
assays(ASEres)$pValueASE,
p.adjust(assays(ASEres)$pValueASE, "fdr"),
assays(ASEres)$pValueHeterogeneity,
p.adjust(assays(ASEres)$pValueHeterogeneity, "fdr"))
df1 = data.frame(rownames(assays(metadata(ASEres)$locusSpecificResults)$MAF),
assays(metadata(ASEres)$locusSpecificResults)$MAF,
assays(metadata(ASEres)$locusSpecificResults)$allele1IsMajor)
colnames(df)=c('Gene', 'majorAlleleFrequency','pValueASE', 'adjPvalueASE','pValueHeterogeneity', 'adjPvalueHeterogeneity')
colnames(df1)=c('Allele', 'MAF', 'allele1IsMajor')
#' counts matrix size:
#+ matrix size, echo = F, warning = F, error = F, message = F
cat(paste("One-sample analysis with counts over", nrow(df), "transcripts\n"))
#' ### Table of adjusted p-values transcripts:
#+ Result table, echo = F, warning = F, error = F, message = F
head(df)
#' ### Table of locus specific results:
#+ Result table1, echo = F, warning = F, error = F, message = F
head(df1)
# Write csv file on the output of the tool
write.table(as.data.frame(df), file=paste0(fileOutput,".tsv"), sep="\t", row.names = F, quote = FALSE)
#write.table(df,fileOutput, quote=FALSE, row.names = FALSE, sep=" ")
#' ### Histogram of adjusted p-values
#+ pvalhist, fig.width = 12, fig.height = 12, echo = F, warning = F, error = F, message = F
hist(as.numeric(df[,2]), main = "Histogram of ASE p-values", xlab = "Adjusted p-values", breaks = 5, col = "powderblue")
# Output workspace image.
save.image(paste0(fileOutput,".env.RData"))
# R data file output file
p <- rowRanges(ASEres)
save(p, file=paste0(fileOutput,".out.RData"))
############ Read GTF and find gene positions#########
gtf1 <- readGFF("Homo_sapiens.GRCh37.87.gtf", version=2L)
gtf <- gtf1[gtf1$type=='gene',]
gtf <- gtf[ , colSums(is.na(gtf)) == 0]
g <- gtf[gtf$gene_id %in% df[,'Gene'],]
## Ne slazu se dimenzije!!!!
d1 = df[df[,'Gene'] %in% g$gene_id,]
geneChr <- g$seqid
geneID <- g$gene_id
geneStart <- g$start
geneEnd <- g$end
geneStrand <- g$strand
geneP <- sapply(geneID, function(x, df) df[df$Gene==x,]$adjPvalueASE, df)
data <- data.frame(geneChr, geneID, geneStart, geneEnd, geneP, geneStrand)
fancy_scientific <- function(l) {
# turn in to character string in scientific notation
l <- format(l, scientific = TRUE)
# quote the part before the exponent to keep all the digits
l <- gsub("^(.*)e", "'\\1'e", l)
# turn the 'e+' into plotmath format
l <- gsub("e", "%*%10^", l)
# return this as an expression
parse(text=l)
}
data$geneChr <- as.numeric(as.character(data$geneChr))
#data$geneChr <- as.numeric(levels(data$geneChr))[data$geneChr]
p <- ggplot() + scale_y_continuous(name="Chromosome", breaks = c(1:23)) +
scale_x_continuous(name="Gene Position",labels=fancy_scientific)
p <- p + geom_rect(data = data[data$geneStrand=='+',], aes(xmax = geneStart, xmin = geneEnd, ymax = geneChr + 0.4, ymin = geneChr + 0.1, fill=geneP, color=geneP))
#p <- p + scale_fill_gradient(low="green", high="green4")
p <- p + geom_rect(data = data[data$geneStrand=='-',], aes(xmax = geneStart, xmin = geneEnd, ymax = geneChr - 0.1, ymin = geneChr - 0.4, fill=geneP, color=geneP))
#p <- p + scale_fill_gradient2(low="gray90", high="blue")
#p <- p + coord_cartesian(xlim = c(5000, 1000000))
p
p <- ggplotly(p)
p
#' # Session info
#+ session, echo = F, warning = F, error = F, message = F
sessionInfo()