2 package org.forester.application;
5 import java.util.ArrayList;
7 import java.util.SortedSet;
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;
20 public class subtree_feature_count {
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";
32 public static void main( final String args[] ) {
34 final CommandLineArguments cla = new CommandLineArguments( args );
35 if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length < 2 ) ) {
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 );
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 );
51 catch ( final Exception e ) {
53 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
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" );
64 ext.setIndicator( ( byte ) 1 );
69 boolean is_first = true;
70 PhylogenyNode first_node = null;
71 PhylogenyNode last_node = null;
73 for( final PhylogenyNode ext : ext_descs ) {
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" ) ) {
90 if ( xref.getComment().equalsIgnoreCase( "nmr" ) ) {
93 if ( xref.getComment().equalsIgnoreCase( "model" ) ) {
101 final StringBuilder sb = new StringBuilder();
102 sb.append( String.valueOf( c ) );
104 sb.append( first_node.getName() );
106 sb.append( last_node.getName() );
108 sb.append( String.valueOf( xray ) );
110 sb.append( String.valueOf( nmr ) );
112 sb.append( String.valueOf( model ) );
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 ) {
123 final StringBuilder s = analyzeSubtree( n, depth );
124 System.out.println( s.toString() );
128 private static PhylogenyNode moveUp( final PhylogenyNode node, final double depth ) {
129 PhylogenyNode n = node;
130 PhylogenyNode prev = node;
131 while ( depth < n.calculateDistanceToRoot() ) {
138 private static void printHelp() {
139 ForesterUtil.printProgramInformation( PRG_NAME,
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();
156 private static void setAllIndicatorsToZero( final Phylogeny phy ) {
157 for( final PhylogenyNodeIterator it = phy.iteratorPostorder(); it.hasNext(); ) {
158 it.next().setIndicator( ( byte ) 0 );