Clover icon

Coverage Report

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

File RnamlFile.java

 

Coverage histogram

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

Code metrics

20
59
9
1
242
160
25
0.42
6.56
9
2.78

Classes

Class Line # Actions
RnamlFile 46 59 25
0.681818268.2%
 

Contributing tests

This file is covered by 2 tests. .

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.analysis.Rna;
24    import jalview.datamodel.AlignmentAnnotation;
25    import jalview.datamodel.Annotation;
26    import jalview.datamodel.Sequence;
27    import jalview.datamodel.SequenceI;
28    import jalview.util.MessageManager;
29    import jalview.util.Platform;
30   
31    import java.io.BufferedReader;
32    import java.io.FileNotFoundException;
33    import java.io.FileReader;
34    import java.io.IOException;
35    import java.util.ArrayList;
36    import java.util.List;
37   
38    import com.stevesoft.pat.Regex;
39   
40    import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
41    import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
42    import fr.orsay.lri.varna.exceptions.ExceptionPermissionDenied;
43    import fr.orsay.lri.varna.factories.RNAFactory;
44    import fr.orsay.lri.varna.models.rna.RNA;
45   
 
46    public class RnamlFile extends AlignFile
47    {
48    public String id;
49   
50    protected ArrayList<RNA> result;
51   
 
52  0 toggle public RnamlFile()
53    {
54  0 super();
55   
56    }
57   
 
58  1 toggle public RnamlFile(String inFile, DataSourceType type) throws IOException
59    {
60  1 super(inFile, type);
61   
62    }
63   
 
64  1 toggle public RnamlFile(FileParse source) throws IOException
65    {
66  1 super(source);
67   
68    }
69   
 
70  0 toggle public BufferedReader CreateReader() throws FileNotFoundException
71    {
72  0 FileReader fr = null;
73  0 fr = new FileReader(inFile);
74   
75  0 BufferedReader r = new BufferedReader(fr);
76  0 return r;
77    }
78   
79    /*
80    * (non-Javadoc)
81    *
82    * @see jalview.io.AlignFile#parse()
83    */
 
84  2 toggle @Override
85    public void parse() throws IOException
86    {
87  2 if (System.getProperty("java.version").indexOf("1.6") > -1
88    || System.getProperty("java.version").indexOf("1.5") > -1)
89    {
90    // patch for 'This parser does not support specification "null" version
91    // "null"' error
92    // this hack ensures we get a properly updated SAXParserFactory on older
93    // JVMs
94    // thanks to Stefan Birkner over at https://coderwall.com/p/kqsrrw
95  0 System.setProperty("javax.xml.parsers.SAXParserFactory",
96    "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl");
97    }
98    // rather than lose exception semantics whilst parsing RNAML with VARNA we
99    // wrap the routine and catch all exceptions before passing them up the
100    // chain as an IOException
101  2 try
102    {
103  2 _parse();
104    } catch (ExceptionPermissionDenied pdx)
105    {
106  0 errormessage = MessageManager.formatMessage(
107    "exception.rnaml_couldnt_access_datasource", new String[]
108    { pdx.getMessage() });
109  0 throw new IOException(pdx);
110    } catch (ExceptionLoadingFailed lf)
111    {
112  0 errormessage = MessageManager.formatMessage(
113    "exception.ranml_couldnt_process_data", new String[]
114    { lf.getMessage() });
115  0 throw new IOException(lf);
116    } catch (ExceptionFileFormatOrSyntax iff)
117    {
118  0 errormessage = MessageManager
119    .formatMessage("exception.ranml_invalid_file", new String[]
120    { iff.getMessage() });
121  0 throw new IOException(iff);
122    } catch (Exception x)
123    {
124  0 error = true;
125  0 errormessage = MessageManager.formatMessage(
126    "exception.ranml_problem_parsing_data", new String[]
127    { x.getMessage() });
128  0 throw new IOException(errormessage, x);
129    }
130    }
131   
 
132  2 toggle @SuppressWarnings("unchecked")
133    public void _parse()
134    throws FileNotFoundException, ExceptionPermissionDenied,
135    ExceptionLoadingFailed, ExceptionFileFormatOrSyntax
136    {
137   
138  2 result = RNAFactory.loadSecStrRNAML(getReader());
139   
140    // ArrayList<ArrayList> allarray = new ArrayList();
141    // ArrayList<ArrayList<SimpleBP>> BP = new ArrayList();
142    // ArrayList strucinarray = new ArrayList();
143  2 SequenceI[] sqs = new SequenceI[result.size()];
144   
145  5 for (int i = 0; i < result.size(); i++)
146    {
147   
148  3 RNA current = result.get(i);
149  3 String rna = current.getStructDBN(true);
150  3 String seq = current.getSeq();
151  3 int begin = 1;
152  3 int end = seq.length();
153   
154  3 id = current.getName();
155  3 if (id == null || id.trim().length() == 0)
156    {
157  3 id = safeName(getDataName());
158  3 if (result.size() > 1)
159    {
160  2 id += "." + i;
161    }
162    }
163  3 sqs[i] = new Sequence(id, seq, begin, end);
164  3 if (seq.length() != (end - begin + 1))
165    {
166  0 sqs[i].setEnd(sqs[i].findPosition(sqs[i].getLength()));
167    }
168  3 String[] annot = new String[rna.length()];
169  3 Annotation[] ann = new Annotation[rna.length()];
170   
171  566 for (int j = 0; j < rna.length(); j++)
172    {
173  563 annot[j] = "" + rna.charAt(j);
174   
175    }
176  566 for (int k = 0; k < rna.length(); k++)
177    {
178  563 ann[k] = new Annotation(annot[k], "",
179    Rna.getRNASecStrucState(annot[k]).charAt(0), 0f);
180    }
181   
182  3 AlignmentAnnotation align = new AlignmentAnnotation(
183    "Secondary Structure",
184  3 current.getID().trim().length() > 0
185    ? "Secondary Structure for " + current.getID()
186    : "",
187    ann);
188   
189  3 sqs[i].addAlignmentAnnotation(align);
190  3 sqs[i].setRNA(result.get(i));
191   
192    // allarray.add(strucinarray);
193   
194  3 annotations.addElement(align);
195    // BP.add(align.bps);
196   
197    }
198   
199  2 setSeqs(sqs);
200    }
201   
 
202  0 toggle @Override
203    public String print(SequenceI[] s, boolean jvSuffix)
204    {
205  0 return "not yet implemented";
206    }
207   
 
208  0 toggle public List<RNA> getRNA()
209    {
210  0 return result;
211    }
212   
213    // public static void main(String[] args) {
214    // Pattern p= Pattern.compile("(.+)[.][^.]+");
215    // Matcher m = p.matcher("toto.xml.zip");
216    // jalview.bin.Console.outPrintln(m.matches());
217    // jalview.bin.Console.outPrintln(m.group(1));
218    // }
219    /**
220    * make a friendly ID string.
221    *
222    * @param dataName
223    * @return truncated dataName to after last '/'
224    */
 
225  3 toggle private String safeName(String dataName)
226    {
227  3 int b = 0;
228  ? if ((b = dataName.lastIndexOf(".")) > 0)
229    {
230  3 dataName = dataName.substring(0, b - 1);
231    }
232  3 b = 0;
233  3 Regex m = Platform.newRegex("[\\/]?([-A-Za-z0-9]+)\\.?");
234  3 String mm = dataName;
235  12 while (m.searchFrom(dataName, b))
236    {
237  9 mm = m.stringMatched();
238  9 b = m.matchedTo();
239    }
240  3 return mm;
241    }
242    }