Clover icon

Coverage Report

  1. Project Clover database Thu Dec 4 2025 14:43:25 GMT
  2. Package jalview.io

File BSMLFile.java

 

Coverage histogram

../../img/srcFileCovDistChart7.png
30% of files have more coverage

Code metrics

14
71
7
1
225
161
18
0.25
10.14
7
2.57

Classes

Class Line # Actions
BSMLFile 56 71 18
0.684782668.5%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
7    * Jalview is free software: you can redistribute it and/or
8    * modify it under the terms of the GNU General Public License
9    * as published by the Free Software Foundation, either version 3
10    * of the License, or (at your option) any later version.
11    *
12    * Jalview is distributed in the hope that it will be useful, but
13    * WITHOUT ANY WARRANTY; without even the implied warranty
14    * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15    * PURPOSE. See the GNU General Public License for more details.
16    *
17    * You should have received a copy of the GNU General Public License
18    * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19    * The Jalview Authors are detailed in the 'AUTHORS' file.
20    */
21    package jalview.io;
22   
23    import jalview.datamodel.Sequence;
24    import jalview.datamodel.SequenceFeature;
25    import jalview.datamodel.SequenceI;
26    import jalview.util.MessageManager;
27   
28    import java.io.BufferedReader;
29    import java.io.FileNotFoundException;
30    import java.io.FileReader;
31    import java.io.IOException;
32    import java.util.Hashtable;
33    import java.util.Map;
34   
35    import javax.xml.parsers.DocumentBuilder;
36    import javax.xml.parsers.DocumentBuilderFactory;
37    import javax.xml.parsers.ParserConfigurationException;
38   
39    import org.w3c.dom.Document;
40    import org.w3c.dom.Element;
41    import org.w3c.dom.NodeList;
42    import org.xml.sax.InputSource;
43    import org.xml.sax.SAXException;
44   
45    import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
46    import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
47    import fr.orsay.lri.varna.exceptions.ExceptionPermissionDenied;
48   
49    /**
50    * Preliminary reader for Bioinformatics Sequence Markup Language
51    * http://www.bsml.org
52    *
53    * @author hansonr
54    *
55    */
 
