Skip to content

Commit

Permalink
maybe workign overlapping scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
lbergelson committed Oct 2, 2023
1 parent 30cd32a commit dee9ce9
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 35 deletions.
5 changes: 3 additions & 2 deletions src/main/java/org/broad/igv/sam/AlignmentDataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public AlignmentInterval getLoadedInterval(ReferenceFrame frame) {
}

public AlignmentInterval getLoadedInterval(ReferenceFrame frame, boolean includeOverlaps) {
// Search for interval completely containining reference frame region
// Search for interval completely containing reference frame region
for (AlignmentInterval interval : intervalCache) {
if (interval.contains(frame.getCurrentRange())) {
return interval;
Expand Down Expand Up @@ -354,11 +354,12 @@ public void load(ReferenceFrame frame,

loadedInterval.packAlignments(renderOptions);

IGVEventBus.getInstance().post(new DataLoadedEvent(frame));
} finally {
currentlyLoading = null;
}

IGVEventBus.getInstance().post(new DataLoadedEvent(frame));


}

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/broad/igv/sam/AlignmentTrack.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ public class AlignmentTrack extends AbstractTrack implements IGVEventObserver {
public static void sortSelectedReadsToTheTop(final Set<String> selectedReadNames) {
//copy this in case it changes out from under us
Set<String> selectedReadNameCopy = new HashSet<>(selectedReadNames);
UIUtilities.invokeOnEventThread(() ->
IGV.getInstance().sortAlignmentTracks(SortOption.NONE, null, null, false, selectedReadNameCopy));
IGV.getInstance().sortAlignmentTracks(SortOption.NONE, null, null, false, selectedReadNameCopy);
}

public enum ColorOption {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected void paintComponent(final Graphics g) {
g.setColor(Color.LIGHT_GRAY);
elementsOnScreen.clear();
final int mid = getHeight() / 3;
Map<SupplementaryAlignment, AlignmentArrow> saInPositionOrder = drawAlignmentsInPositionOrder((Graphics2D) g.create(), toDraw, selected, getWidth(), mid);
Map<SupplementaryAlignment, AlignmentArrow> saInPositionOrder = drawAlignmentsInCondensedChromosomeOrder((Graphics2D) g.create(), toDraw, selected, getWidth(), mid);
drawArcs((Graphics2D) g.create(), toDraw, selected, mid, saInPositionOrder);
drawContigLabels((Graphics2D) g.create(), mid + 15, saInPositionOrder);
final int readOrderMidline = 2 * getHeight() / 3;
Expand Down Expand Up @@ -156,7 +156,7 @@ private Map<SupplementaryAlignment, AlignmentArrow> drawAlignmentsInReadOrder(fi

double lastPosition = scaledBorderGap;
for (SupplementaryAlignment sa : (Iterable<SupplementaryAlignment>) toDraw::iterateInReadOrder) {
final double spaceToUse = getSpaceToUse(totalAlignedBases, availableSpace, sa.getNumberOfAlignedBases());
final double spaceToUse = getSpaceToUse(availableSpace, sa.getNumberOfAlignedBases(), totalAlignedBases);
final int end = (int) (lastPosition + spaceToUse);
AlignmentArrow readArrow = new AlignmentArrow(midline, ALIGNMENT_HEIGHT, (int) lastPosition, end, sa.getStrand());
lastPosition = end + scaledAlignmentGap;
Expand All @@ -173,8 +173,8 @@ private static Graphics2D getSelectedGraphics(final Graphics2D g) {
selectedGraphics.setColor(SELECTED_COLOR);
return selectedGraphics;
}
private <T extends Locatable> Map<Locatable, List<T>> groupBySpanningInterval(List<T> intervals){

private static <T extends Locatable> Map<Locatable, List<T>> groupBySpanningInterval(List<T> intervals){
List<T> currentGroup = null;
Map<Locatable, List<T>> output = new LinkedHashMap<>();
Locatable spanning = null;
Expand All @@ -191,7 +191,9 @@ private <T extends Locatable> Map<Locatable, List<T>> groupBySpanningInterval(Li
spanning = new Interval(spanning.getContig(), Math.min(spanning.getStart(), loc.getStart()), Math.max(spanning.getEnd(), loc.getEnd()));
} else {
output.put(spanning, currentGroup);
currentGroup = null;
currentGroup = new ArrayList<>();
currentGroup.add(loc);
spanning = loc;
}
}
if( currentGroup != null) {
Expand All @@ -213,11 +215,43 @@ private static Map<SupplementaryAlignment, AlignmentArrow> drawAlignmentsInConde
final int scaledContigGap = scale(2, BETWEEN_CONTIG_GAP, width);
final int scaledBorderGap = scale(BORDER_GAP / 3, BORDER_GAP, width);

final var groupedBySpan = groupBySpanningInterval(toDraw.streamInPositionOrder().toList());
final Map<String, List<Locatable>> byContig = groupedBySpan.keySet()
.stream()
.collect(Collectors.groupingBy(Locatable::getContig));

//tihs should probably vary per contig instead of being uniform
final double perContigAvailableSpace = (width - (2 * scaledBorderGap + (contigs.size() -1) * scaledContigGap))/((double)contigs.size());

int contigStart = scaledBorderGap;
for(var contigEntry: byContig.entrySet()){
//find the available space for each contig and set the drawing head there
int contigEnd = (int)(contigStart + perContigAvailableSpace);
List<Locatable> distinctSpans = contigEntry.getValue();
//find the reference length of all the span groups on this contig
int totalSpansLength = distinctSpans.stream().mapToInt(Locatable::getLengthOnReference).sum();
int spanStart = contigStart;
for(Locatable span: distinctSpans){
//now handle each span group
int spanLength = span.getLengthOnReference();
int spanSpaceAvailable = (int)getSpaceToUse((double) perContigAvailableSpace - (distinctSpans.size() -1) * scaledAlignmentGap, spanLength, totalSpansLength );
for(SupplementaryAlignment sa: groupedBySpan.get(span)){
//each read in the span is placed relatively within the space
int scaledReadStart = (int)getSpaceToUse(spanSpaceAvailable, sa.getStart() - span.getStart(), spanLength);
int scaledReadEnd = (int)getSpaceToUse(spanSpaceAvailable, sa.getEnd() - span.getStart(), spanLength);
//arrows that overlap will appear overlapping one another
AlignmentArrow readArrow = new AlignmentArrow(midline, ALIGNMENT_HEIGHT,
spanStart + scaledReadStart,
spanStart + scaledReadEnd,
sa.getStrand());
positions.put(sa, readArrow);
spanStart += spanSpaceAvailable + scaledAlignmentGap;
}

final double availableSpace = width - (2 * scaledBorderGap + (toDraw.size() - 1) * scaledAlignmentGap + (contigs.size() - 1) * scaledContigGap);

String lastContig = contigs.get(0);
double lastPosition = scaledBorderGap;
}
//move contig start forward
contigStart = contigEnd + scaledContigGap;
}

// | ... [ ]>-5kbp<[ ] ... [ ]> | |[ |> ... |
// Contigs all the same scale?
Expand All @@ -228,25 +262,6 @@ private static Map<SupplementaryAlignment, AlignmentArrow> drawAlignmentsInConde
// merge overlappers into single zones
// discover close by / far away zones




for (SupplementaryAlignment sa : (Iterable<SupplementaryAlignment>) toDraw::iterateInPositionOrder) {

final double spaceToUse = getSpaceToUse(totalAlignedBases, availableSpace, sa.getLengthOnReference());
final String newContig = sa.getContig();
if (lastPosition != scaledBorderGap && !Objects.equals(lastContig, newContig)) {
lastPosition += scaledContigGap;
}

lastContig = newContig;
final int end = (int) (lastPosition + spaceToUse);

AlignmentArrow readArrow = new AlignmentArrow(midline, ALIGNMENT_HEIGHT, (int) lastPosition, end, sa.getStrand());
positions.put(sa, readArrow);
lastPosition = end + scaledAlignmentGap;
}

drawArrows(g, selected, positions, toDraw.getPrimaryAlignment());
return positions;
}
Expand All @@ -267,7 +282,7 @@ private static Map<SupplementaryAlignment, AlignmentArrow> drawAlignmentsInPosit
double lastPosition = scaledBorderGap;
for (SupplementaryAlignment sa : (Iterable<SupplementaryAlignment>) toDraw::iterateInPositionOrder) {

final double spaceToUse = getSpaceToUse(totalAlignedBases, availableSpace, sa.getLengthOnReference());
final double spaceToUse = getSpaceToUse(availableSpace, sa.getLengthOnReference(), totalAlignedBases);
final String newContig = sa.getContig();
if (lastPosition != scaledBorderGap && !Objects.equals(lastContig, newContig)) {
lastPosition += scaledContigGap;
Expand Down Expand Up @@ -320,7 +335,7 @@ private static void drawArrows(final Graphics2D g, final Set<SupplementaryAlignm
}
}

private static double getSpaceToUse(final int totalAlignedBases, final double availableSpace, final int numberOfBasePairs) {
private static double getSpaceToUse(final double availableSpace, final int numberOfBasePairs, final int totalAlignedBases) {
final double fractionOfWhole = (double) numberOfBasePairs / totalAlignedBases;
return fractionOfWhole * availableSpace;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/broad/igv/util/index/Interval.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@


// Quick and dirty interval class
public class Interval implements Comparable {
public class /**/Interval implements Comparable {

final int low;
final int high;
Expand Down

0 comments on commit dee9ce9

Please sign in to comment.