Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extractCoverageFromVcf fixed version #155

Closed
shajoezhu opened this issue Oct 23, 2016 · 1 comment
Closed

extractCoverageFromVcf fixed version #155

shajoezhu opened this issue Oct 23, 2016 · 1 comment

Comments

@shajoezhu
Copy link
Member

shajoezhu commented Oct 23, 2016

extractCoverageFromVcf<-function (vcfName, ADFieldIndex = 2) 
{
    h <- function(w) {
        if (any(grepl("gzfile connection", w))) 
            invokeRestart("muffleWarning")
    }
    gzf = gzfile(vcfFile, open = "rb")
    skipNum = 0
    line = withCallingHandlers(readLines(gzf, n = 1), warning = h)
    while (length(line) > 0) {
        if (grepl("##", line)) {
            skipNum = skipNum + 1
        }
        else {
            break
        }
        line = withCallingHandlers(readLines(gzf, n = 1), warning = h)
    }
    close(gzf)
    vcf = read.table(gzfile(vcfName), skip = skipNum, header = T, 
        comment.char = "", stringsAsFactors = FALSE, check.names = FALSE)
    sampleName = names(vcf)[10]
    tmp = vcf[[sampleName]]
    field = strsplit(as.character(tmp), ":")
    tmpCovStr = unlist(lapply(field, `[[`, ADFieldIndex))
    tmpCov = strsplit(as.character(tmpCovStr), ",")
    indexOfCheckingCov.logic = (lapply(tmpCov,'length') != 2)
    chrom = vcf[,1]
    pos = vcf[,2]
    if ( sum(indexOfCheckingCov.logic) > 0 ){
        cat("Drop markers at", which(indexOfCheckingCov.logic),"\n")
        tmpCov = tmpCov[!indexOfCheckingCov.logic]
        chrom = chrom[!indexOfCheckingCov.logic]
        pos = pos[!indexOfCheckingCov.logic]
    }
    refCount = as.numeric(unlist(lapply(tmpCov, `[[`, 1)))
    altCount = as.numeric(unlist(lapply(tmpCov, `[[`, 2)))
    return(data.frame(CHROM = chrom, POS = pos, refCount = refCount, 
        altCount = altCount))
}
@shajoezhu
Copy link
Member Author

This is a hack to filter out the site that vcf sample field doesn't have the second field AD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant