diff --git a/R/combineArrays.R b/R/combineArrays.R index f187cea..59bdf6f 100644 --- a/R/combineArrays.R +++ b/R/combineArrays.R @@ -34,7 +34,8 @@ outType <- match.arg(outType) .isRGOrStop(rgSet) - stopifnot(.is450k(rgSet) || .isEPIC(rgSet)) + # Make EPICv2 compatible | Add type EPICv2 + stopifnot(.is450k(rgSet) || .isEPIC(rgSet) || .isEPICv2(rgSet)) array <- annotation(rgSet)["array"] if (array == outType) stop("'rgSet' already in the 'outType' array type.") @@ -54,15 +55,24 @@ commonNames <- intersect(probes1$Name, probes2$Name) probes1 <- probes1[match(commonNames, probes1$Name), ] probes2 <- probes2[match(commonNames, probes2$Name), ] + # Make EPICv2 compatible | EPICv2 remove different design probes + if (.isEPICv2(rgSet)){ + Diff_Color_Indices <- which(probes1$Color != probes2$Color) + commonNames <- setdiff(commonNames, probes2$Name[Diff_Color_Indices]) + Diff_ProbeSeqA_Indices <- which(probes1$ProbeSeqA != probes2$ProbeSeqA) + commonNames <- setdiff(commonNames, probes2$Name[Diff_ProbeSeqA_Indices]) + probes1 <- probes1[match(commonNames, probes1$Name), ] + probes2 <- probes2[match(commonNames, probes2$Name), ] + } stopifnot(all(probes1$Color == probes2$Color)) stopifnot(all(probes1$ProbeSeqA == probes2$ProbeSeqA)) stopifnot(all(probes1$ProbeSeqB == probes2$ProbeSeqB)) # Translating rgSet2 addresses to rgSet1 addresses translate <- c(probes1$AddressA, probes1$AddressB) names(translate) <- c(probes2$AddressA, probes2$AddressB) - wh <- which(rownames(rgSet) %in% names(translate)) - rownames(rgSet)[wh] <- translate[rownames(rgSet)[wh]] - keepAddresses$I <- unname(translate) + # Fix Bug | Save relation between rgSet2 addresses and rgSet1 addresses. Don't change rownames(rgSet) here + keepAddresses$I <- names(translate) + translate_all <- translate # Probes of Type II probes1 <- getProbeInfo(manifest1, type = "II") @@ -70,13 +80,20 @@ commonNames <- intersect(probes1$Name, probes2$Name) probes1 <- probes1[match(commonNames, probes1$Name),] probes2 <- probes2[match(commonNames, probes2$Name),] + # Make EPICv2 compatible | EPICv2 remove different design probes + if (.isEPICv2(rgSet)){ + Diff_ProbeSeqA_Indices <- which(probes1$ProbeSeqA != probes2$ProbeSeqA) + commonNames <- setdiff(commonNames, probes2$Name[Diff_ProbeSeqA_Indices]) + probes1 <- probes1[match(commonNames, probes1$Name), ] + probes2 <- probes2[match(commonNames, probes2$Name), ] + } stopifnot(all(probes1$ProbeSeqA == probes2$ProbeSeqA)) # Translating rgSet2 addresses to rgSet1 addresses translate <- probes1$AddressA names(translate) <- probes2$AddressA - wh <- which(rownames(rgSet) %in% names(translate)) - rownames(rgSet)[wh] <- translate[rownames(rgSet)[wh]] - keepAddresses$II <- unname(translate) + # Fix Bug | Save relation between rgSet2 addresses and rgSet1 addresses. Don't change rownames(rgSet) here + keepAddresses$II <- names(translate) + translate_all <- c(translate_all, translate) # Probes of Type SnpI probes1 <- getProbeInfo(manifest1, type = "SnpI") @@ -84,14 +101,23 @@ commonNames <- intersect(probes1$Name, probes2$Name) probes1 <- probes1[match(commonNames, probes1$Name),] probes2 <- probes2[match(commonNames, probes2$Name),] + # Make EPICv2 compatible | EPICv2 remove different design probes + if (.isEPICv2(rgSet)){ + Diff_ProbeSeq1A2B_Indices <- which(probes1$ProbeSeqA != probes2$ProbeSeqB) + commonNames <- setdiff(commonNames, probes2$Name[Diff_ProbeSeq1A2B_Indices]) + Diff_ProbeSeq1B2A_Indices <- which(probes1$ProbeSeqB != probes2$ProbeSeqA) + commonNames <- setdiff(commonNames, probes2$Name[Diff_ProbeSeq1B2A_Indices]) + probes1 <- probes1[match(commonNames, probes1$Name), ] + probes2 <- probes2[match(commonNames, probes2$Name), ] + } stopifnot(all(probes1$ProbeSeqA == probes2$ProbeSeqB)) stopifnot(all(probes1$ProbeSeqB == probes2$ProbeSeqA)) # Translating rgSet2 addresses to rgSet1 addresses translate <- c(probes1$AddressA, probes1$AddressB) names(translate) <- c(probes2$AddressA, probes2$AddressB) - wh <- which(rownames(rgSet) %in% names(translate)) - rownames(rgSet)[wh] <- translate[rownames(rgSet)[wh]] - keepAddresses$SnpI <- unname(translate) + # Fix Bug | Save relation between rgSet2 addresses and rgSet1 addresses. Don't change rownames(rgSet) here + keepAddresses$SnpI <- names(translate) + translate_all <- c(translate_all, translate) # Probes of Type SnpII probes1 <- getProbeInfo(manifest1, type = "SnpII") @@ -103,9 +129,9 @@ # Translating rgSet2 addresses to rgSet1 addresses translate <- probes1$AddressA names(translate) <- probes2$AddressA - wh <- which(rownames(rgSet) %in% names(translate)) - rownames(rgSet)[wh] <- translate[rownames(rgSet)[wh]] - keepAddresses$SnpII <- unname(translate) + # Fix Bug | Save relation between rgSet2 addresses and rgSet1 addresses. Don't change rownames(rgSet) here + keepAddresses$SnpII <- names(translate) + translate_all <- c(translate_all, translate) # Probes of Type Control probes1 <- getProbeInfo(manifest1, type = "Control") @@ -119,6 +145,13 @@ keepAddresses <- do.call("c", keepAddresses) keepAddresses <- keepAddresses[keepAddresses %in% rownames(rgSet)] rgSet <- rgSet[keepAddresses, ] + # Fix Bug | Change rownames(rgSet) to rgSet1 addresses here + wh <- which(rownames(rgSet) %in% names(translate_all)) + rownames(rgSet)[wh] <- translate_all[rownames(rgSet)[wh]] + # Set rownames(Green) and rownames(Red) to rgSet1 addresses + rownames(rgSet@assays@data@listData$Green) <- rownames(rgSet) + rownames(rgSet@assays@data@listData$Red) <- rownames(rgSet) + # Set annotation to outType annotation(rgSet) <- .getAnnotationFromOutType(outType) rgSet } diff --git a/R/utils.R b/R/utils.R index 14218cb..91a3d94 100644 --- a/R/utils.R +++ b/R/utils.R @@ -202,6 +202,10 @@ ilogit2 <- function(x) 2^x / (1 + 2^x) annotation(object)["array"] == "IlluminaHumanMethylationEPIC" } +.isEPICv2 <- function(object) { + annotation(object)["array"] == "IlluminaHumanMethylationEPICv2" +} + .isAllergy <- function(object) { annotation(object)["array"] == "IlluminaHumanMethylationAllergy" }