in progress
[jalview.git] / forester / java / src / org / forester / application / map_lengths.java
1 // $Id:
2 //
3 // forester -- software libraries and applications
4 // for genomics and evolutionary biology research.
5 //
6 // Copyright (C) 2011 Christian M Zmasek
7 // Copyright (C) 2011 Sanford-Burnham Medical Research Institute
8 // All rights reserved
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 //
24 // Contact: phylosoft @ gmail . com
25 // WWW: www.phylosoft.org/forester
26
27 package org.forester.application;
28
29 import java.io.File;
30 import java.io.IOException;
31 import java.util.List;
32
33 import org.forester.archaeopteryx.Archaeopteryx;
34 import org.forester.io.parsers.FastaParser;
35 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
36 import org.forester.phylogeny.Phylogeny;
37 import org.forester.phylogeny.PhylogenyNode;
38 import org.forester.phylogeny.data.PropertiesMap;
39 import org.forester.phylogeny.data.Property;
40 import org.forester.phylogeny.data.Property.AppliesTo;
41 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
42 import org.forester.phylogeny.factories.PhylogenyFactory;
43 import org.forester.sequence.Sequence;
44 import org.forester.util.CommandLineArguments;
45 import org.forester.util.ForesterUtil;
46
47 public class map_lengths {
48
49     final static private String PRG_NAME = "map_lengths";
50
51     public static void main( final String[] args ) {
52         CommandLineArguments cla = null;
53         try {
54             cla = new CommandLineArguments( args );
55         }
56         catch ( final Exception e ) {
57             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
58         }
59         try {
60             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
61             final PhyloXmlParser xml_parser = new PhyloXmlParser();
62             final Phylogeny[] phylogenies_0 = factory.create( cla.getFile( 0 ), xml_parser );
63             final Phylogeny phy = phylogenies_0[ 0 ];
64             for( int i = 1; i < cla.getNumberOfNames(); i++ ) {
65                 final String fasta_name = cla.getName( i );
66                 final List<Sequence> seqs = FastaParser.parse( new File( fasta_name ) );
67                 for( int s = 0; s < seqs.size(); s++ ) {
68                     final Sequence seq = seqs.get( s );
69                     final int actual_length = seq.getLength() - seq.getNumberOfGapResidues();
70                     String node_name = "" + seq.getIdentifier();
71                     node_name = node_name.substring( 0, node_name.indexOf( "/" ) );
72                     final PhylogenyNode n = phy.getNode( node_name );
73                     if ( n.getNodeData().getProperties() == null ) {
74                         n.getNodeData().setProperties( new PropertiesMap() );
75                     }
76                     final PropertiesMap properties = n.getNodeData().getProperties();
77                     final Property p = new Property( "r:" + i, "" + actual_length, "", "xsd:integer", AppliesTo.NODE );
78                     properties.addProperty( p );
79                 }
80             }
81             Archaeopteryx.createApplication( phy );
82         }
83         catch ( final IOException e ) {
84             // TODO Auto-generated catch block
85             e.printStackTrace();
86         }
87     }
88 }