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.SecStrConsensus.SimpleBP;
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;
36 import com.stevesoft.pat.Regex;
38 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
39 import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
40 import fr.orsay.lri.varna.exceptions.ExceptionPermissionDenied;
41 import fr.orsay.lri.varna.factories.RNAFactory;
42 import fr.orsay.lri.varna.models.rna.RNA;
44 public class RnamlFile extends AlignFile
48 protected ArrayList<RNA> result;
56 public RnamlFile(String inFile, String type) throws IOException
62 public RnamlFile(FileParse source) throws IOException
68 public BufferedReader CreateReader() throws FileNotFoundException
71 fr = new FileReader(inFile);
73 BufferedReader r = new BufferedReader(fr);
80 * @see jalview.io.AlignFile#parse()
82 public void parse() throws IOException
84 if (System.getProperty("java.version").indexOf("1.6") > -1
85 || System.getProperty("java.version").indexOf("1.5") > -1)
87 // patch for 'This parser does not support specification "null" version
89 // this hack ensures we get a properly updated SAXParserFactory on older
91 // thanks to Stefan Birkner over at https://coderwall.com/p/kqsrrw
92 System.setProperty("javax.xml.parsers.SAXParserFactory",
93 "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl");
95 // rather than lose exception semantics whilst parsing RNAML with VARNA we
96 // wrap the routine and catch all exceptions before passing them up the
97 // chain as an IOException
101 } catch (ExceptionPermissionDenied pdx)
103 errormessage = MessageManager.formatMessage("exception.rnaml_couldnt_access_datasource", new String[]{pdx.getMessage()});
104 throw new IOException(pdx);
105 } catch (ExceptionLoadingFailed lf)
107 errormessage = MessageManager.formatMessage("exception.ranml_couldnt_process_data", new String[]{lf.getMessage()});
108 throw new IOException(lf);
109 } catch (ExceptionFileFormatOrSyntax iff)
111 errormessage = MessageManager.formatMessage("exception.ranml_invalid_file", new String[]{iff.getMessage()});
112 throw new IOException(iff);
113 } catch (Exception x)
116 errormessage = MessageManager.formatMessage("exception.ranml_problem_parsing_data", new String[]{x.getMessage()});
117 throw new IOException(errormessage , x);
121 @SuppressWarnings("unchecked")
122 public void _parse() throws FileNotFoundException,
123 ExceptionPermissionDenied, ExceptionLoadingFailed,
124 ExceptionFileFormatOrSyntax
127 result = RNAFactory.loadSecStrRNAML(getReader());
129 ArrayList<ArrayList> allarray = new ArrayList();
130 ArrayList<ArrayList<SimpleBP>> BP = new ArrayList();
131 ArrayList strucinarray = new ArrayList();
132 SequenceI[] seqs = new SequenceI[result.size()];
134 for (int i = 0; i < result.size(); i++)
137 RNA current = result.get(i);
138 String rna = current.getStructDBN(true);
139 String seq = current.getSeq();
141 int end = seq.length();
143 id = current.getName();
144 if (id == null || id.trim().length() == 0)
146 id = safeName(getDataName());
147 if (result.size() > 1)
152 seqs[i] = new Sequence(id, seq, begin, end);
154 seqs[i].setEnd(seqs[i].findPosition(seqs[i].getLength()));
155 String[] annot = new String[rna.length()];
156 Annotation[] ann = new Annotation[rna.length()];
158 for (int j = 0; j < rna.length(); j++)
160 annot[j] = "" + rna.charAt(j);
163 for (int k = 0; k < rna.length(); k++)
165 ann[k] = new Annotation(annot[k], "",
166 jalview.schemes.ResidueProperties.getRNASecStrucState(
167 annot[k]).charAt(0), 0f);
170 AlignmentAnnotation align = new AlignmentAnnotation(
171 "Secondary Structure",
172 current.getID().trim().length() > 0 ? "Secondary Structure for "
176 seqs[i].addAlignmentAnnotation(align);
177 seqs[i].setRNA(result.get(i));
179 allarray.add(strucinarray);
181 annotations.addElement(align);
189 public static String print(SequenceI[] s)
191 return "not yet implemented";
194 public String print()
196 System.out.print("print :");
197 return print(getSeqsAsArray());
200 public ArrayList getRNA()
205 // public static void main(String[] args) {
206 // Pattern p= Pattern.compile("(.+)[.][^.]+");
207 // Matcher m = p.matcher("toto.xml.zip");
208 // System.out.println(m.matches());
209 // System.out.println(m.group(1));
212 * make a friendly ID string.
215 * @return truncated dataName to after last '/'
217 private String safeName(String dataName)
220 if ((b = dataName.lastIndexOf(".")) > 0)
222 dataName = dataName.substring(0, b - 1);
225 Regex m = new Regex("[\\/]?([-A-Za-z0-9]+)\\.?");
226 String mm = dataName;
227 while (m.searchFrom(dataName, b))
229 mm = m.stringMatched();