d8692f055909f38fb277f9b53a8c7f9768cab307
[jalview.git] / forester / java / src / org / forester / io / parsers / phyloxml / data / BinaryCharactersParser.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // All rights reserved
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
25
26 package org.forester.io.parsers.phyloxml.data;
27
28 import java.util.SortedSet;
29 import java.util.TreeSet;
30
31 import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
32 import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
33 import org.forester.io.parsers.phyloxml.XmlElement;
34 import org.forester.phylogeny.data.BinaryCharacters;
35 import org.forester.phylogeny.data.PhylogenyData;
36
37 public class BinaryCharactersParser implements PhylogenyDataPhyloXmlParser {
38
39     private static final BinaryCharactersParser _instance;
40     static {
41         try {
42             _instance = new BinaryCharactersParser();
43         }
44         catch ( final Throwable e ) {
45             throw new RuntimeException( e.getMessage() );
46         }
47     }
48
49     private BinaryCharactersParser() {
50     }
51
52     @Override
53     public PhylogenyData parse( final XmlElement element ) throws PhyloXmlDataFormatException {
54         final SortedSet<String> present = new TreeSet<String>();
55         final SortedSet<String> gained = new TreeSet<String>();
56         final SortedSet<String> lost = new TreeSet<String>();
57         String type = "";
58         int present_count = BinaryCharacters.COUNT_DEFAULT;
59         int gained_count = BinaryCharacters.COUNT_DEFAULT;
60         int lost_count = BinaryCharacters.COUNT_DEFAULT;
61         if ( element.isHasAttribute( PhyloXmlMapping.BINARY_CHARACTERS_TYPE_ATTR ) ) {
62             type = element.getAttribute( PhyloXmlMapping.BINARY_CHARACTERS_TYPE_ATTR );
63         }
64         try {
65             if ( element.isHasAttribute( PhyloXmlMapping.BINARY_CHARACTERS_PRESENT_COUNT_ATTR ) ) {
66                 present_count = Integer.parseInt( element
67                         .getAttribute( PhyloXmlMapping.BINARY_CHARACTERS_PRESENT_COUNT_ATTR ) );
68             }
69             if ( element.isHasAttribute( PhyloXmlMapping.BINARY_CHARACTERS_GAINED_COUNT_ATTR ) ) {
70                 gained_count = Integer.parseInt( element
71                         .getAttribute( PhyloXmlMapping.BINARY_CHARACTERS_GAINED_COUNT_ATTR ) );
72             }
73             if ( element.isHasAttribute( PhyloXmlMapping.BINARY_CHARACTERS_LOST_COUNT_ATTR ) ) {
74                 lost_count = Integer
75                         .parseInt( element.getAttribute( PhyloXmlMapping.BINARY_CHARACTERS_LOST_COUNT_ATTR ) );
76             }
77         }
78         catch ( final NumberFormatException e ) {
79             throw new PhyloXmlDataFormatException( "failed to parse integer from element " + element.getQualifiedName() );
80         }
81         for( int i = 0; i < element.getNumberOfChildElements(); ++i ) {
82             final XmlElement child_element = element.getChildElement( i );
83             if ( child_element.getQualifiedName().equals( PhyloXmlMapping.BINARY_CHARACTERS_PRESENT ) ) {
84                 parseCharacters( present, child_element );
85             }
86             else if ( child_element.getQualifiedName().equals( PhyloXmlMapping.BINARY_CHARACTERS_GAINED ) ) {
87                 parseCharacters( gained, child_element );
88             }
89             else if ( child_element.getQualifiedName().equals( PhyloXmlMapping.BINARY_CHARACTERS_LOST ) ) {
90                 parseCharacters( lost, child_element );
91             }
92         }
93         BinaryCharacters bc = null;
94         if ( present_count != BinaryCharacters.COUNT_DEFAULT ) {
95             bc = new BinaryCharacters( present, gained, lost, type, present_count, gained_count, lost_count );
96         }
97         else {
98             bc = new BinaryCharacters( present, gained, lost, type );
99         }
100         return bc;
101     }
102
103     private void parseCharacters( final SortedSet<String> present, final XmlElement child_element ) {
104         for( int j = 0; j < child_element.getNumberOfChildElements(); ++j ) {
105             final XmlElement child_child_element = child_element.getChildElement( j );
106             if ( child_child_element.getQualifiedName().equals( PhyloXmlMapping.BINARY_CHARACTER ) ) {
107                 present.add( child_child_element.getValueAsString() );
108             }
109         }
110     }
111
112     public static PhylogenyDataPhyloXmlParser getInstance() {
113         return _instance;
114     }
115 }