It seems that many people have issues with both splitting up VCF files, and iterating over columns with a for-loop, but I haven't come across any questions that tackles the two in a way relevant to working with a VCF file containing many samples - as will be explained.
: :
Loci Sample1
[1] 0/1:15:55:54:49:5:9.26%:2.8371E-2:37:36:49:0:5:0
[2] 0/1:42:55:53:40:13:24.53%:5.2873E-5:34:37:40:0:13:0
[3] 0/1:15:54:54:49:5:9.26%:2.8371E-2:35:33:49:0:5:0
The question is how to create an eye-friendly table over many loci (rows) and multiple samples (columns) with lots of output statistics (each separated by ":")?
: :
I have developed an R script which can take the information from a single sample column and output a matrix that separates each individual statistic. The code is as follows:
data <- vcf.small
# First, create a list representing each row (locus) and separate the
# statistics; second, breakdown the list's structure but maintain data order.
split1 <-strsplit(as.character(data$Sample1),":")
split2 <- unlist(split1)
# Create a matrix: here, there are 14 values by 3 loci.
mtx1a <- matrix(split2, ncol=14, nrow=3, dimnames=list(NULL,c("GT","GQ","SDP","DP","RD","AD","FREQ","PVAL","RBQ","ABQ","RDF","RDR","ADF","ADR")), byrow=TRUE)
# Create some additional variables (columns) to add to the matrix.
sample <- matrix(rep(1,3), ncol=1, nrow=3, dimnames=list(NULL,c("SAMPLE")))
locus <- matrix(1:3, ncol=1, nrow=3, dimnames=list(NULL,c("LOCUS")))
# Add them to the matrix.
mtx1b <- cbind(mtx1a,sample)
mtx1b <- cbind(mtx1b,locus)
Voila, the output:
GT GQ SDP DP RD AD FREQ PVAL RBQ ABQ RDF RDR ADF ADR SAMPLE LOCUS
[1,] "0/1" "15" "55" "54" "49" "5" "9.26%" "2.8371E-2" "37" "36" "49" "0" "5" "0" "1" "1"
[2,] "0/1" "42" "55" "53" "40" "13" "24.53%" "5.2873E-5" "34" "37" "40" "0" "13" "0" "1" "2"
[3,] "0/1" "15" "54" "54" "49" "5" "9.26%" "2.8371E-2" "35" "33" "49" "0" "5" "0" "1" "3"
: :
The output is perfect, but now I can't for the life of me figure out how to make a for-loop that encompasses the above code to create a separate matrix for each sample. I reasoned:
for(i in names(data){
split[i] <-strsplit(as.character(data$[i]),":")
split[i] <- unlist(split[i])
mtx[i]a <- matrix(split2, ncol=14, nrow=3,
[etc etc..]
}
The problem is that I need to create customized individual variables to set up each matrix for each of the samples (ie the columns). However, R will not take [i] as a place-holder, where i = the sample(/column) name.
Ideally, each sample(/column) specific variable would read as: "splitSample1", "splitSample2", "splitSample3", etc. This is mainly to allow the for-loop to process all the columns without having to recreate code specific for each column name. I guess what I am trying to do is recreate the "$i" syntax from Linux, but obviously that doesn't work here.
Resolving this issue will make working with very large data sets much more manageable, and I have really tried searching for work-arounds. Any help is much appreciated!
I think it is better to store the results in a data.frame
or data.table
as the class
type are different for each split column. matrix
can store only a single class. If there is a single character
column, the class will be character
for all the columns
.
Using the devel
version of data.table
, we can use tstrsplit
to split into columns as well as change the class
with type.convert=TRUE
. The devel version can be installed from here
library(data.table)#v1.9.5+
nm1 <- c('GT', 'GQ', 'SDP', 'DP', 'RD', 'AD', 'FREQ', 'PVAL', 'RBQ',
'ABQ', 'RDF', 'RDR', 'ADF', 'ADR')
setDT(data)[, (nm1):=tstrsplit(Sample1, ':', type.convert=TRUE)][,
Sample1:=NULL][, c('sample', 'locus'):= list(1, 1:3)][]
# GT GQ SDP DP RD AD FREQ PVAL RBQ ABQ RDF RDR ADF ADR sample locus
#1: 0/1 15 55 54 49 5 9.26% 2.8371e-02 37 36 49 0 5 0 1 1
#2: 0/1 42 55 53 40 13 24.53% 5.2873e-05 34 37 40 0 13 0 1 2
#3: 0/1 15 54 54 49 5 9.26% 2.8371e-02 35 33 49 0 5 0 1 3
If there are multiple 'Sample' columns in the dataset, we can use lapply
to loop over the columns and create the split datasets in a list ('lst').
nm2 <- paste0('splitSample', 1:ncol(data2))
lst <- setNames(
lapply(seq_len(ncol(data2)), function(i)
setDT(list(data2[,i]))[, (nm1) := tstrsplit(V1, ":",
type.convert=TRUE)][, V1:=NULL][,
c('sample', 'locus'):= list(i, 1:.N)]),
nm2)
It would be easier to work in a 'list', but if we need to have separate dataset objects in the global environment (not recommended), we can use list2env
.
list2env(lst, envir=.GlobalEnv)
splitSample1
# GT GQ SDP DP RD AD FREQ PVAL RBQ ABQ RDF RDR ADF ADR sample locus
#1: 0/1 15 55 54 49 5 9.26% 2.8371E-2 37 36 49 0 5 0 1 1
#2: 0/1 42 55 53 40 13 24.53% 5.2873E-5 34 37 40 0 13 0 1 2
#3: 0/1 15 54 54 49 5 9.26% 2.8371E-2 35 33 49 0 5 0 1 3
splitSample2
# GT GQ SDP DP RD AD FREQ PVAL RBQ ABQ RDF RDR ADF ADR sample locus
#1: 0/2 15 55 55 49 5 10.26% 2.971E-2 37 32 49 0 5 0 2 1
#2: 0/2 52 55 53 40 13 22.53% 1.2873E-5 34 37 12 0 13 0 2 2
#3: 0/2 17 54 54 49 18 9.29% 3.8371E-2 42 33 49 0 5 0 2 3
NOTE: Here, I used the input dataset as a data.frame.
data <- structure(list(Sample1 =
c("0/1:15:55:54:49:5:9.26%:2.8371E-2:37:36:49:0:5:0",
"0/1:42:55:53:40:13:24.53%:5.2873E-5:34:37:40:0:13:0",
"0/1:15:54:54:49:5:9.26%:2.8371E-2:35:33:49:0:5:0"
)), .Names = "Sample1", class = "data.frame", row.names = c(NA, -3L))
data2 <- structure(list(Sample1 =
c("0/1:15:55:54:49:5:9.26%:2.8371E-2:37:36:49:0:5:0",
"0/1:42:55:53:40:13:24.53%:5.2873E-5:34:37:40:0:13:0",
"0/1:15:54:54:49:5:9.26%:2.8371E-2:35:33:49:0:5:0"
), Sample2 = c("0/2:15:55:55:49:5:10.26%:2.971E-2:37:32:49:0:5:0",
"0/2:52:55:53:40:13:22.53%:1.2873E-5:34:37:12:0:13:0",
"0/2:17:54:54:49:18:9.29%:3.8371E-2:42:33:49:0:5:0")),
.Names = c("Sample1", "Sample2"), class = "data.frame",
row.names = c(NA, -3L))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.