#A function that removes breakpoints - After a breakpoint is removed, it attempts to reconstruct the mosaic, #by finding the two closest segments that can be thought as consecutive. The criterion is #remove.list - a list of indices of the breakpoints that should be removed remove.and.reconstruct <- function(remove.list, breakpoints){ rem.indeces = c() for (index in remove.list){ del.from = del.to = index #if it's first or last bp in chromosome ok: if ((breakpoints[index,2]==breakpoints[index-1,2]) & (breakpoints[index,2] == breakpoints[index+1,2])){ current.chunk = breakpoints[which((breakpoints[,1]==breakpoints[index,1]) & (breakpoints[,2]==breakpoints[index,2])),] bef = current.chunk[1:index,"left.founder"] aft = current.chunk[(index+1):nrow(current.chunk),"left.founder"] matching.distances = match(bef, aft) + (length(bef)-1):0 if (length(na.omit(matching.distances))>0){ dist = min(na.omit(matching.distances)) print(dist) del.from = max(which(matching.distances == dist)) del.to = del.from + dist - 1 } } rem.indeces = c(rem.indeces, del.from:del.to) } breakpoints = breakpoints[-rem.indeces,] print(rem.indeces) return(breakpoints) }