R/expectedDistance.aldex.r

Defines functions aldex.expectedDistance

Documented in aldex.expectedDistance

aldex.expectedDistance <- function(clrData) {
    if (class(clrData) != "aldex.clr" && class(clrData) != "list") {
        stop("Please supply a valid aldex.clr object.")
    }

    d.mc <- getMonteCarloInstances(clrData)
    distances <- array(0, dim=c(length(getSampleIDs(clrData)), length(getSampleIDs(clrData)), clrData@mc.samples))

    message("computing distances for each instance...")
    # Transpose call makes each row a sample and each col a feature for dist()
    for (i in 1:clrData@mc.samples) {
        distances[,,i] <- distances[,,i] + as.matrix(dist(t(sapply(d.mc, function(x) {x[,i]}))))
    }

    # Apply median() across all distance-value instances for each sample pair
    message("computing median distance across instances...")
    expectedDist <- as.matrix(apply(distances, c(1, 2), median))

    rownames(expectedDist) <- getSampleIDs(clrData)
    colnames(expectedDist) <- getSampleIDs(clrData)

    return(as.dist(expectedDist))
}

Try the ALDEx2 package in your browser

Any scripts or data that you put into this service are public.

ALDEx2 documentation built on Nov. 8, 2020, 8:05 p.m.