@@ -315,3 +315,107 @@ Export_GWASpoly <- function(object, file, naIfZeroReads = TRUE){
315315 # export
316316 write.csv(outdata , file = file , row.names = FALSE , quote = FALSE )
317317}
318+
319+ RADdata2VCF <- function (object , file = NULL , asSNPs = TRUE , hindhe = TRUE ,
320+ sampleinfo = data.frame (row.names = GetTaxa(object )),
321+ contigs = data.frame (row.names = unique(object $ locTable $ Chr ))){
322+ # shortcuts to functions to use
323+ DataFrame <- S4Vectors :: DataFrame
324+
325+ varok <- attr(object $ alleleNucleotides , " Variable_sites_only" )
326+ if (is.null(varok ) || varok ){
327+ stop(" Complete haplotype information not provided; unable to determine SNP positions. Use refgenome argument in VCF2RADdata." )
328+ }
329+ if (is.null(object $ locTable $ Chr ) || is.null(object $ locTable $ Pos )){
330+ stop(" Need chromosome and position information (Chr and Pos in locTable)." )
331+ }
332+ if (is.null(object $ locTable $ Ref )){
333+ warning(" Reference allele not indicated. Using the major allele for each locus." )
334+ object $ locTable $ Ref <- object $ alleleNucleotides [OneAllelePerMarker(object , commonAllele = TRUE )]
335+ }
336+ if (nrow(sampleinfo ) != nTaxa(object ) || ! all(rownames(sampleinfo ) %in% GetTaxa(object ))){
337+ " sampleinfo doesn't match taxa in RADdata object."
338+ }
339+
340+ # Determine most probable genotypes, and their ploidies
341+ temp <- GetProbableGenotypes(object , omit1allelePerLocus = FALSE )
342+ geno <- temp $ genotypes
343+ pld_index <- temp $ ploidy_index
344+ pld_ind_per_loc <-
345+ tapply(pld_index , object $ alleles2loc ,
346+ function (x ){
347+ u <- unique(x )
348+ if (length(u ) == 1 ){
349+ return (u )
350+ } else {
351+ return (as.integer(names(which.max(table(x )))))
352+ }
353+ })
354+ pld_per_loc <- sapply(object $ priorProbPloidies , sum )[pld_ind_per_loc ]
355+
356+ # Process data with internal RCpp function
357+ temp <- PrepVCFexport(geno , object $ alleles2loc , object $ alleleDepth ,
358+ object $ alleleNucleotides , object $ locTable , pld_per_loc ,
359+ asSNPs )
360+ REF <- Biostrings :: DNAStringSet(temp $ REF )
361+ ALT <- Biostrings :: DNAStringSetList(temp $ ALT )
362+ CHROM <- as.character(object $ locTable $ Chr )[temp $ Lookup ]
363+ rr <- GenomicRanges :: GRanges(CHROM ,
364+ IRanges :: IRanges(start = temp $ POS ,
365+ width = BiocGenerics :: width(REF )))
366+ fixed <- DataFrame(REF = REF , ALT = ALT )
367+ cd <- DataFrame(row.names = GetTaxa(object ))
368+ if (ncol(sampleinfo ) > 0 ){
369+ cd <- cbind(cd , sampleinfo [GetTaxa(object ),])
370+ colnames(cd ) <- colnames(sampleinfo )
371+ }
372+ DP <- t(object $ locDepth [,as.character(temp $ Lookup )])
373+ rownames(DP ) <- NULL
374+ info <- DataFrame(NS = rowSums(DP > 0 ), DP = rowSums(DP ), LU = temp $ Lookup )
375+ infohdr <- DataFrame(row.names = c(" NS" , " DP" , " LU" ), Number = c(" 1" , " 1" , " 1" ),
376+ Type = c(" Integer" , " Integer" , " Integer" ),
377+ Description = c(" Number of samples with data" , " Combined depth across samples" ,
378+ " Lookup index of marker in RADdata object" ))
379+ metahdr <- DataFrame()
380+ if (ncol(contigs ) > 0 ){
381+ ctg <- DataFrame(row.names = rownames(contigs ), contigs )
382+ colnames(ctg ) <- colnames(contigs )
383+ } else {
384+ ctg <- DataFrame(row.names = rownames(contigs ))
385+ }
386+
387+ # Add Hind/He if desired
388+ if (hindhe ){
389+ infohdr <- rbind(infohdr ,
390+ DataFrame(row.names = " HH" , Number = " 1" , Type = " Float" ,
391+ Description = " Hind/He for the locus in the RADdata object" ))
392+ metahdr <- DataFrame(row.names = " HH" , Number = " 1" , Type = " Float" ,
393+ Description = " Hind/He for the sample, averaged across loci in the RADdata object" )
394+ hh <- HindHe(object )
395+ info $ HH <- colMeans(hh , na.rm = TRUE )[temp $ Lookup ]
396+ cd $ HH <- rowMeans(hh , na.rm = TRUE )
397+ }
398+
399+ # Build VCF object
400+ hdr <- VariantAnnotation :: VCFHeader(reference = rownames(ctg ),
401+ samples = GetTaxa(object ),
402+ IRanges :: DataFrameList(fileformat = DataFrame(row.names = " fileformat" , Value = " VCFv4.3" ),
403+ fileDate = DataFrame(row.names = " fileDate" , Value = gsub(" -" , " " , Sys.Date())),
404+ source = DataFrame(row.names = " source" , Value = paste0(" polyRADv" , packageVersion(" polyRAD" ))),
405+ FORMAT = DataFrame(row.names = c(" GT" , " AD" , " DP" ),
406+ Number = c(" 1" , " R" , " 1" ), Type = c(" String" , " Integer" , " Integer" ),
407+ Description = c(" Genotype" , " Read depth for each allele" , " Read depth" )),
408+ INFO = infohdr , META = metahdr , contig = ctg ))
409+ if (ncol(cd ) > 0 ){
410+ VariantAnnotation :: meta(hdr )$ SAMPLE <- cd
411+ }
412+ vcf <- VariantAnnotation :: VCF(rowRanges = rr , fixed = fixed , info = info , colData = cd ,
413+ geno = S4Vectors :: SimpleList(GT = temp $ GT , AD = temp $ AD , DP = DP ),
414+ exptData = list (header = hdr ), collapsed = TRUE )
415+
416+ # output
417+ if (! is.null(file )){
418+ VariantAnnotation :: writeVcf(vcf , file )
419+ }
420+ return (vcf )
421+ }
0 commit comments