Commits

Anonymous committed 150e880

added Newick to commandline and tested

Comments (0)

Files changed (3)

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

 
 import org.apache.commons.io.FilenameUtils;
 import org.apache.log4j.Logger;
+import org.xmlcml.image.ArgIterator;
 import org.xmlcml.image.ImageProcessor;
 import org.xmlcml.image.pixel.PixelGraph;
 import org.xmlcml.image.pixel.PixelIslandList;
 
 	private final static Logger LOG = Logger.getLogger(DiagramAnalyzer.class);
 	
-//	public static final String BINARIZE = "-b";
-//	public static final String BINARIZE1 = "--binarize";
-//	public static final String DEBUG = "-d";
-//	public static final String DEBUG1 = "--debug";
-//	public static final String INPUT = "-i";
-//	public static final String INPUT1 = "--input";
-//	public static final String OUTPUT = "-o";
-//	public static final String OUTPUT1 = "--output";
-//	public static final String THRESH = "-t";
-//	public static final String THRESH1 = "--threshold";
-
 	private static final String TARGET = "target/";
 
 	protected boolean debug;
-
 	protected ImageProcessor imageProcessor;
 	
 	public DiagramAnalyzer() {
 		imageProcessor.usage();
 	}
 
-	
-	protected int parseArg(int iarg, String[] args) {
-		ensureImageProcessor();
-		if (false) {
-			
-		} else if (args[iarg].equals(ImageProcessor.DEBUG) || args[iarg].equals(ImageProcessor.DEBUG1)) {
-			debug = true;
-			imageProcessor.setDebug(true);
-			iarg++;
-//		} else if (args[iarg].equals(BINARIZE) || args[iarg].equals(BINARIZE1)) {
-//			imageProcessor.setBinarize(true);
-//			iarg++;
-		} else if (args[iarg].equals(ImageProcessor.INPUT) || args[iarg].equals(ImageProcessor.INPUT1)) {
-			ImageProcessor.checkHasMoreArgs(iarg++, args);
-			setInputFile(new File(args[iarg++]));
-//		} else if (args[iarg].equals(MAXISLAND) || args[iarg].equals(MAXISLAND1)) {
-//			checkHasMoreArgs(iarg++, args);
-//			getPixelProcessor().setMaxIsland(Integer.parseInt(args[iarg++]));
-		} else if (args[iarg].equals(ImageProcessor.OUTPUT) || args[iarg].equals(ImageProcessor.OUTPUT1)) {
-			ImageProcessor.checkHasMoreArgs(iarg++, args);
-			setOutputDir(new File(args[iarg++]));
-		} else if (args[iarg].equals(ImageProcessor.THRESH) || args[iarg].equals(ImageProcessor.THRESH1)) {
-			ImageProcessor.checkHasMoreArgs(iarg++, args);
-			imageProcessor.setThreshold(Integer.parseInt(args[iarg++]));
-		} else {
-			iarg = imageProcessor.parseArg(iarg, args);
-		}
-		return iarg;
-	}
-	
-
-	private void ensureImageProcessor() {
+	protected void ensureImageProcessor() {
 		if (imageProcessor == null) {
 			this.imageProcessor = new ImageProcessor();
 		}
 		imageProcessor.debug();
 	}
 
-	public void checkMandatory() {
-//		if (inputFile == null) {
-//			throw new RuntimeException("must give input file");
-//		}
+	protected boolean parseArgs(ArgIterator argIterator) {
+		boolean found = true;
+		if (argIterator.size() == 0) {
+			usage();
+		} else {
+			while (argIterator.hasNext()) {
+				if (debug) {
+					LOG.debug(argIterator.getCurrent());
+				}
+				found &= parseArgAndAdvance(argIterator);
+			}
+		}
+		if (debug) {
+			this.debug();
+		}
+		return found;
+	}
+	
+	protected boolean parseArgAndAdvance(ArgIterator argIterator) {
+		throw new RuntimeException("Must override parseArgAndAdvance");
+	}
+
+	protected boolean runCommands() {
+		throw new RuntimeException("Must override runCommands");
 	}
 
 	public void setInputFile(File file) {
 		imageProcessor.processImageFile();
 	}
 
-
+	protected void parseArgs(String[] args) {
+		this.parseArgs(new ArgIterator(args));
+	}
+	
+	protected void parseArgsAndRun(String[] args) {
+		this.parseArgs(new ArgIterator(args));
+		this.runCommands();
+	}
 
 }

src/main/java/org/xmlcml/diagrams/PhyloTree.java

 package org.xmlcml.diagrams;
 
 import java.io.File;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 
+import nu.xom.Element;
+
 import org.apache.commons.io.FileUtils;
 import org.apache.log4j.Logger;
+import org.xmlcml.image.ArgIterator;
 import org.xmlcml.image.ImageProcessor;
 import org.xmlcml.image.pixel.PixelEdge;
 import org.xmlcml.image.pixel.PixelGraph;
 import org.xmlcml.image.pixel.PixelIslandList;
 import org.xmlcml.image.pixel.PixelNode;
+import org.xmlcml.xml.XMLUtil;
 
 /**
  * phylogenetic tree from edges and nodes.
 
 	private String newickString;
 
+	private List<PixelGraph> pixelGraphList;
+
 	public PhyloTree() {
 		setDefaults();
 	}
 		return newickString;
 	}
 	
-	public void writeNewick() throws IOException {
+	public String createNewick(PixelGraph pixelGraph) {
+		List<PixelNode> rootNodes = pixelGraph.getPossibleRootNodes1();
+		String newick = null;
+		if (rootNodes.size() == 1) {
+			newick = this.createNewick(pixelGraph, rootNodes.get(0));
+		}
+		return newick;
+	}
+	
+	public Element createXML(PixelGraph pixelGraph) {
+		List<PixelNode> rootNodes = pixelGraph.getPossibleRootNodes1();
+		Element xml = null;
+		if (rootNodes.size() == 1) {
+			xml = this.createXML(pixelGraph, rootNodes.get(0));
+		}
+		return xml;
+	}
+	
+	public void writeNewickQuietly() {
 		if (newickString != null && newickFile != null) {
-			FileUtils.writeStringToFile(newickFile, newickString);
+			try {
+				FileUtils.writeStringToFile(newickFile, newickString);
+			} catch (Exception e) {
+				throw new RuntimeException("Cannot write Newick: "+newickFile, e);
+			}
 		}
 	}
 
 	}
 
 	public static void main(String[] args) throws Exception {
-		PhyloTree phyloTree = new PhyloTree();
-		phyloTree.processArgsAndRun(args);
+		DiagramAnalyzer phyloTree = new PhyloTree();
+		phyloTree.parseArgs(args);
 	}
 
-	private void processArgsAndRun(String[] args) throws IOException {
-		if (args.length == 0) {
-			this.usage();
+	protected boolean parseArgAndAdvance(ArgIterator argIterator) {
+		boolean found = true;
+		ensureImageProcessor();
+		String arg = argIterator.getCurrent();
+		if (debug) {
+			LOG.debug(arg);
+		}
+		if (false) {
+			
+		} else if (arg.equals(ImageProcessor.DEBUG) || arg.equals(ImageProcessor.DEBUG1)) {
+			debug = true;
+			argIterator.setDebug(true);
+			argIterator.next();
+		} else if (arg.equals(PhyloTree.NEWICK) || arg.equals(PhyloTree.NEWICK1)) {
+			String value = argIterator.getSingleValue();
+			if (value != null) {
+				setNewickFile(new File(value));
+			}
 		} else {
-			this.parseArgs(0, args);
-			this.runArgs();
-		}
-	}
-
-	protected int parseArgs(int iarg, String[] args) {
-		for (iarg = 0; iarg < args.length;) {
-
-			LOG.trace(args[iarg]);
-			int ii = 0;
-			if ((ii = super.parseArg(iarg, args)) != -1) {
-				iarg = ii;
-			} else if (args[iarg].equals(NEWICK) || args[iarg].equals(NEWICK1)) {
-				ImageProcessor.checkHasMoreArgs(iarg++, args);
-				setNewickFile(new File(args[iarg++]));
-			} else {
-				LOG.error("Cannot interpret: " + args[iarg]);
-				iarg++;
+			found = imageProcessor.parseArgAndAdvance(argIterator);
+			if (!found) {
+				LOG.debug("skipped unknown token: "+argIterator.getCurrent());
+				argIterator.next();
 			}
 		}
-
-		checkMandatory();
-		if (debug) {
-			super.debug();
-			this.debug();
-		}
-		return 0;
+		return found;
 	}
 
 	public void debug() {
 		}
 	}
 
-	public void checkMandatory() {
-		super.checkMandatory();
-		// check mandatory here
-	}
-
-	private void runArgs() throws IOException {
+	protected boolean runCommands() {
 		super.processImageFile();
 		this.processGraph();
+		int island = ensurePixelProcessor().getIsland() ;
+		if (newickFile != null && island >= 0) {
+			PixelGraph graph = pixelGraphList.get(island);
+			newickString = createNewick(graph);
+			this.writeNewickQuietly();
+		}
+		return true;
 	}
 	
 	public void processGraph() {
 		try {
 			PixelIslandList pixelIslandList = imageProcessor.getOrCreatePixelIslandList();
-			List<PixelGraph> pixelGraphList = pixelIslandList.analyzeEdgesAndPlot();
+			pixelGraphList = pixelIslandList.analyzeEdgesAndPlot();
 		} catch (IOException e) {
 			throw new RuntimeException("pixelGraphList failed", e);
 		}
 		this.newickFile.getParentFile().mkdirs();
 	}
 
+	public void parseArgsAndRun(String[] args) {
+		ensureImageProcessor();
+		this.parseArgs(args);
+		this.runCommands();
+	}
+
 }

src/test/java/org/xmlcml/diagrams/PhyloTreeTest.java

 package org.xmlcml.diagrams;
 
 import java.awt.image.BufferedImage;
-
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 
 	@Test
 	public void testImageProcessor() {
-		PhyloTree phyloTree = new PhyloTree();
+		DiagramAnalyzer phyloTree = new PhyloTree();
 		phyloTree.readAndProcessInputFile(new File(Fixtures.ROSS_DIR, "pone.0016923.g002.png"));
 	}
 	
 		List<PixelNode> rootNodes = pixelGraph.getPossibleRootNodes1();
 		LOG.debug("root "+rootNodes);
 		if (rootNodes.size() == 1) {
-//			PhyloTree phyloTree = new PhyloTree();
 			String newick = phyloTree.createNewick(pixelGraph, rootNodes.get(0));
-			LOG.debug("N "+newick);
 			Element xml = phyloTree.createXML(pixelGraph, rootNodes.get(0));
-			LOG.debug("X "+xml.toXML());
 			XMLUtil.debug(xml, new FileOutputStream("target/plot/0036933.g001/tree.xml"), 1);
 		}
 	}
 				+ "_nodes.svg"));
 	}
 
+	/** get PixelIsland through CommandLine.
+	 * 
+	 */
+	@Test
+	public void testGetPixelIslandThroughCommandLine() {
+		String[] args = {"-d", "-i",  "src/test/resources/org/xmlcml/diagrams/ross/pone.0036933.g001.png", 
+				"--island", "0", "-o", "target/plot/36933/", "--newick", "target/plot/36933.nwk"};
+		PhyloTree phyloTree = new PhyloTree();
+		phyloTree.parseArgsAndRun(args);
+	}
+
 }