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.decoratorX
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.iteratorExternalForward();
48             int i = 0;
49             while ( it.hasNext() ) {
50                 final PhylogenyNode node = it.next();
51                 processNode( node, t );
52                 i++;
53             }
54             final PhylogenyNodeIterator it2 = phy.iteratorExternalForward();
55             while ( it2.hasNext() ) {
56                 final PhylogenyNode node = it2.next();
57                 processNode2( node, phy );
58             }
59             final PhylogenyWriter writer = new PhylogenyWriter();
60             writer.toPhyloXML( outtree, phy, 0 );
61         }
62         catch ( final Exception e ) {
63             System.out.println( e.getLocalizedMessage() );
64             e.printStackTrace();
65             System.exit( -1 );
66         }
67     }
68
69     private static void processNode( final PhylogenyNode node, final BasicTable<String> t ) throws Exception {
70         final String node_seq = node.getNodeData().getSequence().getMolecularSequence().toUpperCase();
71         boolean found = false;
72         for( int col = 0; col < t.getNumberOfRows(); ++col ) {
73             final String table_seq = t.getValueAsString( SEQ_COLUMN, col ).toUpperCase();
74             if ( table_seq.contains( node_seq ) ) {
75                 if ( found ) {
76                     // throw new Exception( "Sequence from node " + node + " is not unique: " + node_seq );
77                 }
78                 found = true;
79                 final Annotation annotation = new Annotation( "target", t.getValueAsString( TARGET_COLUMN, col ) );
80                 node.getNodeData().getSequence().addAnnotation( annotation );
81                 System.out.println( node + "->" + annotation );
82             }
83         }
84         // if ( !found ) {
85         //     throw new Exception( "Sequence from node " + node + " not found: " + node_seq );
86         // }
87     }
88
89     private static void processNode2( final PhylogenyNode node, final Phylogeny t ) {
90         if ( ( node.getNodeData().getSequence().getAnnotations() == null )
91                 || node.getNodeData().getSequence().getAnnotations().isEmpty() ) {
92             t.deleteSubtree( node, true );
93         }
94     }
95 }