2 // ~/SOFTWARE_DEV/ECLIPSE_WORKSPACE/forester/java/forester.jar
3 // org.forester.application.decorator2
4 // RRMa_ALL_plus_RRMa_ee3_50_hmmalign_05_40_fme_with_seqs_2.phylo.xml
5 // nature12311-s3_cz_4.txt x
7 package org.forester.application;
11 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
12 import org.forester.io.writers.PhylogenyWriter;
13 import org.forester.phylogeny.Phylogeny;
14 import org.forester.phylogeny.PhylogenyNode;
15 import org.forester.phylogeny.data.Annotation;
16 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
17 import org.forester.phylogeny.factories.PhylogenyFactory;
18 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
19 import org.forester.util.BasicTable;
20 import org.forester.util.BasicTableParser;
21 import org.forester.util.CommandLineArguments;
23 public class decorator2 {
25 private static final int SEQ_COLUMN = 3;
26 private static final int TARGET_COLUMN = 4;
28 public static void main( final String args[] ) {
33 CommandLineArguments cla = null;
34 cla = new CommandLineArguments( args );
35 intree = cla.getFile( 0 );
36 intable = cla.getFile( 1 );
37 outtree = cla.getFile( 2 );
38 if ( outtree.exists() ) {
39 System.out.println( outtree + " already exists" );
42 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
43 final PhyloXmlParser xml_parser = new PhyloXmlParser();
44 final Phylogeny phy = factory.create( intree, xml_parser )[ 0 ];
45 final BasicTable<String> t = BasicTableParser.parse( intable, '\t' );
46 System.out.println( t.toString() );
47 final PhylogenyNodeIterator it = phy.iteratorPostorder();
49 while ( it.hasNext() ) {
50 final PhylogenyNode node = it.next();
51 if ( node.isExternal() ) {
52 processNode( node, t );
56 final PhylogenyWriter writer = new PhylogenyWriter();
57 writer.toPhyloXML( outtree, phy, 0 );
59 catch ( final Exception e ) {
60 System.out.println( e.getLocalizedMessage() );
66 private static void processNode( final PhylogenyNode node, BasicTable<String> t ) throws Exception {
67 String node_seq = node.getNodeData().getSequence().getMolecularSequence().toUpperCase();
68 boolean found = false;
69 for( int col = 0; col < t.getNumberOfRows(); ++col ) {
70 String table_seq = t.getValueAsString( SEQ_COLUMN, col ).toUpperCase();
71 if ( table_seq.contains( node_seq ) ) {
73 throw new Exception( "Sequence from node " + node + " is not unique: " + node_seq );
76 Annotation annotation = new Annotation( "target:" + t.getValueAsString( TARGET_COLUMN, col ) );
77 node.getNodeData().getSequence().addAnnotation( annotation );
78 System.out.println( node + "->" + annotation );
82 // throw new Exception( "Sequence from node " + node + " not found: " + node_seq );