public void addRegion(GenomicFeature r, String featureName) {
boolean featureStrand = r.isPositiveStrand();
Interval newInterval = new Interval(r.getSequenceName(), r.getStart(), r.getEnd(), r.isPositiveStrand(), featureName);
List<Interval> toRemove = new ArrayList<Interval>();
//DEBUG
/*if (featureName.equals("ENSG00000214827") && (r.getStart() == 154292309 || r.getStart() == 154292241) ) {
System.out.println("It's something new here!");
}*/
if (featureIntervalMap.containsKey(featureName)) {
Collection<Interval> intervals = featureIntervalMap.get(featureName);
for (Interval interval: intervals ) {
if (newInterval.intersects(interval) || newInterval.abuts(interval)) {
if (newInterval.getStart() == interval.getStart() && newInterval.getEnd() == interval.getEnd()) {
// equal interval is already present for this feature
return;
}
newInterval = concatenateIntervals(newInterval, interval);
toRemove.add(interval);
}
}
intervals.removeAll(toRemove);
}
// If we removed intervals from featureIntervalMap, then need to update the intervalTree accordingly
for (Interval iv : toRemove) {
IntervalTree.Node<Set<Feature>> redundantInterval = intervalTree.find(iv.getStart(), iv.getEnd());
Set<Feature> features = redundantInterval.getValue();
removeFeatureByName(featureName, features);
if (features.isEmpty()) {
intervalTree.remove(iv.getStart(), iv.getEnd());
}
}
IntervalTree.Node<Set<Feature>> dublicateInterval = intervalTree.find(newInterval.getStart(), newInterval.getEnd());
if (dublicateInterval != null ) {
Set<Feature> intervalFeatures = dublicateInterval.getValue();
intervalFeatures.add(new Feature(featureName, featureStrand) );
} else {
Set<Feature> intervalFeatures = new HashSet<Feature>();
intervalFeatures.add(new Feature(featureName, featureStrand));
intervalTree.put(newInterval.getStart(), newInterval.getEnd(), intervalFeatures);
}
featureIntervalMap.put(featureName, newInterval);