Error in mapply(function(x, y)
1
0
Entering edit mode
2.6 years ago

Dear all,

I am working on R and using the rehh package. I am converting the vcf file to an object of haplohh-class using the command

> hp_vcf <- data2haplohh(hap_file = "sample.vcf", polarize_vcf = FALSE, vcf_reader = "data.table", chr.name = "CM004562.1", verbose = TRUE)

But I am getting an error

* Reading input file(s) *
Using package 'data.table' to read vcf.
Extracting map information.
Extracting haplotypes.
Number of individuals which are 
Haploid Diploid Triploid, ... : 
1 2 
0 1 
Error in mapply(function(x, y) { : 
  zero-length inputs cannot be mixed with those of non-zero length

Someone please help me in resolving this error. Any help would be highly appreciated

Thanks in advance

rehh R • 948 views
ADD COMMENT
2
Entering edit mode
2.6 years ago

I am unfortunately not familiar with the package, but out of curiosity, I had a look at the source code of that function.

The error is thrown by the function that will create haplotype names out of the combination of individuals and the ploidy. The names of the individuals are taken from colnames(gt), but there are apparently no column names.

if (nrow(gt) > 0) {
  # set haplotype names as individual + underscore + 1:ploidy
  rownames(tmp_haplo) <-
    as.vector(unlist(
      mapply(function(x, y) {
        if (y == 1) {
          # haploid -> return un-changed
          x
        } else{
          # return vector c("HG1_1",...,"HG1_y")
          paste(x, 1:y, sep = "_")
        }
      }, colnames(gt), ind_ploidy, USE.NAMES = FALSE),
      use.names = FALSE
    ))
}

So it seems that you are trying to read a vcf file with multiple samples, however there are issues with reading the header. Therefore, I would try to manually read your file in the same manner as the function and inspect the result:

library(data.table)
      gt <- sub(":.*$", "", as.matrix(
        data.table::fread(
          "sample.vcf",
          skip = "#CHROM",
          drop = c(
            "#CHROM",
            "POS",
            "ID",
            "REF",
            "ALT",
            "QUAL",
            "FILTER",
            "INFO",
            "FORMAT"
          ),
          stringsAsFactors = FALSE,
          showProgress = FALSE,
        )
      ))

So on this matrix, have a look what colnames(gt) looks like...

Alternatively, you could try the external VCF reader package that is supported: library(vcfR)

Good luck!

ADD COMMENT
0
Entering edit mode

I am very grateful to Matthias Zepper for the quick response and guidance. I am going to try the vcfR package to read the vcf file. Hopefully, it will work. Thank you so much.

ADD REPLY

Login before adding your answer.

Traffic: 2058 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6