From f47b5de663d2ca59a4d53587ea8d5cd20c594db2 Mon Sep 17 00:00:00 2001 From: "cmzmasek@gmail.com" Date: Thu, 26 Sep 2013 21:33:57 +0000 Subject: [PATCH] inprogress --- .../src/org/forester/application/decorator2.java | 85 -------------------- .../src/org/forester/application/decoratorX.java | 49 +++++++---- .../java/src/org/forester/util/BasicTable.java | 85 +++++++++++--------- 3 files changed, 83 insertions(+), 136 deletions(-) delete mode 100644 forester/java/src/org/forester/application/decorator2.java diff --git a/forester/java/src/org/forester/application/decorator2.java b/forester/java/src/org/forester/application/decorator2.java deleted file mode 100644 index fbbf41c..0000000 --- a/forester/java/src/org/forester/application/decorator2.java +++ /dev/null @@ -1,85 +0,0 @@ -// java -Xmx2048m -cp -// ~/SOFTWARE_DEV/ECLIPSE_WORKSPACE/forester/java/forester.jar -// org.forester.application.decorator2 -// RRMa_ALL_plus_RRMa_ee3_50_hmmalign_05_40_fme_with_seqs_2.phylo.xml -// nature12311-s3_cz_4.txt x - -package org.forester.application; - -import java.io.File; - -import org.forester.io.parsers.phyloxml.PhyloXmlParser; -import org.forester.io.writers.PhylogenyWriter; -import org.forester.phylogeny.Phylogeny; -import org.forester.phylogeny.PhylogenyNode; -import org.forester.phylogeny.data.Annotation; -import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory; -import org.forester.phylogeny.factories.PhylogenyFactory; -import org.forester.phylogeny.iterators.PhylogenyNodeIterator; -import org.forester.util.BasicTable; -import org.forester.util.BasicTableParser; -import org.forester.util.CommandLineArguments; - -public class decorator2 { - - private static final int SEQ_COLUMN = 3; - private static final int TARGET_COLUMN = 4; - - public static void main( final String args[] ) { - File intree = null; - File outtree = null; - File intable = null; - try { - CommandLineArguments cla = null; - cla = new CommandLineArguments( args ); - intree = cla.getFile( 0 ); - intable = cla.getFile( 1 ); - outtree = cla.getFile( 2 ); - if ( outtree.exists() ) { - System.out.println( outtree + " already exists" ); - System.exit( -1 ); - } - final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance(); - final PhyloXmlParser xml_parser = new PhyloXmlParser(); - final Phylogeny phy = factory.create( intree, xml_parser )[ 0 ]; - final BasicTable t = BasicTableParser.parse( intable, '\t' ); - System.out.println( t.toString() ); - final PhylogenyNodeIterator it = phy.iteratorPostorder(); - int i = 0; - while ( it.hasNext() ) { - final PhylogenyNode node = it.next(); - if ( node.isExternal() ) { - processNode( node, t ); - } - i++; - } - final PhylogenyWriter writer = new PhylogenyWriter(); - writer.toPhyloXML( outtree, phy, 0 ); - } - catch ( final Exception e ) { - System.out.println( e.getLocalizedMessage() ); - e.printStackTrace(); - System.exit( -1 ); - } - } - - private static void processNode( final PhylogenyNode node, BasicTable t ) throws Exception { - String node_seq = node.getNodeData().getSequence().getMolecularSequence().toUpperCase(); - boolean found = false; - for( int col = 0; col < t.getNumberOfRows(); ++col ) { - String table_seq = t.getValueAsString( SEQ_COLUMN, col ).toUpperCase(); - if ( table_seq.contains( node_seq ) ) { - if ( found ) { - throw new Exception( "Sequence from node " + node + " is not unique: " + node_seq ); - } - found = true; - Annotation annotation = new Annotation( "target:" + t.getValueAsString( TARGET_COLUMN, col ) ); - node.getNodeData().getSequence().addAnnotation( annotation ); - System.out.println( node + "->" + annotation ); - } - } - // if ( !found ) { - // throw new Exception( "Sequence from node " + node + " not found: " + node_seq ); - // } - } -} diff --git a/forester/java/src/org/forester/application/decoratorX.java b/forester/java/src/org/forester/application/decoratorX.java index 916a4b7..677d9ed 100644 --- a/forester/java/src/org/forester/application/decoratorX.java +++ b/forester/java/src/org/forester/application/decoratorX.java @@ -22,21 +22,29 @@ import org.forester.util.CommandLineArguments; public class decoratorX { - private static final int SEQ_COLUMN = 3; - private static final int TARGET_COLUMN = 4; + private static final int SEQ_NAME_COLUMN = 1; + private static final int SPECIES_COLUMN = 2; + private static final int SEQ_COLUMN = 3; + private static final int TARGET_COLUMN = 4; public static void main( final String args[] ) { File intree = null; - File outtree = null; + File outtree1 = null; + File outtree2 = null; File intable = null; try { CommandLineArguments cla = null; cla = new CommandLineArguments( args ); intree = cla.getFile( 0 ); intable = cla.getFile( 1 ); - outtree = cla.getFile( 2 ); - if ( outtree.exists() ) { - System.out.println( outtree + " already exists" ); + outtree1 = cla.getFile( 2 ); + outtree2 = cla.getFile( 3 ); + if ( outtree1.exists() ) { + System.out.println( outtree1 + " already exists" ); + System.exit( -1 ); + } + if ( outtree2.exists() ) { + System.out.println( outtree2 + " already exists" ); System.exit( -1 ); } final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance(); @@ -51,17 +59,18 @@ public class decoratorX { processNode( node, t ); i++; } + final PhylogenyWriter writer1 = new PhylogenyWriter(); + writer1.toPhyloXML( outtree1, phy, 0 ); final PhylogenyNodeIterator it2 = phy.iteratorExternalForward(); while ( it2.hasNext() ) { final PhylogenyNode node = it2.next(); processNode2( node, phy ); } - final PhylogenyWriter writer = new PhylogenyWriter(); - writer.toPhyloXML( outtree, phy, 0 ); + final PhylogenyWriter writer2 = new PhylogenyWriter(); + writer2.toPhyloXML( outtree2, phy, 0 ); } catch ( final Exception e ) { System.out.println( e.getLocalizedMessage() ); - e.printStackTrace(); System.exit( -1 ); } } @@ -69,14 +78,26 @@ public class decoratorX { private static void processNode( final PhylogenyNode node, final BasicTable t ) throws Exception { final String node_seq = node.getNodeData().getSequence().getMolecularSequence().toUpperCase(); boolean found = false; - for( int col = 0; col < t.getNumberOfRows(); ++col ) { - final String table_seq = t.getValueAsString( SEQ_COLUMN, col ).toUpperCase(); + String found_row = ""; + String found_protein_name = ""; + String found_species = ""; + for( int row = 0; row < t.getNumberOfRows(); ++row ) { + final String table_seq = t.getValueAsString( SEQ_COLUMN, row ).toUpperCase(); if ( table_seq.contains( node_seq ) ) { if ( found ) { - // throw new Exception( "Sequence from node " + node + " is not unique: " + node_seq ); + if ( !found_protein_name.equals( t.getValueAsString( SEQ_NAME_COLUMN, row ) ) + || !found_species.equals( t.getValueAsString( SPECIES_COLUMN, row ) ) ) { + throw new Exception( "Sequence from node " + node + " is not unique: " + node_seq + "\n" + + "Already found in row " + found_row ); + } + } + else { + found = true; + found_row = t.getRowAsString( row, ", " ); + found_protein_name = t.getValueAsString( SEQ_NAME_COLUMN, row ); + found_species = t.getValueAsString( SPECIES_COLUMN, row ); } - found = true; - final Annotation annotation = new Annotation( "target", t.getValueAsString( TARGET_COLUMN, col ) ); + final Annotation annotation = new Annotation( "target", t.getValueAsString( TARGET_COLUMN, row ) ); node.getNodeData().getSequence().addAnnotation( annotation ); System.out.println( node + "->" + annotation ); } diff --git a/forester/java/src/org/forester/util/BasicTable.java b/forester/java/src/org/forester/util/BasicTable.java index 2c7afc8..cb96dca 100644 --- a/forester/java/src/org/forester/util/BasicTable.java +++ b/forester/java/src/org/forester/util/BasicTable.java @@ -31,14 +31,28 @@ import java.util.Map; public class BasicTable { - private Map> _rows; - private int _max_row; private int _max_col; + private int _max_row; + private Map> _rows; public BasicTable() { init(); } + // Returns -1 if not found, IllegalArgumentException if not unique. + public int findRow( final String first_col_value ) throws IllegalArgumentException { + int result = -1; + for( int i = 0; i < this.getNumberOfRows(); ++i ) { + if ( getValueAsString( 0, i ).equals( first_col_value ) ) { + if ( result >= 0 ) { + throw new IllegalArgumentException( "\"" + first_col_value + "\" is not unique" ); + } + result = i; + } + } + return result; + } + public Map getColumnsAsMap( final int key_col, final int value_col ) throws IllegalArgumentException { final Map map = new HashMap(); for( int row = 0; row < getNumberOfRows(); ++row ) { @@ -76,20 +90,6 @@ public class BasicTable { return map; } - // Returns -1 if not found, IllegalArgumentException if not unique. - public int findRow( final String first_col_value ) throws IllegalArgumentException { - int result = -1; - for( int i = 0; i < this.getNumberOfRows(); ++i ) { - if ( getValueAsString( 0, i ).equals( first_col_value ) ) { - if ( result >= 0 ) { - throw new IllegalArgumentException( "\"" + first_col_value + "\" is not unique" ); - } - result = i; - } - } - return result; - } - public int getNumberOfColumns() { return _max_col + 1; } @@ -98,12 +98,15 @@ public class BasicTable { return _max_row + 1; } - private Map getRow( final int row ) { - return getRows().get( "" + row ); - } - - private Map> getRows() { - return _rows; + public final String getRowAsString( final int row, final String separator ) { + final StringBuilder sb = new StringBuilder(); + for( int col = 0; col < getNumberOfColumns(); ++col ) { + sb.append( getValue( col, row ).toString() ); + if ( col < ( getNumberOfColumns() - 1 ) ) { + sb.append( separator ); + } + } + return sb.toString(); } public E getValue( final int col, final int row ) throws IllegalArgumentException { @@ -129,24 +132,10 @@ public class BasicTable { return null; } - private void init() { - _rows = new HashMap>(); - setMaxCol( -1 ); - setMaxRow( -1 ); - } - public boolean isEmpty() { return getNumberOfRows() <= 0; } - private void setMaxCol( final int max_col ) { - _max_col = max_col; - } - - private void setMaxRow( final int max_row ) { - _max_row = max_row; - } - public void setValue( final int col, final int row, final E value ) { if ( ( row < 0 ) || ( col < 0 ) ) { throw new IllegalArgumentException( "attempt to use negative values for row or column" ); @@ -171,7 +160,7 @@ public class BasicTable { @Override public String toString() { - final StringBuffer sb = new StringBuffer(); + final StringBuilder sb = new StringBuilder(); for( int row = 0; row < getNumberOfRows(); ++row ) { for( int col = 0; col < getNumberOfColumns(); ++col ) { sb.append( getValue( col, row ) ); @@ -185,4 +174,26 @@ public class BasicTable { } return sb.toString(); } + + private Map getRow( final int row ) { + return getRows().get( "" + row ); + } + + private Map> getRows() { + return _rows; + } + + private void init() { + _rows = new HashMap>(); + setMaxCol( -1 ); + setMaxRow( -1 ); + } + + private void setMaxCol( final int max_col ) { + _max_col = max_col; + } + + private void setMaxRow( final int max_row ) { + _max_row = max_row; + } } -- 1.7.10.2