clean up
[jalview.git] / forester / java / src / org / forester / io / parsers / phyloxml / data / SequenceRelationParser.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: www.phylosoft.org/forester
25
26 package org.forester.io.parsers.phyloxml.data;
27
28 import java.util.HashMap;
29 import java.util.Map;
30
31 import org.forester.io.parsers.phyloxml.PhyloXmlHandler;
32 import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
33 import org.forester.io.parsers.phyloxml.XmlElement;
34 import org.forester.io.parsers.util.PhylogenyParserException;
35 import org.forester.phylogeny.Phylogeny;
36 import org.forester.phylogeny.data.Confidence;
37 import org.forester.phylogeny.data.Sequence;
38 import org.forester.phylogeny.data.SequenceRelation;
39
40 public class SequenceRelationParser implements PhylogenyDataPhyloXmlParser {
41
42     private static final Map<Phylogeny, SequenceRelationParser> _instances = new HashMap<Phylogeny, SequenceRelationParser>();
43     private Phylogeny                                           _phylogeny;
44
45     private SequenceRelationParser() {
46     }
47
48     @Override
49     public SequenceRelation parse( final XmlElement element ) throws PhylogenyParserException {
50         final SequenceRelation seqRelation = new SequenceRelation();
51         if ( element.isHasAttribute( PhyloXmlMapping.SEQUENCE_RELATION_TYPE ) ) {
52             final String sType = element.getAttribute( PhyloXmlMapping.SEQUENCE_RELATION_TYPE );
53             seqRelation.setType( SequenceRelation.SEQUENCE_RELATION_TYPE.valueOf( sType ) );
54         }
55         if ( element.isHasAttribute( PhyloXmlMapping.SEQUENCE_RELATION_ID_REF0 ) && ( _phylogeny != null ) ) {
56             final Sequence ref = PhyloXmlHandler.getSequenceMapByIdForPhylogeny( _phylogeny )
57                     .get( element.getAttribute( PhyloXmlMapping.SEQUENCE_RELATION_ID_REF0 ) );
58             if ( ref != null ) {
59                 seqRelation.setRef0( ref );
60             }
61         }
62         if ( element.isHasAttribute( PhyloXmlMapping.SEQUENCE_RELATION_ID_REF1 ) && ( _phylogeny != null ) ) {
63             final Sequence ref = PhyloXmlHandler.getSequenceMapByIdForPhylogeny( _phylogeny )
64                     .get( element.getAttribute( PhyloXmlMapping.SEQUENCE_RELATION_ID_REF1 ) );
65             if ( ref != null ) {
66                 seqRelation.setRef1( ref );
67             }
68         }
69         if ( element.isHasAttribute( PhyloXmlMapping.SEQUENCE_RELATION_DISTANCE ) ) {
70             seqRelation
71                     .setDistance( Double.valueOf( element.getAttribute( PhyloXmlMapping.SEQUENCE_RELATION_DISTANCE ) ) );
72         }
73         for( int i = 0; i < element.getNumberOfChildElements(); ++i ) {
74             final XmlElement child_element = element.getChildElement( i );
75             if ( child_element.getQualifiedName().equals( PhyloXmlMapping.CONFIDENCE ) ) {
76                 seqRelation.setConfidence( ( Confidence ) ConfidenceParser.getInstance().parse( child_element ) );
77             }
78         }
79         return seqRelation;
80     }
81
82     public static PhylogenyDataPhyloXmlParser getInstance( final Phylogeny phylogeny ) {
83         SequenceRelationParser instance = _instances.get( phylogeny );
84         if ( instance == null ) {
85             instance = new SequenceRelationParser();
86             instance._phylogeny = phylogeny;
87             _instances.put( phylogeny, instance );
88         }
89         return instance;
90     }
91 }