inprogress
[jalview.git] / forester / java / src / org / forester / io / parsers / phyloxml / data / SequenceParser.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 org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
29 import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
30 import org.forester.io.parsers.phyloxml.XmlElement;
31 import org.forester.phylogeny.data.Accession;
32 import org.forester.phylogeny.data.Annotation;
33 import org.forester.phylogeny.data.DomainArchitecture;
34 import org.forester.phylogeny.data.Sequence;
35 import org.forester.phylogeny.data.Uri;
36
37 public class SequenceParser implements PhylogenyDataPhyloXmlParser {
38
39     private static final PhylogenyDataPhyloXmlParser _instance;
40     static {
41         try {
42             _instance = new SequenceParser();
43         }
44         catch ( final Throwable e ) {
45             throw new RuntimeException( e.getMessage() );
46         }
47     }
48
49     private SequenceParser() {
50     }
51
52     @Override
53     public Sequence parse( final XmlElement element ) throws PhyloXmlDataFormatException {
54         final Sequence sequence = new Sequence();
55         if ( element.isHasAttribute( PhyloXmlMapping.SEQUENCE_TYPE ) ) {
56             sequence.setType( element.getAttribute( PhyloXmlMapping.SEQUENCE_TYPE ) );
57         }
58         if ( element.isHasAttribute( PhyloXmlMapping.SEQUENCE_SOURCE_ID ) ) {
59             sequence.setSourceId( element.getAttribute( PhyloXmlMapping.SEQUENCE_SOURCE_ID ) );
60         }
61         for( int i = 0; i < element.getNumberOfChildElements(); ++i ) {
62             final XmlElement child_element = element.getChildElement( i );
63             if ( child_element.getQualifiedName().equals( PhyloXmlMapping.SEQUENCE_LOCATION ) ) {
64                 sequence.setLocation( child_element.getValueAsString() );
65             }
66             else if ( child_element.getQualifiedName().equals( PhyloXmlMapping.SEQUENCE_NAME ) ) {
67                 sequence.setName( child_element.getValueAsString() );
68             }
69             else if ( child_element.getQualifiedName().equals( PhyloXmlMapping.SEQUENCE_GENE_NAME ) ) {
70                 sequence.setGeneName( child_element.getValueAsString() );
71             }
72             else if ( child_element.getQualifiedName().equals( PhyloXmlMapping.SEQUENCE_MOL_SEQ ) ) {
73                 if ( child_element.isHasAttribute( PhyloXmlMapping.SEQUENCE_MOL_SEQ_ALIGNED_ATTR ) ) {
74                     sequence.setMolecularSequenceAligned( Boolean.parseBoolean( child_element
75                             .getAttribute( PhyloXmlMapping.SEQUENCE_MOL_SEQ_ALIGNED_ATTR ) ) );
76                 }
77                 sequence.setMolecularSequence( child_element.getValueAsString() );
78             }
79             else if ( child_element.getQualifiedName().equals( PhyloXmlMapping.ACCESSION ) ) {
80                 sequence.setAccession( ( Accession ) AccessionParser.getInstance().parse( child_element ) );
81             }
82             else if ( child_element.getQualifiedName().equals( PhyloXmlMapping.SEQUENCE_SYMBOL ) ) {
83                 sequence.setSymbol( child_element.getValueAsString() );
84             }
85             else if ( child_element.getQualifiedName().equals( PhyloXmlMapping.ANNOTATION ) ) {
86                 sequence.addAnnotation( ( Annotation ) AnnotationParser.getInstance().parse( child_element ) );
87             }
88             else if ( child_element.getQualifiedName().equals( PhyloXmlMapping.SEQUENCE_DOMAIN_ARCHITECURE ) ) {
89                 sequence.setDomainArchitecture( ( DomainArchitecture ) DomainArchitectureParser.getInstance()
90                         .parse( child_element ) );
91             }
92             else if ( child_element.getQualifiedName().equals( PhyloXmlMapping.URI ) ) {
93                 sequence.addUri( ( Uri ) UriParser.getInstance().parse( child_element ) );
94             }
95             else if ( child_element.getQualifiedName().equals( PhyloXmlMapping.SEQUENCE_X_REFS ) ) {
96                 for( int j = 0; j < child_element.getNumberOfChildElements(); ++j ) {
97                     //    final XmlElement c = child_element.getChildElement( j );
98                     sequence.addCrossReference( ( Accession ) AccessionParser.getInstance().parse( child_element
99                             .getChildElement( j ) ) );
100                 }
101                 //sequence.addUri( ( Uri ) UriParser.getInstance().parse( child_element ) );
102             }
103         }
104         return sequence;
105     }
106
107     public static PhylogenyDataPhyloXmlParser getInstance() {
108         return _instance;
109     }
110 }