cn.MOPS fails with 'missing value where TRUE/FALSE needed' I am trying to use cn.MOPS to call CNVs on a set of whole exome sequencing data bam files. My script (up to the point where it fails) is:

source("https://bioconductor.org/biocLite.R")
library(cn.mops)
BAMFiles &lt;- c('sample1.bam', 'sample2.bam', 'sample3.bam', 'sample4.bam', 'sample5.bam', 'sample6.bam', 'sample7.bam')
segments &lt;- read.table("region.bed",sep="\t",as.is=TRUE)
gr &lt;- GRanges(segments[,1],IRanges(segments[,2],segments[,3]))
X &lt;- getSegmentReadCountsFromBAM(BAMFiles,GR=gr,parallel=11)
resCNMOPS &lt;- exomecn.mops(X)


The last command, resCNMOPS &lt;- exomecn.mops(X) fails with:

Error in if (all(segMedianT == 0)) { : 
  missing value where TRUE/FALSE needed
In addition: Warning message:
In normalizeChromosomes(X, chr = chr, normType = normType, qu = normQu,  :
  Normalization for reference sequence  chrUn_gl000228 not applicable, because of low number of segments


If I run traceback immediately afterwards, I get:

&gt; traceback()
4: FUN(newX[, i], ...)
3: apply(sINI[chrIdx, , drop = FALSE], 2, segment, minSeg = minWidth, 
       ...)
2: cn.mops(input = input, I = I, classes = classes, priorImpact = priorImpact, 
       cyc = cyc, parallel = parallel, normType = normType, normQu = normQu, 
       norm = norm, sizeFactor = sizeFactor, quSizeFactor = quSizeFactor, 
       lowerThreshold = lowerThreshold, upperThreshold = upperThreshold, 
       minWidth = minWidth, segAlgorithm = segAlgorithm, minReadCount = minReadCount, 
       useMedian = useMedian, returnPosterior = returnPosterior, 
       ...)
1: exomecn.mops(X)


I am using R version 3.4.4 on an Ubuntu 16.04.5 LTS machine.

My bed file only has one region which is on chrUn_gl000228:

$ grep chrUn_gl000228 region.bed 
chrUn_gl000228  112604  113879


If I remove that region and rerun the same code, it works as expected. So, my question is, how can I detect this and fix it automatically? Given a collection of bam files and the bed file describing the capture regions used to sequence the samples and generate the bam files, how can I check if cn.MOPS will complain about any of the segments?

Presumably, once I have built these two objects:

segments &lt;- read.table("region.bed",sep="\t",as.is=TRUE)
gr &lt;- GRanges(segments[,1],IRanges(segments[,2],segments[,3]))
X &lt;- getSegmentReadCountsFromBAM(BAMFiles,GR=gr,parallel=11)


I should be able to then check them and see if they have the right data and, if not, remove the offending segment from the GRanges object. But how can I do this?
I managed to track down what was producing this error. The problem was that my bed file only had one segment for chromosome chrUn_gl000228. However, the cn.MOPS method relies on normalizing read counts across multiple segments for each chromosome. Since this chromosome only had a single segment, the exomecn.mops(X) was failing. 

So, either remove the single segment from the bed file, or use a bed file that has at least 2 segments on each chromosome. I created a fake segment on chrUn_gl000228 and confirmed that the program worked as expected and didn't throw any errors.