1. petermr
  2. diagramanalyzer

Commits

petermr  committed f2015de

tested phylo examples, especially finding left-oriented trees

  • Participants
  • Parent commits 2c47a21
  • Branches default

Comments (0)

Files changed (10)

File src/main/java/org/xmlcml/diagrams/DiagramAnalyzer.java

View file
 		imageProcessor.setDefaults();
 	}
 
-	/** creates a simpe XML tree of graph.
+	/** creates a simple XML tree of graph.
 	 * 
 	 * Probably only useful if graph is a tree (maybe separate this later)
 	 * 

File src/main/java/org/xmlcml/diagrams/phylo/PhyloTreeAnalyzer.java

View file
 import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 import org.apache.commons.io.FileUtils;
 import org.apache.log4j.Logger;
 import org.xmlcml.diagrams.DiagramAnalyzer;
+import org.xmlcml.euclid.Int2;
 import org.xmlcml.image.ArgIterator;
 import org.xmlcml.image.ImageProcessor;
 import org.xmlcml.image.pixel.PixelEdge;
 
 	private final static Logger LOG = Logger.getLogger(PhyloTreeAnalyzer.class);
 
-	// public final static String NEWICK = "-n";
+	public final static String LENGTH1 = "--lengths";
 	public final static String NEWICK1 = "--newick";
-
-	// public final static String NEWICK = "-n";
 	public final static String ROOT1 = "--root";
 
 	private File newickFile;
 
 	private String newickString;
 	private List<PixelGraph> pixelGraphList;
-	private ComparatorType rootPosition;
+	private ComparatorType rootPosition = ComparatorType.LEFT;
+	private boolean computeLengths;
 
 	public PhyloTreeAnalyzer() {
 		setDefaults();
 		List<PixelNode> descendants = new ArrayList<PixelNode>();
 		for (PixelEdge edge : edges) {
 			PixelNode other = edge.getOtherNode(pixelNode);
-			if (other != null && !other.equals(parent)
-					&& !usedPixelNodes.contains(other)) {
-				usedPixelNodes.add(other);
-				descendants.add(other);
+			if (other != null) {
+				if (other.equals(parent)) {
+					
+				} else if (!usedPixelNodes.contains(other)) {
+					usedPixelNodes.add(other);
+					descendants.add(other);
+				}
+			} else {
+				LOG.debug("other node is null");
 			}
 		}
 		if (descendants.size() > 1) {
 			sb.append("(");
 		}
-		createNewickTreeFromDescendants(pixelNode, sb, descendants);
+		createNewickTreeFromDescendants(pixelNode, parent, sb, descendants);
+		Double distance = getDistance(pixelNode, parent);
 		if (descendants.size() > 1) {
+			appendDistance(sb, distance);
 			sb.append(")");
 		}
 	}
 
-	private void createNewickTreeFromDescendants(PixelNode pixelNode,
+	private Double getDistance(PixelNode pixelNode, PixelNode parent) {
+		Double distance = null;
+		if (parent != null && computeLengths) {
+			Int2 pixel2 = pixelNode.getCentrePixel().getInt2();
+			Int2 parent2 = parent.getCentrePixel().getInt2();
+			if (rootPosition == null || rootPosition.equals(ComparatorType.LEFT) || rootPosition.equals(ComparatorType.RIGHT)) {
+				distance = (double) Math.abs(pixel2.getX() - parent2.getX());
+			} else if (rootPosition.equals(ComparatorType.TOP) || rootPosition.equals(ComparatorType.BOTTOM)) {
+				distance = (double) Math.abs(pixel2.getY() - parent2.getY());
+			}
+			distance *= 0.001;
+		}
+		return distance;
+	}
+
+	private void createNewickTreeFromDescendants(PixelNode pixelNode, PixelNode parent,
 			StringBuilder sb, List<PixelNode> descendants) {
 		for (int i = 0; i < descendants.size(); i++) {
 			String label = descendants.get(i).getLabel();
 				sb.append(label);
 			}
 			createNewickTreeFromDescendants(descendants.get(i), pixelNode, sb);
+			Double distance = getDistance(pixelNode, parent);
 			if (i < descendants.size() - 1) {
+				appendDistance(sb, distance);
 				sb.append(",");
 			}
 		}
 	}
 
+	private void appendDistance(StringBuilder sb, Double distance) {
+		if (distance != null) {
+			sb.append(":"+distance);
+		}
+	}
+
 	protected void usage() {
 		System.err.println("Usage: phylotree [args]");
 		super.usage();
 			debug = true;
 			argIterator.setDebug(true);
 			argIterator.next();
-		} else if (/*
-					 * arg.equals(PhyloTreeAnalyzer.NEWICK) ||
-					 */arg.equals(PhyloTreeAnalyzer.NEWICK1)) {
+		} else if (arg.equals(PhyloTreeAnalyzer.NEWICK1)) {
 			String value = argIterator.getSingleValue();
 			if (value != null) {
 				setNewickFile(new File(value));
 			}
-		} else if (/*
-					 * arg.equals(PhyloTreeAnalyzer.ROOT) ||
-					 */arg.equals(PhyloTreeAnalyzer.ROOT1)) {
+		} else if (arg.equals(PhyloTreeAnalyzer.LENGTH1)) {
+			argIterator.getValues();
+			setComputeLengths(true);
+		} else if (arg.equals(PhyloTreeAnalyzer.ROOT1)) {
 			String value = argIterator.getSingleValue();
 			if (value != null) {
 				setRoot(value.toUpperCase());
 		return found;
 	}
 
+	private void setComputeLengths(boolean b) {
+		this.computeLengths = b;
+	}
+
 	private void setRoot(String root) {
 		ComparatorType[] types = { ComparatorType.BOTTOM, ComparatorType.LEFT,
 				ComparatorType.TOP, ComparatorType.RIGHT, };
 
 	public void debug() {
 		if (newickFile != null) {
-			System.err.println("newick output: " + newickFile);
+			LOG.debug("newick output: " + newickFile);
 		}
 	}
 
 	protected boolean runCommands() {
 		super.processImageFile();
 		this.processGraph();
-		int island = ensurePixelProcessor().getIsland();
-		if (newickFile != null && island >= 0) {
-			PixelGraph graph = pixelGraphList.get(island);
+		int selectedIsland = ensurePixelProcessor().getSelectedIsland();
+		if (newickFile != null && selectedIsland >= 0) {
+			PixelGraph graph = pixelGraphList.get(selectedIsland);
 			LOG.debug("edges "+graph.getEdges().size()+" nodes "+graph.getNodes().size()+" "+graph.toString());
 			for (PixelEdge edge : graph.getEdges()) {
 				LOG.trace(edge.getPixelNodes().size());
 			}
 			if (rootPosition != null) {
-				PixelNode rootNode = graph
-						.getPossibleRootPixelNode(rootPosition);
-				newickString = createNewick(graph, rootNode);
+				PixelNode rootNode = null;
+				List<PixelNode> rootNodes = graph.getPossibleRootNodes1();
+				Collections.sort(rootNodes);
+				if (rootNodes.size() > 0) {
+					rootNode = rootNodes.get(0);
+					LOG.debug("found root: "+rootNode);
+				} else {
+					rootNode = graph.getRootPixelNodeFromExtremeEdge(rootPosition);
+				}
+				if (rootNode != null) {
+					newickString = createNewick(graph, rootNode);
+				}
 			} else {
 				newickString = createNewick(graph);
 			}
 
 	public void processGraph() {
 		try {
-			PixelIslandList pixelIslandList = imageProcessor
-					.getOrCreatePixelIslandList();
+			PixelIslandList pixelIslandList = imageProcessor.getOrCreatePixelIslandList();
 			pixelGraphList = pixelIslandList.createGraphList();
 		} catch (IOException e) {
 			throw new RuntimeException("pixelGraphList failed", e);

File src/main/java/org/xmlcml/diagrams/plot/PlotAnalyzer.java

View file
 	public boolean runCommands() {
 		super.processImageFile();
 		this.processGraph();
-		int island = ensurePixelProcessor().getIsland() ;
+		int island = ensurePixelProcessor().getSelectedIsland() ;
 		if (csvFile != null && island >= 0) {
 //			PixelGraph graph = pixelGraphList.get(island);
 //			newickString = createNewick(graph);

File src/test/java/org/xmlcml/diagrams/misc/MiscTest.java

View file
 import org.junit.Test;
 import org.xmlcml.diagrams.Fixtures;
 import org.xmlcml.diagrams.phylo.PhyloTreeAnalyzer;
-import org.xmlcml.diagrams.phylo.PhyloTreeTest;
+import org.xmlcml.diagrams.phylo.PhyloTreeAnalyzerTest;
 
 public class MiscTest {
 

File src/test/java/org/xmlcml/diagrams/molecules/MoleculeTest.java

View file
 import org.junit.Before;
 import org.junit.Test;
 import org.xmlcml.diagrams.Fixtures;
-import org.xmlcml.diagrams.phylo.PhyloTreeTest;
+import org.xmlcml.diagrams.phylo.PhyloTreeAnalyzerTest;
 import org.xmlcml.graphics.svg.SVGG;
 import org.xmlcml.graphics.svg.SVGSVG;
 import org.xmlcml.image.ImageProcessor;
 			PixelGraph graph = PixelGraph.createGraph(island);
 			graph.createAndDrawGraph(g);
 		}
-		PhyloTreeTest.extractAndDrawChars(inputFile, pixelIslandList, g, 2);
+		PhyloTreeAnalyzerTest.extractAndDrawChars(inputFile, pixelIslandList, g, 2);
 		SVGSVG.wrapAndWriteAsSVG(g, new File("target/molecules/postermol.graphAndChars.svg"));
 
 	}
 			PixelGraph graph = PixelGraph.createGraph(island);
 			graph.createAndDrawGraph(g);
 		}
-		PhyloTreeTest.extractAndDrawChars(inputFile, pixelIslandList, g, 2);
+		PhyloTreeAnalyzerTest.extractAndDrawChars(inputFile, pixelIslandList, g, 2);
 		SVGSVG.wrapAndWriteAsSVG(g, new File("target/molecules/natprod1.graphAndChars.svg"));
 
 	}

File src/test/java/org/xmlcml/diagrams/phylo/MiscPhyloTest.java

View file
 	@Test
 	// BEETLE pics
 	public void test16662GraphsAndTextThreshold() throws IOException {
-		PhyloTreeTest.createGraphsCharsAndPlot(Fixtures.ROSS_DIR, "pone.", "0016662.g001", 2,
+		PhyloTreeAnalyzerTest.createGraphsCharsAndPlot(Fixtures.ROSS_DIR, "pone.", "0016662.g001", 2,
 				180 /* 220 */);
 	}
 
 	@Test
 	// some italic
 	public void test17932GraphsAndText() throws IOException {
-		PhyloTreeTest.createGraphsCharsAndPlot(Fixtures.ROSS_DIR, "pone.", "0017932.g051", 2,
+		PhyloTreeAnalyzerTest.createGraphsCharsAndPlot(Fixtures.ROSS_DIR, "pone.", "0017932.g051", 2,
 				180);
 	}
 
 	@Test
 	// tree and madagascar
 	public void test2718GraphsAndText() throws IOException {
-		PhyloTreeTest.createGraphsCharsAndPlot(Fixtures.MISC_DIR, "",
+		PhyloTreeAnalyzerTest.createGraphsCharsAndPlot(Fixtures.MISC_DIR, "",
 				"14153252278_41a2718f1c_o", 2, 240);
 	}
 
 	@Test
 	public void testMadagascarGraphsAndText() throws IOException {
-		PhyloTreeTest.createGraphsCharsAndPlot(Fixtures.MISC_DIR, "", "madagascar", 2, 240);
+		PhyloTreeAnalyzerTest.createGraphsCharsAndPlot(Fixtures.MISC_DIR, "", "madagascar", 2, 240);
 	}
 
 	@Test
 	// some italic + simple tree
 	public void test17170GraphsAndText() throws IOException {
-		PhyloTreeTest.createGraphsCharsAndPlot(Fixtures.ROSS_DIR, "pone.", "0017170.g002",
+		PhyloTreeAnalyzerTest.createGraphsCharsAndPlot(Fixtures.ROSS_DIR, "pone.", "0017170.g002",
 				30, 180);
 	}
 
 
 	@Test
 	public void test16923_003Tree() throws IOException {
-		PhyloTreeTest.createGraphsCharsAndPlot(Fixtures.ROSS_DIR, "pone.", "0016923.g002", 2,
+		PhyloTreeAnalyzerTest.createGraphsCharsAndPlot(Fixtures.ROSS_DIR, "pone.", "0016923.g002", 2,
 				180);
 	}
 
 	@Test
 	// pastel blocks
 	public void test22249Tree() throws IOException {
-		PhyloTreeTest.createGraphsCharsAndPlot(Fixtures.ROSS_DIR, "pone.", "0022249.g003", 2,
+		PhyloTreeAnalyzerTest.createGraphsCharsAndPlot(Fixtures.ROSS_DIR, "pone.", "0022249.g003", 2,
 				100);
 	}
 
 	@Test
 	// serif
 	public void test18360Tree() throws IOException {
-		PhyloTreeTest.createGraphsCharsAndPlot(Fixtures.ROSS_DIR, "pone.", "0018360.g001", 2,
+		PhyloTreeAnalyzerTest.createGraphsCharsAndPlot(Fixtures.ROSS_DIR, "pone.", "0018360.g001", 2,
 				240);
 	}
 
 	@Test
 	// gray and dark gray areas
 	public void test63008Tree() throws IOException {
-		PhyloTreeTest.createGraphsCharsAndPlot(Fixtures.ROSS_DIR, "pone.", "0063008.g001", 2,
+		PhyloTreeAnalyzerTest.createGraphsCharsAndPlot(Fixtures.ROSS_DIR, "pone.", "0063008.g001", 2,
 				30);
 	}
 
 	@Test
 	public void test86675Tree() throws IOException {
-		PhyloTreeTest.createGraphsCharsAndPlot(Fixtures.ROSS_DIR, "pone.", "0086675.g002", 2,
+		PhyloTreeAnalyzerTest.createGraphsCharsAndPlot(Fixtures.ROSS_DIR, "pone.", "0086675.g002", 2,
 				180);
 	}
 

File src/test/java/org/xmlcml/diagrams/plot/PlotTest.java

View file
 				createDefaultProcessorAndProcess(infile).getOrCreatePixelIslandList();
 		List<PixelRingList> pixelRingListList = plot.createRingListList();
 //		Assert.assertEquals("characters", 4, plot.size());
-		drawRings(pixelRingListList, new File("target/plot/ynumbers.svg"));
+//		drawRings(pixelRingListList, new File("target/plot/ynumbers.svg"));
+		drawRings(pixelRingListList, outfile2);
 	}
 
 	private static void assertSizes(PixelRingList ringList, int[] sizes) {

File src/test/java/org/xmlcml/diagrams/table/TableTest.java

View file
 
 import org.junit.Test;
 import org.xmlcml.diagrams.Fixtures;
-import org.xmlcml.diagrams.phylo.PhyloTreeTest;
+import org.xmlcml.diagrams.phylo.PhyloTreeAnalyzerTest;
 
 public class TableTest {
 
 	@Test
 	// ocr text
 	public void testAR01_12GraphsAndText() throws IOException {
-		PhyloTreeTest.createGraphsCharsAndPlot(Fixtures.MISC_DIR, "", "AR01_12", 2, 128);
+		PhyloTreeAnalyzerTest.createGraphsCharsAndPlot(Fixtures.MISC_DIR, "", "AR01_12", 2, 128);
 	}
 
 	@Test
 	public void testBP2012() throws IOException {
-		PhyloTreeTest.createGraphsCharsAndPlot(Fixtures.MISC_DIR, "", "BP2012", 2,
+		PhyloTreeAnalyzerTest.createGraphsCharsAndPlot(Fixtures.MISC_DIR, "", "BP2012", 2,
 				180);
 	}
 

File src/test/resources/org/xmlcml/diagrams/misc/small.png

Added
New image

File src/test/resources/org/xmlcml/diagrams/misc/small1.png

Added
New image