library(GenomicRanges) ################################################### for (noIm in 1:noImplanted) { if (noIm==1) { implRange <- GRanges(seqnames="chr", ranges=IRanges(start=intervalImp[[noIm]][,1], end=intervalImp[[noIm]][,2])) implSamp1 <- implG[[noIm]] } else { implRange <- c(implRange,GRanges(seqnames="chr", ranges=IRanges(start=intervalImp[[noIm]][,1], end=intervalImp[[noIm]][,2]))) implSamp1 <- c(implSamp1,implG[[noIm]]) } } implSamp <- unique(implSamp1) ################################################### allSamp <- c() allSnps <- c() rrS <- c(0) foundAll <- matrix(0,samplesN,snps) if (nrow(resGermline)>0) { samp1V <- as.vector(unlist(resGermline[1])) samp2V <- as.vector(unlist(resGermline[3])) beginV <- as.vector(unlist(resGermline[8])) endV <- as.vector(unlist(resGermline[9])) beginV[which(beginV==0)] <- 1 lengthIBDV <- pos[endV] - pos[beginV] w1 <- which(lengthIBDV>shunkFact*ShunkLength) w2 <- which(lengthIBDV<1.2*1000*lengthI) w3 <- intersect(w1,w2) samp1V <- samp1V[w3] samp2V <- samp2V[w3] beginV <- beginV[w3] endV <- endV[w3] allSamp <- union(samp1V,samp2V) gr <- GRanges(seqnames="chr", ranges=IRanges(start=beginV, end=endV)) gr1 <- reduce(gr) theSnps <- rep(0,snps) sG <- start(gr1) eG <- end(gr1) for (i in 1:length(sG)) { range <- sG[i]:eG[i] theSnps[range] <- 1 } allSnps <- which(theSnps>0) Rsum <- function(i) { sel <- which(samp1V == i | samp2V == i) sum(width(reduce(gr[sel]))) } rrS <- sapply(1:samplesN,Rsum) R1 <- which(gr %in% implRange) R21 <- which(samp1V %in% implSamp) R22 <- which(samp2V %in% implSamp) R2 <- union(R21,R22) RA <- intersect(R1,R2) samp1V <- samp1V[RA] samp2V <- samp2V[RA] beginV <- beginV[RA] endV <- endV[RA] gr2 <- gr[RA] Psum <- function(i) { sel1 <- which(implSamp1== i) if (length(sel1)>0) { sel <- which(samp1V == i | samp2V == i) grn <- intersect(gr2[sel],implRange[sel1]) sum(width(reduce(grn))) } else { 0 } } rrP <- sapply(1:samplesN,Psum) } detected <- sum(rrS) all <- samplesN*snps Pos <- sum(implantT) trueP <- sum(rrP)