in pprogress...
[jalview.git] / forester_applications / src / org / forester / applications / subtree_feature_count.java
1 // javac -cp ~/SOFTWARE_DEV/ECLIPSE_WORKSPACE/forester/java/forester.jar
2 // ~/SOFTWARE_DEV/ECLIPSE_WORKSPACE/forester_applications/src/org/forester/applications/subtree_feature_count.java
3 //
4 // java -Xmx2048m -cp
5 // /home/czmasek/SOFTWARE_DEV/ECLIPSE_WORKSPACE/forester_applications/src/:/home/czmasek/SOFTWARE_DEV/ECLIPSE_WORKSPACE/forester/java/forester.jar
6 // org.forester.applications.subtree_feature_count
7
8 package org.forester.applications;
9
10 import java.io.File;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.SortedSet;
14
15 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
16 import org.forester.phylogeny.Phylogeny;
17 import org.forester.phylogeny.PhylogenyMethods;
18 import org.forester.phylogeny.PhylogenyNode;
19 import org.forester.phylogeny.data.Accession;
20 import org.forester.phylogeny.data.Sequence;
21 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
22 import org.forester.phylogeny.factories.PhylogenyFactory;
23 import org.forester.util.CommandLineArguments;
24 import org.forester.util.ForesterUtil;
25
26 public class subtree_feature_count {
27
28     final static private String MIN_DISTANCE_TO_ROOT_OPTION = "d";
29     final static private String E_MAIL                      = "phylosoft@gmail.com";
30     final static private String HELP_OPTION_1               = "help";
31     final static private String HELP_OPTION_2               = "h";
32     final static private String PRG_DATE                    = "131120";
33     final static private String PRG_DESC                    = "";
34     final static private String PRG_NAME                    = "subtree_feature_count";
35     final static private String PRG_VERSION                 = "0.90";
36     final static private String WWW                         = "sites.google.com/site/cmzmasek/home/software/forester";
37
38     public static void main( final String args[] ) {
39         try {
40             final CommandLineArguments cla = new CommandLineArguments( args );
41             if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length < 2 ) ) {
42                 printHelp();
43                 System.exit( 0 );
44             }
45             final List<String> allowed_options = new ArrayList<String>();
46             allowed_options.add( MIN_DISTANCE_TO_ROOT_OPTION );
47             final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
48             if ( dissallowed_options.length() > 0 ) {
49                 ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
50             }
51             final double min_distance_to_root = cla.getOptionValueAsDouble( MIN_DISTANCE_TO_ROOT_OPTION );
52             if ( min_distance_to_root <= 0 ) {
53                 ForesterUtil.fatalError( PRG_NAME, "attempt to use min distance to root of: " + min_distance_to_root );
54             }
55             final File intree_file = cla.getFile( 0 );
56             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
57             final Phylogeny phy = factory.create( intree_file, PhyloXmlParser.createPhyloXmlParserXsdValidating() )[ 0 ];
58             execute( phy, min_distance_to_root );
59         }
60         catch ( final Exception e ) {
61             e.printStackTrace();
62             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
63         }
64     }
65
66     private final static void execute( final Phylogeny phy, final double min_distance_to_root ) {
67         final List<List<PhylogenyNode>> ll = PhylogenyMethods.divideIntoSubTrees( phy, min_distance_to_root );
68         for( final List<PhylogenyNode> l : ll ) {
69             int xray = 0;
70             int nmr = 0;
71             int model = 0;
72             for( final PhylogenyNode node : l ) {
73                 if ( node.getNodeData().isHasSequence() ) {
74                     final Sequence seq = node.getNodeData().getSequence();
75                     final SortedSet<Accession> xrefs = seq.getCrossReferences();
76                     if ( !ForesterUtil.isEmpty( xrefs ) ) {
77                         for( final Accession xref : xrefs ) {
78                             if ( xref.getSource().equalsIgnoreCase( "pdb" ) ) {
79                                 if ( xref.getComment().equalsIgnoreCase( "x-ray" )
80                                         || xref.getComment().equalsIgnoreCase( "xray" ) ) {
81                                     ++xray;
82                                 }
83                                 if ( xref.getComment().equalsIgnoreCase( "nmr" ) ) {
84                                     ++nmr;
85                                 }
86                                 if ( xref.getComment().equalsIgnoreCase( "model" ) ) {
87                                     ++model;
88                                 }
89                             }
90                         }
91                     }
92                 }
93             }
94             final int n = l.size();
95             final double xray_p = ForesterUtil.round( ( 100.0 * xray ) / n, 1 );
96             final double nmr_p = ForesterUtil.round( ( 100.0 * nmr ) / n, 1 );
97             final double model_p = ForesterUtil.round( ( 100.0 * model ) / n, 1 );
98             final StringBuilder sb = new StringBuilder();
99             sb.append( String.valueOf( n ) );
100             sb.append( "\t" );
101             sb.append( String.valueOf( xray ) );
102             sb.append( "\t" );
103             sb.append( String.valueOf( nmr ) );
104             sb.append( "\t" );
105             sb.append( String.valueOf( model ) );
106             sb.append( "\t" );
107             sb.append( String.valueOf( xray_p ) );
108             sb.append( "\t" );
109             sb.append( String.valueOf( nmr_p ) );
110             sb.append( "\t" );
111             sb.append( String.valueOf( model_p ) );
112             System.out.println( sb );
113         }
114     }
115
116     private static void printHelp() {
117         ForesterUtil.printProgramInformation( PRG_NAME,
118                                               PRG_DESC,
119                                               PRG_VERSION,
120                                               PRG_DATE,
121                                               E_MAIL,
122                                               WWW,
123                                               ForesterUtil.getForesterLibraryInformation() );
124         System.out.print( "Usage: " );
125         System.out.println( PRG_NAME + " -d=<min distance to root> <intree>" );
126         System.out.println();
127         System.out.println();
128     }
129 }