inprogress
[jalview.git] / forester / java / src / org / forester / application / subtree_feature_count.java
1
2 package org.forester.application;
3
4 import java.io.File;
5 import java.util.ArrayList;
6 import java.util.List;
7 import java.util.SortedSet;
8
9 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
10 import org.forester.phylogeny.Phylogeny;
11 import org.forester.phylogeny.PhylogenyNode;
12 import org.forester.phylogeny.data.Accession;
13 import org.forester.phylogeny.data.Sequence;
14 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
15 import org.forester.phylogeny.factories.PhylogenyFactory;
16 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
17 import org.forester.util.CommandLineArguments;
18 import org.forester.util.ForesterUtil;
19
20 public class subtree_feature_count {
21
22     final static private String DEPTH_OPTION  = "d";
23     final static private String E_MAIL        = "phylosoft@gmail.com";
24     final static private String HELP_OPTION_1 = "help";
25     final static private String HELP_OPTION_2 = "h";
26     final static private String PRG_DATE      = "131120";
27     final static private String PRG_DESC      = "";
28     final static private String PRG_NAME      = "subtree_feature_count";
29     final static private String PRG_VERSION   = "0.90";
30     final static private String WWW           = "sites.google.com/site/cmzmasek/home/software/forester";
31
32     public static void main( final String args[] ) {
33         try {
34             final CommandLineArguments cla = new CommandLineArguments( args );
35             if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length < 2 ) ) {
36                 printHelp();
37                 System.exit( 0 );
38             }
39             final List<String> allowed_options = new ArrayList<String>();
40             allowed_options.add( DEPTH_OPTION );
41             final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
42             if ( dissallowed_options.length() > 0 ) {
43                 ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
44             }
45             final double depth = cla.getOptionValueAsDouble( DEPTH_OPTION );
46             final File intree_file = cla.getFile( 0 );
47             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
48             final Phylogeny phy = factory.create( intree_file, PhyloXmlParser.createPhyloXmlParserXsdValidating() )[ 0 ];
49             execute( phy, depth );
50         }
51         catch ( final Exception e ) {
52             e.printStackTrace();
53             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
54         }
55     }
56
57     private static StringBuilder analyzeSubtree( final PhylogenyNode n, final double depth ) {
58         final PhylogenyNode node = moveUp( n, depth );
59         final List<PhylogenyNode> ext_descs = node.getAllExternalDescendants();
60         for( final PhylogenyNode ext : ext_descs ) {
61             if ( ext.getIndicator() != 0 ) {
62                 throw new RuntimeException( "this should not have happened" );
63             }
64             ext.setIndicator( ( byte ) 1 );
65         }
66         int xray = 0;
67         int nmr = 0;
68         int model = 0;
69         boolean is_first = true;
70         PhylogenyNode first_node = null;
71         PhylogenyNode last_node = null;
72         int c = 0;
73         for( final PhylogenyNode ext : ext_descs ) {
74             if ( is_first ) {
75                 first_node = ext;
76                 is_first = false;
77             }
78             last_node = ext;
79             ++c;
80             if ( ext.getNodeData().isHasSequence() ) {
81                 final Sequence seq = ext.getNodeData().getSequence();
82                 final SortedSet<Accession> xrefs = seq.getCrossReferences();
83                 if ( !ForesterUtil.isEmpty( xrefs ) ) {
84                     for( final Accession xref : xrefs ) {
85                         if ( xref.getSource().equalsIgnoreCase( "pdb" ) ) {
86                             if ( xref.getComment().equalsIgnoreCase( "x-ray" )
87                                     || xref.getComment().equalsIgnoreCase( "xray" ) ) {
88                                 ++xray;
89                             }
90                             if ( xref.getComment().equalsIgnoreCase( "nmr" ) ) {
91                                 ++nmr;
92                             }
93                             if ( xref.getComment().equalsIgnoreCase( "model" ) ) {
94                                 ++model;
95                             }
96                         }
97                     }
98                 }
99             }
100         }
101         final StringBuilder sb = new StringBuilder();
102         sb.append( String.valueOf( c ) );
103         sb.append( "\t" );
104         sb.append( first_node.getName() );
105         sb.append( "\t" );
106         sb.append( last_node.getName() );
107         sb.append( "\t" );
108         sb.append( String.valueOf( xray ) );
109         sb.append( "\t" );
110         sb.append( String.valueOf( nmr ) );
111         sb.append( "\t" );
112         sb.append( String.valueOf( model ) );
113         return sb;
114     }
115
116     private static void execute( final Phylogeny phy, final double depth ) {
117         setAllIndicatorsToZero( phy );
118         for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
119             final PhylogenyNode n = it.next();
120             if ( n.getIndicator() != 0 ) {
121                 continue;
122             }
123             final StringBuilder s = analyzeSubtree( n, depth );
124             System.out.println( s.toString() );
125         }
126     }
127
128     private static PhylogenyNode moveUp( final PhylogenyNode node, final double depth ) {
129         PhylogenyNode n = node;
130         PhylogenyNode prev = node;
131         while ( depth < n.calculateDistanceToRoot() ) {
132             prev = n;
133             n = n.getParent();
134         }
135         return prev;
136     }
137
138     private static void printHelp() {
139         ForesterUtil.printProgramInformation( PRG_NAME,
140                                               PRG_DESC,
141                                               PRG_VERSION,
142                                               PRG_DATE,
143                                               E_MAIL,
144                                               WWW,
145                                               ForesterUtil.getForesterLibraryInformation() );
146         System.out.println( "Usage:" );
147         System.out.println();
148         System.out.println( PRG_NAME + "" );
149         System.out.println();
150         System.out.println( " example: " );
151         System.out.println();
152         System.out.println();
153         System.out.println();
154     }
155
156     private static void setAllIndicatorsToZero( final Phylogeny phy ) {
157         for( final PhylogenyNodeIterator it = phy.iteratorPostorder(); it.hasNext(); ) {
158             it.next().setIndicator( ( byte ) 0 );
159         }
160     }
161 }