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, new PhyloXmlParser() )[ 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 for( final PhylogenyNode ext : node.getAllExternalDescendants() ) {
60 ext.setIndicator( ( byte ) 1 );
65 boolean is_first = true;
66 PhylogenyNode first_node = null;
67 PhylogenyNode last_node = null;
69 for( final PhylogenyNode ext : node.getAllExternalDescendants() ) {
76 if ( ext.getNodeData().isHasSequence() ) {
77 final Sequence seq = ext.getNodeData().getSequence();
78 final SortedSet<Accession> xrefs = seq.getCrossReferences();
79 if ( !ForesterUtil.isEmpty( xrefs ) ) {
80 for( final Accession xref : xrefs ) {
81 if ( xref.getSource().equalsIgnoreCase( "pdb" ) ) {
82 if ( xref.getComment().equalsIgnoreCase( "x-ray" )
83 || xref.getComment().equalsIgnoreCase( "xray" ) ) {
86 if ( xref.getComment().equalsIgnoreCase( "nmr" ) ) {
89 if ( xref.getComment().equalsIgnoreCase( "model" ) ) {
97 final StringBuilder sb = new StringBuilder();
98 sb.append( String.valueOf( c ) );
100 sb.append( first_node.getName() );
102 sb.append( last_node.getName() );
104 sb.append( String.valueOf( xray ) );
106 sb.append( String.valueOf( nmr ) );
108 sb.append( String.valueOf( model ) );
112 private static void execute( final Phylogeny phy, final double depth ) {
113 setAllIndicatorsToZero( phy );
114 for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
115 final PhylogenyNode n = it.next();
116 if ( n.getIndicator() != 0 ) {
119 final StringBuilder s = analyzeSubtree( n, depth );
120 System.out.println( s.toString() );
124 private static PhylogenyNode moveUpOLd( final PhylogenyNode node, final double depth ) {
125 PhylogenyNode n = node;
126 double current_depth = 0.0;
127 while ( current_depth < depth ) {
128 current_depth += n.getDistanceToParent();
129 if ( n.getParent() == null ) {
130 throw new IllegalArgumentException( "Depth " + depth + " is too large" );
137 private static PhylogenyNode moveUp( final PhylogenyNode node, final double depth ) {
138 PhylogenyNode n = node;
139 PhylogenyNode prev = node;
140 while ( depth > n.calculateDistanceToRoot() ) {
147 private static void printHelp() {
148 ForesterUtil.printProgramInformation( PRG_NAME,
154 ForesterUtil.getForesterLibraryInformation() );
155 System.out.println( "Usage:" );
156 System.out.println();
157 System.out.println( PRG_NAME + "" );
158 System.out.println();
159 System.out.println( " example: " );
160 System.out.println();
161 System.out.println();
162 System.out.println();
165 private static void setAllIndicatorsToZero( final Phylogeny phy ) {
166 for( final PhylogenyNodeIterator it = phy.iteratorPostorder(); it.hasNext(); ) {
167 it.next().setIndicator( ( byte ) 0 );