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
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 decoratorX {
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.iteratorExternalForward();
49 while ( it.hasNext() ) {
50 final PhylogenyNode node = it.next();
51 processNode( node, t );
54 final PhylogenyNodeIterator it2 = phy.iteratorExternalForward();
55 while ( it2.hasNext() ) {
56 final PhylogenyNode node = it2.next();
57 processNode2( node, phy );
59 final PhylogenyWriter writer = new PhylogenyWriter();
60 writer.toPhyloXML( outtree, phy, 0 );
62 catch ( final Exception e ) {
63 System.out.println( e.getLocalizedMessage() );
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 ) ) {
76 // throw new Exception( "Sequence from node " + node + " is not unique: " + node_seq );
79 final Annotation annotation = new Annotation( "target", t.getValueAsString( TARGET_COLUMN, col ) );
80 node.getNodeData().getSequence().addAnnotation( annotation );
81 System.out.println( node + "->" + annotation );
85 // throw new Exception( "Sequence from node " + node + " not found: " + node_seq );
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 );