2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
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;
30 import java.io.BufferedReader;
31 import java.io.FileNotFoundException;
32 import java.io.FileReader;
33 import java.io.IOException;
34 import java.util.ArrayList;
35 import java.util.List;
37 import com.stevesoft.pat.Regex;
39 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
40 import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
41 import fr.orsay.lri.varna.exceptions.ExceptionPermissionDenied;
42 import fr.orsay.lri.varna.factories.RNAFactory;
43 import fr.orsay.lri.varna.models.rna.RNA;
45 public class RnamlFile extends AlignFile
49 protected ArrayList<RNA> result;
57 public RnamlFile(String inFile, String type) throws IOException
63 public RnamlFile(FileParse source) throws IOException
69 public BufferedReader CreateReader() throws FileNotFoundException
72 fr = new FileReader(inFile);
74 BufferedReader r = new BufferedReader(fr);
81 * @see jalview.io.AlignFile#parse()
84 public void parse() throws IOException
86 if (System.getProperty("java.version").indexOf("1.6") > -1
87 || System.getProperty("java.version").indexOf("1.5") > -1)
89 // patch for 'This parser does not support specification "null" version
91 // this hack ensures we get a properly updated SAXParserFactory on older
93 // thanks to Stefan Birkner over at https://coderwall.com/p/kqsrrw
94 System.setProperty("javax.xml.parsers.SAXParserFactory",
95 "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl");
97 // rather than lose exception semantics whilst parsing RNAML with VARNA we
98 // wrap the routine and catch all exceptions before passing them up the
99 // chain as an IOException
103 } catch (ExceptionPermissionDenied pdx)
105 errormessage = MessageManager.formatMessage(
106 "exception.rnaml_couldnt_access_datasource",
107 new String[] { pdx.getMessage() });
108 throw new IOException(pdx);
109 } catch (ExceptionLoadingFailed lf)
111 errormessage = MessageManager.formatMessage(
112 "exception.ranml_couldnt_process_data",
113 new String[] { lf.getMessage() });
114 throw new IOException(lf);
115 } catch (ExceptionFileFormatOrSyntax iff)
117 errormessage = MessageManager.formatMessage(
118 "exception.ranml_invalid_file",
119 new String[] { iff.getMessage() });
120 throw new IOException(iff);
121 } catch (Exception x)
124 errormessage = MessageManager.formatMessage(
125 "exception.ranml_problem_parsing_data",
126 new String[] { x.getMessage() });
127 throw new IOException(errormessage, x);
131 @SuppressWarnings("unchecked")
132 public void _parse() throws FileNotFoundException,
133 ExceptionPermissionDenied, ExceptionLoadingFailed,
134 ExceptionFileFormatOrSyntax
137 result = RNAFactory.loadSecStrRNAML(getReader());
139 // ArrayList<ArrayList> allarray = new ArrayList();
140 // ArrayList<ArrayList<SimpleBP>> BP = new ArrayList();
141 // ArrayList strucinarray = new ArrayList();
142 SequenceI[] sqs = new SequenceI[result.size()];
144 for (int i = 0; i < result.size(); i++)
147 RNA current = result.get(i);
148 String rna = current.getStructDBN(true);
149 String seq = current.getSeq();
151 int end = seq.length();
153 id = current.getName();
154 if (id == null || id.trim().length() == 0)
156 id = safeName(getDataName());
157 if (result.size() > 1)
162 sqs[i] = new Sequence(id, seq, begin, end);
164 sqs[i].setEnd(sqs[i].findPosition(sqs[i].getLength()));
165 String[] annot = new String[rna.length()];
166 Annotation[] ann = new Annotation[rna.length()];
168 for (int j = 0; j < rna.length(); j++)
170 annot[j] = "" + rna.charAt(j);
173 for (int k = 0; k < rna.length(); k++)
175 ann[k] = new Annotation(annot[k], "", Rna.getRNASecStrucState(
176 annot[k]).charAt(0), 0f);
179 AlignmentAnnotation align = new AlignmentAnnotation(
180 "Secondary Structure",
181 current.getID().trim().length() > 0 ? "Secondary Structure for "
185 sqs[i].addAlignmentAnnotation(align);
186 sqs[i].setRNA(result.get(i));
188 // allarray.add(strucinarray);
190 annotations.addElement(align);
191 // BP.add(align.bps);
198 public static String print(SequenceI[] s)
200 return "not yet implemented";
204 public String print()
206 System.out.print("print :");
207 return print(getSeqsAsArray());
210 public List<RNA> getRNA()
215 // public static void main(String[] args) {
216 // Pattern p= Pattern.compile("(.+)[.][^.]+");
217 // Matcher m = p.matcher("toto.xml.zip");
218 // System.out.println(m.matches());
219 // System.out.println(m.group(1));
222 * make a friendly ID string.
225 * @return truncated dataName to after last '/'
227 private String safeName(String dataName)
230 if ((b = dataName.lastIndexOf(".")) > 0)
232 dataName = dataName.substring(0, b - 1);
235 Regex m = new Regex("[\\/]?([-A-Za-z0-9]+)\\.?");
236 String mm = dataName;
237 while (m.searchFrom(dataName, b))
239 mm = m.stringMatched();