+++ /dev/null
-// 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<String> 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<String> 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 );
- // }
- }
-}
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();
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 );
}
}
private static void processNode( final PhylogenyNode node, final BasicTable<String> 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 );
}
public class BasicTable<E> {
- private Map<String, Map<String, E>> _rows;
- private int _max_row;
private int _max_col;
+ private int _max_row;
+ private Map<String, Map<String, E>> _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<String, E> getColumnsAsMap( final int key_col, final int value_col ) throws IllegalArgumentException {
final Map<String, E> map = new HashMap<String, E>();
for( int row = 0; row < getNumberOfRows(); ++row ) {
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;
}
return _max_row + 1;
}
- private Map<String, E> getRow( final int row ) {
- return getRows().get( "" + row );
- }
-
- private Map<String, Map<String, E>> 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 {
return null;
}
- private void init() {
- _rows = new HashMap<String, Map<String, E>>();
- 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" );
@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 ) );
}
return sb.toString();
}
+
+ private Map<String, E> getRow( final int row ) {
+ return getRows().get( "" + row );
+ }
+
+ private Map<String, Map<String, E>> getRows() {
+ return _rows;
+ }
+
+ private void init() {
+ _rows = new HashMap<String, Map<String, E>>();
+ 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;
+ }
}