我有一个 R 脚本,在其中我可以在映射后从 .sam 文件中读取行,并且我想将 sam 文件的行解析为字符串,以便更轻松地操作它们并创建我想要的 wig 文件或计算我需要的 cov3 和 cov5。你能帮我让这个脚本更快地工作吗?如何更快地将巨大的 .sam 文件的行解析为数据帧?这是我的脚本:
gc()
rm(list=ls())
exptPath <- "/home/dimitris/INDEX3PerfectUnique31cov5.sam"
lines <- readLines(exptPath)
pos = lines
pos
chrom = lines
chrom
pos = ""
chrom = ""
nn = length(lines)
nn
# parse lines of sam file into strings(this part is very very slow)
rr = strsplit(lines,"\t", fixed = TRUE)
rr
trr = do.call(rbind.data.frame, rr)
pos = as.numeric(as.character(trr[8:nn,4]))
# for cov3
#pos = pos+25
#pos
chrom = trr[8:nn,3]
pos = as.numeric(pos)
pos
tab1 = table(chrom,pos, exclude="")
tab1
ftab1 = as.data.frame(tab1)
ftab1 = subset(ftab1, ftab1[3] != 0)
ftab1 = subset(ftab1, ftab1[1] != "<NA>")
oftab1 = ftab1[ order(ftab1[,1]), ]
final.ftab1 = oftab1[,2:3]
write.table(final.ftab1, "ind3_cov5_wig.txt", row.names=FALSE,
sep=" ", quote=FALSE)