inprogress
[jalview.git] / forester / java / src / org / forester / application / decoratorX.java
1 // java -Xmx2048m -cp
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
6
7 package org.forester.application;
8
9 import java.io.File;
10
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;
22
23 public class decoratorX {
24
25     private static final int SEQ_COLUMN    = 3;
26     private static final int TARGET_COLUMN = 4;
27
28     public static void main( final String args[] ) {
29         File intree = null;
30         File outtree = null;
31         File intable = null;
32         try {
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" );
40                 System.exit( -1 );
41             }
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();
48             int i = 0;
49             while ( it.hasNext() ) {
50                 final PhylogenyNode node = it.next();
51                 if ( node.isExternal() ) {
52                     processNode( node, t );
53                 }
54                 i++;
55             }
56             final PhylogenyWriter writer = new PhylogenyWriter();
57             writer.toPhyloXML( outtree, phy, 0 );
58         }
59         catch ( final Exception e ) {
60             System.out.println( e.getLocalizedMessage() );
61             e.printStackTrace();
62             System.exit( -1 );
63         }
64     }
65
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 ) ) {
72                 if ( found ) {
73                     throw new Exception( "Sequence from node " + node + " is not unique: " + node_seq );
74                 }
75                 found = true;
76                 Annotation annotation = new Annotation( "target:" + t.getValueAsString( TARGET_COLUMN, col ) );
77                 node.getNodeData().getSequence().addAnnotation( annotation );
78                 System.out.println( node + "->" + annotation );
79             }
80         }
81         // if ( !found ) {
82         //     throw new Exception( "Sequence from node " + node + " not found: " + node_seq );
83         // }
84     }
85 }