-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_utils.R
More file actions
executable file
·86 lines (70 loc) · 2.78 KB
/
Copy pathmain_utils.R
File metadata and controls
executable file
·86 lines (70 loc) · 2.78 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
library(parallel)
library(vcfR)
library(Rsamtools)
library(GenomicFeatures)
library(rtracklayer)
library(plyr)
#' convert VCF to data.frame, filter if needed
#' @param vcf_filename: name of VCF file to convert to data.frame
#' @param isFiltered: boolean to indicate whether the VCF file still needs to be filtered or not based on the FILTER column
#' @return vcf_df: VCF file converted to data.frame
vcf2df <- function(vcf_filename, isFiltered)
{
# read in VCF file
vcf_file <- read.vcfR(vcf_filename, verbose=FALSE)
# convert to data frame
if(nrow(vcf_file@fix) != 1)
vcf_df <- cbind(as.data.frame(getFIX(vcf_file), stringsAsFactors =FALSE), INFO2df(vcf_file))
else if(nrow(vcf_file@fix) == 1)
vcf_df <- cbind(t(as.data.frame(getFIX(vcf_file),stringsAsFactors =FALSE)), INFO2df(vcf_file))
if(nrow(vcf_file@gt) > 0)
vcf_df <- cbind(vcf_df,as.data.frame(vcf_file@gt,stringsAsFactors =FALSE))
# only keep calls that passed all filters
if(!isFiltered) {
vcf_df <- vcf_df[which(is.na(vcf_df$FILTER) | vcf_df$FILTER == "PASS"),]
}
vcf_df$REF <- as.character(vcf_df$REF)
vcf_df$ALT <- as.character(vcf_df$ALT)
return(vcf_df)
}
#' Convert the mutations in the data.frame to the GRanges format.
#' @param mutations_df: mutations in data.frame format
#' @param mutation_type: SSM or SIM
#' @param maintainMetadata: boolean to indicate if the columns other than CHROM, POS, REF and ALT in mutations_df should be maintained
#' @return mutations_granges: mutations in GRange format
convert2GRanges <- function(mutations_df, mutation_type, maintainMetadata=FALSE)
{
mut_locs <- unlist(IRangesList(apply(mutations_df, 1, function(x)
{
if(mutation_type == "sim")
{
if(substr(start=1, stop = 1, x= x["REF"]) != substr(start=1, stop = 1, x= x["ALT"]))
{
start_pos <- as.numeric(as.character(x["POS"]))
end_pos <- start_pos + nchar(as.character(x["REF"]))-1
}
else if(nchar(x["REF"]) > 1)
{
start_pos <- as.numeric(as.character(x["POS"])) + 1
end_pos <- start_pos + nchar(as.character(x["REF"]))-2
}
else if(nchar(x["ALT"]) > 1)
{
start_pos <- as.numeric(as.character(x["POS"]))
end_pos <- start_pos
}
mut_loc <- IRanges(start=start_pos, end=end_pos)
}
else
{
mut_loc <- IRanges(start=as.numeric(as.character(x["POS"])), end=as.numeric(as.character(x["POS"])))
}
return(mut_loc)
})))
mutations_df$CHROM <- gsub("MT", "M", mutations_df$CHROM)
mutations_granges <- GRanges(seqnames=paste("chr", mutations_df$CHROM, sep=""), ranges=mut_locs, strand="*")
names(mutations_granges) <- NULL
if(maintainMetadata)
mcols(mutations_granges) <- mutations_df
return(mutations_granges)
}