56    public class BSMLFile extends AlignFile
57    {
58   
 
59  0 toggle public BSMLFile()
60    {
61  0 super();
62   
63    }
64   
 
65  1 toggle public BSMLFile(String inFile, DataSourceType type) throws IOException
66    {
67  1 super(inFile, type);
68   
69    }
70   
 
71  0 toggle public BSMLFile(FileParse source) throws IOException
72    {
73  0 super(source);
74   
75    }
76   
 
77  0 toggle public BufferedReader CreateReader() throws FileNotFoundException
78    {
79  0 FileReader fr = null;
80  0 fr = new FileReader(inFile);
81   
82  0 BufferedReader r = new BufferedReader(fr);
83  0 return r;
84    }
85   
86    /*
87    * (non-Javadoc)
88    *
89    * @see jalview.io.AlignFile#parse()
90    */
 
91  1 toggle @Override
92    public void parse() throws IOException
93    {
94  1 try
95    {
96  1 _parse();
97    } catch (ExceptionPermissionDenied pdx)
98    {
99  0 errormessage = MessageManager.formatMessage(
100    "exception.BSML_couldnt_access_datasource", new String[]
101    { pdx.getMessage() });
102  0 throw new IOException(pdx);
103    } catch (ExceptionLoadingFailed lf)
104    {
105  0 errormessage = MessageManager.formatMessage(
106    "exception.BSML_couldnt_process_data", new String[]
107    { lf.getMessage() });
108  0 throw new IOException(lf);
109    } catch (ExceptionFileFormatOrSyntax iff)
110    {
111  0 errormessage = MessageManager
112    .formatMessage("exception.BSML_invalid_file", new String[]
113    { iff.getMessage() });
114  0 throw new IOException(iff);
115    } catch (Exception x)
116    {
117  0 error = true;
118  0 errormessage = MessageManager.formatMessage(
119    "exception.BSML_problem_parsing_data", new String[]
120    { x.getMessage() });
121  0 throw new IOException(errormessage, x);
122    }
123    }
124   
 
125  1 toggle @SuppressWarnings("unchecked")
126    public void _parse()
127    throws ExceptionPermissionDenied, ExceptionLoadingFailed,
128    ExceptionFileFormatOrSyntax, ParserConfigurationException,
129    SAXException, IOException
130    {
131   
132  1 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
133   
134  1 dbf.setIgnoringElementContentWhitespace(true);
135  1 dbf.setIgnoringComments(true);
136  1 dbf.setValidating(true);
137  1 dbf.setCoalescing(true);
138  1 dbf.setNamespaceAware(true);
139  1 dbf.setFeature("http://xml.org/sax/features/namespaces", false);
140  1 dbf.setFeature("http://xml.org/sax/features/validation", false);
141  1 dbf.setFeature(
142    "http://apache.org/xml/features/nonvalidating/load-dtd-grammar",
143    false);
144  1 dbf.setFeature(
145    "http://apache.org/xml/features/nonvalidating/load-external-dtd",
146    false);
147   
148  1 DocumentBuilder db = dbf.newDocumentBuilder();
149   
150  1 Map<String, SequenceI> htSeq = new Hashtable<>();
151  1 InputSource is = new InputSource(getReader());
152  1 Document d = db.parse(is);
153  1 NodeList sequences = d.getElementsByTagName("Sequence-data");
154  1 int n = sequences.getLength();
155  1 SequenceI[] sqs = new SequenceI[n];
156  2 for (int i = 0; i < n; i++)
157    {
158  1 Element e = (Element) sequences.item(i);
159  1 String s = e.getTextContent();
160  1 String id = e.getAttribute("seq-name");
161  1 SequenceI seq = sqs[i] = new Sequence(id, s, 1, s.length());
162  1 htSeq.put(id, seq);
163    // ?? sqs[i].setEnd(sqs[i].findPosition(sqs[i].getLength()));
164    }
165   
166  1 sequences = d.getElementsByTagName("Sequence");
167  1 n = sequences.getLength();
168  2 for (int i = 0; i < n; i++)
169    {
170  1 Element e = (Element) sequences.item(i);
171  1 String mol = e.getAttribute("molecule"); // dna or rna
172  1 if (!"dna".equals(mol))
173    {
174  0 System.err.println("BSML molecule=rna not implemented");
175  0 continue;
176    }
177  1 String title = e.getAttribute("title");
178  1 SequenceI seq = htSeq.get(title);
179  1 if (seq == null)
180    {
181  0 continue;
182    }
183  1 NodeList features = e.getElementsByTagName("Feature");
184  1 int featureCount = features.getLength();
185  2 for (int f = 0; f < featureCount; f++)
186    {
187  1 Element feature = (Element) features.item(f);
188    // <Feature class="GENE" title="CPXV-GER_1980_EP4-211">
189    // <Interval-loc complement="0" endpos="217104" startpos="216643"/>
190    // <Resource id="GENE-ID:119705"/>
191    // </Feature>
192  1 Element iloc = (Element) feature
193    .getElementsByTagName("Interval-loc").item(0);
194  1 String complement = iloc.getAttribute("complement");
195  1 if (!"0".equals(complement))
196    {
197    // Jalview cannot handle complement genes (running backward on the
198    // complementary strand);
199  0 continue;
200    }
201  1 String fclass = feature.getAttribute("class");
202  1 if (!"GENE".equals(fclass))
203    {
204    // just processing GENE features for now;
205  0 continue;
206    }
207  1 String ftitle = feature.getAttribute("title");
208  1 int start = Integer.parseInt(iloc.getAttribute("startpos"));
209  1 int end = Integer.parseInt(iloc.getAttribute("endpos"));
210  1 SequenceFeature sf = new SequenceFeature("GENE", ftitle, start, end,
211    null);
212  1 seq.addSequenceFeature(sf);
213    }
214  1 setSeqs(sqs);
215    }
216   
217    }
218   
 
219  0 toggle @Override
220    public String print(SequenceI[] s, boolean jvSuffix)
221    {
222  0 return "not yet implemented";
223    }
224   
225    }