Skip to content

Commit

Permalink
unfinished work the alignment weaver
Browse files Browse the repository at this point in the history
  • Loading branch information
shenkers committed Feb 6, 2015
1 parent 902279c commit 8a0abd2
Show file tree
Hide file tree
Showing 2 changed files with 313 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.mskcc.shenkers.control.alignment;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import javafx.beans.binding.DoubleBinding;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.scene.shape.CubicCurveTo;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.PathElement;
import javafx.util.Pair;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
*
* @author Soma
*/
public class CurvedOverlayPath {

Logger logger = LogManager.getLogger();

Path p;

private DoubleProperty yScale;
private DoubleProperty xScale;

private List<Pair<DoubleProperty, DoubleProperty>> relativeXCoords;
private List<Pair<DoubleProperty, DoubleProperty>> relativeYCoords;

List<Pair<DoubleBinding, DoubleBinding>> componentXCoords;
List<Pair<DoubleBinding, DoubleBinding>> componentYCoords;

private List<DoubleProperty> genomeFlipped;

List<PathElement> pathElements;

/**
* @return the yScale
*/
public DoubleProperty getYScale() {
return yScale;
}

/**
* @param yScale the yScale to set
*/
public void setYScale(DoubleProperty yScale) {
this.yScale = yScale;
}

/**
* @return the xScale
*/
public DoubleProperty getXScale() {
return xScale;
}

/**
* @return the relativeXCoords
*/
public List<Pair<DoubleProperty, DoubleProperty>> getRelativeXCoords() {
return relativeXCoords;
}

/**
* @return the relativeYCoords
*/
public List<Pair<DoubleProperty, DoubleProperty>> getRelativeYCoords() {
return relativeYCoords;
}

/**
* @return the genomeFlipped
*/
public List<DoubleProperty> getGenomeFlipped() {
return genomeFlipped;
}

enum PathCase {

first, middle, l_even, l_odd, r_even, r_odd, last;

};

public CurvedOverlayPath(int nGenomes) {

yScale = new SimpleDoubleProperty(1);
xScale = new SimpleDoubleProperty(1);

relativeXCoords = new ArrayList<>();
relativeYCoords = new ArrayList<>();
componentXCoords = new ArrayList<>();
componentYCoords = new ArrayList<>();

for (int i = 0; i < nGenomes; i++) {
relativeXCoords.add(new Pair<>(new SimpleDoubleProperty(), new SimpleDoubleProperty()));
relativeYCoords.add(new Pair<>(new SimpleDoubleProperty(), new SimpleDoubleProperty()));
componentXCoords.add(new Pair<>(getRelativeXCoords().get(i).getKey().multiply(getXScale()), getRelativeXCoords().get(i).getValue().multiply(getXScale())));
componentYCoords.add(new Pair<>(getRelativeYCoords().get(i).getKey().multiply(getYScale()), getRelativeYCoords().get(i).getValue().multiply(getYScale())));
}

pathElements = new ArrayList<>();

// pathElements.add(new MoveTo());
for (int i = 0; i < nGenomes * 4; i++) {
Function<Integer, PathCase> f = (j -> {
if (j == 0) {
return PathCase.first;
} else if (j + 1 == nGenomes * 4) {
return PathCase.last;
} else if (j + 1 == nGenomes * 2) {
return PathCase.middle;
} else if (j % 2 == 1) {
if (j < nGenomes * 2) {
return PathCase.l_odd;
} else {
return PathCase.r_odd;
}
} else {
if (j < nGenomes * 2) {
return PathCase.l_even;
} else {
return PathCase.r_even;
}
}
});

logger.info("i {}", i);

// get the index of the genome
int g = i < nGenomes * 2 ? i / 2 : ((4 * nGenomes - i - 1) / 2);
logger.info("g {}", g);

Pair<DoubleBinding, DoubleBinding> xCoord = componentXCoords.get(g);
Pair<DoubleBinding, DoubleBinding> yCoord = componentYCoords.get(g);

PathCase pc = f.apply(i);
switch (pc) {
case first: {
MoveTo moveTo = new MoveTo();
LineTo lineTo = new LineTo();

moveTo.xProperty().bind(xCoord.getKey());
moveTo.yProperty().bind(yCoord.getKey());

lineTo.xProperty().bind(xCoord.getKey());
lineTo.yProperty().bind(yCoord.getValue());

pathElements.add(moveTo);
pathElements.add(lineTo);
break;
}
case middle: {
LineTo lineTo = new LineTo();
lineTo.xProperty().bind(xCoord.getValue());
lineTo.yProperty().bind(yCoord.getValue());
pathElements.add(lineTo);
break;
}
case last: {
LineTo lineTo = new LineTo();
lineTo.xProperty().bind(xCoord.getKey());
lineTo.yProperty().bind(yCoord.getKey());
pathElements.add(lineTo);
break;
}
case l_odd: {
CubicCurveTo curveTo = new CubicCurveTo();

Pair<DoubleBinding, DoubleBinding> xCoordNext = componentXCoords.get(g + 1);
Pair<DoubleBinding, DoubleBinding> yCoordNext = componentYCoords.get(g + 1);

DoubleBinding controlY = yCoord.getValue().add(yCoordNext.getKey()).divide(2.);

curveTo.controlX1Property().bind(xCoord.getKey());
curveTo.controlY1Property().bind(controlY);

curveTo.controlX2Property().bind(xCoordNext.getKey());
curveTo.controlY2Property().bind(controlY);

curveTo.xProperty().bind(xCoordNext.getKey());
curveTo.yProperty().bind(yCoordNext.getKey());

pathElements.add(curveTo);
break;
}
case l_even: {
LineTo lineTo = new LineTo();
lineTo.xProperty().bind(xCoord.getKey());
lineTo.yProperty().bind(yCoord.getValue());
pathElements.add(lineTo);
break;
}
case r_odd: {
CubicCurveTo curveTo = new CubicCurveTo();

Pair<DoubleBinding, DoubleBinding> xCoordNext = componentXCoords.get(g - 1);
Pair<DoubleBinding, DoubleBinding> yCoordNext = componentYCoords.get(g - 1);

DoubleBinding controlY = yCoord.getKey().add(yCoordNext.getValue()).divide(2.);

curveTo.controlX1Property().bind(xCoord.getValue());
curveTo.controlY1Property().bind(controlY);

curveTo.controlX2Property().bind(xCoordNext.getValue());
curveTo.controlY2Property().bind(controlY);

curveTo.xProperty().bind(xCoordNext.getValue());
curveTo.yProperty().bind(yCoordNext.getValue());

pathElements.add(curveTo);
break;
}
case r_even: {
LineTo lineTo = new LineTo();
lineTo.xProperty().bind(xCoord.getValue());
lineTo.yProperty().bind(yCoord.getKey());
pathElements.add(lineTo);
break;
}
default: {
throw new RuntimeException("should not get here");
}
}
}

p = new Path(pathElements);

}

public Path getPath() {
return p;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@

import htsjdk.samtools.util.Interval;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javafx.util.Pair;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.mskcc.shenkers.control.alignment.AlignmentSource;
import org.mskcc.shenkers.control.alignment.AlignmentWeaver;
import org.mskcc.shenkers.control.alignment.CurvedOverlayPath;
import org.mskcc.shenkers.control.alignment.LocalAlignment;
import org.mskcc.shenkers.control.alignment.NucleotideMapping;
import org.mskcc.shenkers.model.datatypes.Genome;
Expand All @@ -33,8 +38,6 @@ public class ChainAlignmentOverlay {

Map<Pair<Genome, Genome>, AlignmentSource> alignmentSources;



private AlignmentWeaver initializeWeaver(Pair<Genome, Genome> genomePair, Map<Genome, GenomeSpan> displayedSpans) {
Genome fromGenome = genomePair.getKey();
Genome toGenome = genomePair.getValue();
Expand Down Expand Up @@ -108,29 +111,74 @@ public void weave(Map<Genome, GenomeSpan> displayedSpans) {
}
}
}

Map<Genome, List<Integer>> order = weaver.getOrder();

int I = 0;

int inc = 3;

boolean hasSome = true;
while (hasSome) {
hasSome = false;
List<List<Integer>> popped = new ArrayList<List<Integer>>();
Iterator<List<AlignmentWeaver.Pos>> it = ali.iterator();
while (it.hasNext()) {
List<AlignmentWeaver.Pos> l = it.next();
List<AlignmentWeaver.Pos> p = new ArrayList<AlignmentWeaver.Pos>();
popped.add(p);
while (l.size() > 0 && l.get(0).order < I + inc) {
p.add(l.remove(0));
Integer max = order.values().stream().flatMap(o -> o.stream()).max((x, y) -> x - y).get();

List<Map<Genome, Pair<Double, Double>>> relativeCoordinates = new ArrayList<>();
Map<Genome, Pair<Integer, Integer>> previous = displayedSpans.keySet().stream().collect(Collectors.toMap(g -> g, g -> new Pair<Integer, Integer>(0, 0)));
for (int i = 0; i <= max; i += inc) {
Map<Genome, Pair<Integer, Integer>> extremeAbsoluteX = new HashMap<>();

for (Genome g : displayedSpans.keySet()) {
List<Integer> gOrder = order.get(g);

List<Integer> indices = new ArrayList<>();
for (int j = i; j < i + inc; j++) {
int index = gOrder.indexOf(j);
if (index != -1) {
indices.add(index);
}
}
Pair<Integer, Integer> next = null;
if (indices.size() > 0) {
next = new Pair<>(Collections.min(indices), Collections.max(indices));
} else {
next = previous.get(g);
}
extremeAbsoluteX.put(g, next);
}
for (int j = 0; j < numRows; j++) {
if (alignment[j].charAt(i) != '-') {
endIndex[j]++;
}
hasSome |= l.size() > 0;
}
I += inc;
System.out.println(popped);

if ((i + 1) % b == 0) {

BoundPoly4 bp4 = new AlignmentOverlayNGTest.BoundPoly4(3);
List<Double> xCoords = new ArrayList<>();
for (int j = 0; j < numRows; j++) {
// System.out.println(Arrays.asList(startIndex[j], endIndex[j]));
System.out.println(Arrays.asList(startIndex[j] * 1. / nBasesPerRow[j], endIndex[j] * 1. / nBasesPerRow[j]));
xCoords.add(startIndex[j] * 1. / nBasesPerRow[j]);
bp4.relativeXCoords.get(j).getKey().setValue(startIndex[j] * 1. / nBasesPerRow[j]);
startIndex[j] = endIndex[j];

}
relativeXCoords.add(null)
}

boolean hasSome = true;
while (hasSome) {
hasSome = false;
List<List<Integer>> popped = new ArrayList<List<Integer>>();
Iterator<List<AlignmentWeaver.Pos>> it = ali.iterator();
while (it.hasNext()) {
List<AlignmentWeaver.Pos> l = it.next();
List<AlignmentWeaver.Pos> p = new ArrayList<AlignmentWeaver.Pos>();
popped.add(p);
while (l.size() > 0 && l.get(0).order < I + inc) {
p.add(l.remove(0));
}
hasSome |= l.size() > 0;
}
I += inc;
System.out.println(popped);
}
}
}

}
}

0 comments on commit 8a0abd2

Please sign in to comment.