2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
20 import java.io.BufferedReader;
21 import java.io.FileNotFoundException;
22 import java.io.FileReader;
23 import java.io.IOException;
24 import java.util.ArrayList;
26 import jalview.analysis.SecStrConsensus.SimpleBP;
27 import jalview.datamodel.AlignmentAnnotation;
28 import jalview.datamodel.Annotation;
29 import jalview.datamodel.Sequence;
30 import jalview.datamodel.SequenceI;
32 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
33 import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
34 import fr.orsay.lri.varna.exceptions.ExceptionPermissionDenied;
35 import fr.orsay.lri.varna.factories.RNAFactory;
36 import fr.orsay.lri.varna.models.rna.RNA;
38 public class RnamlFile extends AlignFile
42 protected ArrayList<RNA> result;
50 public RnamlFile(String inFile, String type) throws IOException
56 public RnamlFile(FileParse source) throws IOException
62 public BufferedReader CreateReader() throws FileNotFoundException
65 fr = new FileReader(inFile);
67 BufferedReader r = new BufferedReader(fr);
74 * @see jalview.io.AlignFile#parse()
76 public void parse() throws IOException
78 if (System.getProperty("java.version").indexOf("1.6") > -1
79 || System.getProperty("java.version").indexOf("1.5") > -1)
81 // patch for 'This parser does not support specification "null" version
83 // this hack ensures we get a properly updated SAXParserFactory on older
85 // thanks to Stefan Birkner over at https://coderwall.com/p/kqsrrw
86 System.setProperty("javax.xml.parsers.SAXParserFactory",
87 "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl");
89 // rather than lose exception semantics whilst parsing RNAML with VARNA we
90 // wrap the routine and catch all exceptions before passing them up the
91 // chain as an IOException
95 } catch (ExceptionPermissionDenied pdx)
97 errormessage = "Couldn't access datasource (" + pdx.getMessage()
99 throw new IOException(pdx);
100 } catch (ExceptionLoadingFailed lf)
102 errormessage = "Couldn't process data as RNAML file ("
103 + lf.getMessage() + ")";
104 throw new IOException(lf);
105 } catch (ExceptionFileFormatOrSyntax iff)
107 errormessage = "Invalid RNAML file (" + iff.getMessage() + ")";
108 throw new IOException(iff);
109 } catch (Exception x)
112 errormessage = "Problem parsing data as RNAML (" + x.getMessage()
114 throw new IOException("Couldn't parse the datasource as RNAML", x);
118 @SuppressWarnings("unchecked")
119 public void _parse() throws FileNotFoundException,
120 ExceptionPermissionDenied, ExceptionLoadingFailed,
121 ExceptionFileFormatOrSyntax
124 result = RNAFactory.loadSecStrRNAML(getReader());
126 ArrayList<ArrayList> allarray = new ArrayList();
127 ArrayList<ArrayList<SimpleBP>> BP = new ArrayList();
128 ArrayList strucinarray = new ArrayList();
129 SequenceI[] seqs = new SequenceI[result.size()];
131 for (int i = 0; i < result.size(); i++)
134 RNA current = result.get(i);
135 String rna = current.getStructDBN(true);
136 String seq = current.getSeq();
138 int end = seq.length();
140 id = current.getName();
141 seqs[i] = new Sequence(id, seq, begin, end);
143 seqs[i].setEnd(seqs[i].findPosition(seqs[i].getLength()));
144 String[] annot = new String[rna.length()];
145 Annotation[] ann = new Annotation[rna.length()];
147 for (int j = 0; j < rna.length(); j++)
149 annot[j] = "" + rna.charAt(j);
152 for (int k = 0; k < rna.length(); k++)
154 ann[k] = new Annotation(annot[k], "",
155 jalview.schemes.ResidueProperties.getRNASecStrucState(
156 annot[k]).charAt(0), 0f);
159 AlignmentAnnotation align = new AlignmentAnnotation("Sec. str.",
160 current.getID(), ann);
162 seqs[i].addAlignmentAnnotation(align);
163 seqs[i].setRNA(result.get(i));
165 allarray.add(strucinarray);
167 annotations.addElement(align);
175 public static String print(SequenceI[] s)
177 return "not yet implemented";
180 public String print()
182 System.out.print("print :");
183 return print(getSeqsAsArray());
186 public ArrayList getRNA()
191 // public static void main(String[] args) {
192 // Pattern p= Pattern.compile("(.+)[.][^.]+");
193 // Matcher m = p.matcher("toto.xml.zip");
194 // System.out.println(m.matches());
195 // System.out.println(m.group(1));
198 * make a friendly ID string.
201 * @return truncated dataName to after last '/'
203 private String safeName(String dataName)
206 while ((b = dataName.indexOf("/")) > -1 && b < dataName.length())
208 dataName = dataName.substring(b + 1).trim();
211 int e = (dataName.length() - dataName.indexOf(".")) + 1;
212 dataName = dataName.substring(1, e).trim();