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;
25 import java.util.Vector;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
29 import jalview.analysis.SecStrConsensus;
30 import jalview.analysis.SecStrConsensus.SimpleBP;
31 import jalview.datamodel.AlignmentAnnotation;
32 import jalview.datamodel.Annotation;
33 import jalview.datamodel.Sequence;
34 import jalview.datamodel.SequenceI;
36 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
37 import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
38 import fr.orsay.lri.varna.exceptions.ExceptionPermissionDenied;
39 import fr.orsay.lri.varna.factories.RNAFactory;
40 import fr.orsay.lri.varna.models.rna.RNA;
46 public class RnamlFile extends AlignFile {
48 public String namefile;
50 protected ArrayList<RNA> result;
57 public RnamlFile(String inFile, String type) throws Exception {
62 public RnamlFile(FileParse source) throws Exception {
67 public BufferedReader CreateReader() throws FileNotFoundException {
69 fr = new FileReader(inFile);
71 BufferedReader r = new BufferedReader(fr);
76 * @see jalview.io.AlignFile#parse()
78 public void parse() throws IOException
80 // rather than lose exception semantics whilst parsing RNAML with VARNA we
81 // wrap the routine and catch all exceptions before passing them up the
82 // chain as an IOException
88 errormessage="Problem parsing data as RNAML ("+x.getMessage()+")";
89 throw new IOException("Couldn't parse the datasource as RNAML",x);
92 @SuppressWarnings("unchecked")
93 public void _parse() throws FileNotFoundException,
94 ExceptionPermissionDenied, ExceptionLoadingFailed,
95 ExceptionFileFormatOrSyntax {
97 result = RNAFactory.loadSecStrRNAML(getReader());
99 ArrayList<ArrayList> allarray = new ArrayList();
100 ArrayList<ArrayList<SimpleBP>> BP = new ArrayList();
101 ArrayList strucinarray = new ArrayList();
102 SequenceI[] seqs = new SequenceI[result.size()];
103 namefile = inFile.getName();
105 for (int i = 0; i < result.size(); i++) {
107 RNA current = result.get(i);
108 String rna = current.getStructDBN(true);
109 String seq = current.getSeq();
113 System.out.println("id=" + i);
114 name = this.safeName(namefile, i);
115 seqs[i] = new Sequence(name, seq, begin, end);
117 seqs[i].setEnd(seqs[i].findPosition(seqs[i].getLength()));
118 String[] annot = new String[rna.length()];
119 Annotation[] ann = new Annotation[rna.length()];
121 for (int j = 0; j < rna.length(); j++) {
122 annot[j] = "" + rna.charAt(j);
125 for (int k = 0; k < rna.length(); k++) {
126 ann[k] = new Annotation(annot[k], "",
127 jalview.schemes.ResidueProperties.getRNASecStrucState(
128 annot[k]).charAt(0), 0f);
131 AlignmentAnnotation align = new AlignmentAnnotation("Sec. str.",
132 current.getID(), ann);
134 seqs[i].addAlignmentAnnotation(align);
135 seqs[i].setRNA(result.get(i));
137 char[] struc = align.getRNAStruc().toCharArray();
139 allarray.add(strucinarray);
141 this.annotations.addElement(align);
146 int[] tab = SecStrConsensus.extractConsensus(BP);
151 public void parse(BufferedReader r) throws ExceptionPermissionDenied,
152 ExceptionLoadingFailed, ExceptionFileFormatOrSyntax {
154 result = RNAFactory.loadSecStrRNAML(r);
156 ArrayList<ArrayList> allarray = new ArrayList();
157 ArrayList<ArrayList<SimpleBP>> BP = new ArrayList();
158 ArrayList strucinarray = new ArrayList();
159 SequenceI[] seqs = new SequenceI[result.size()];
160 namefile = inFile.getName();
162 for (int i = 0; i < result.size(); i++) {
164 RNA current = result.get(i);
165 String rna = current.getStructDBN(true);
167 String seq = current.getSeq();
171 name = this.safeName(namefile, i);
172 seqs[i] = new Sequence(name, seq, begin, end);
174 seqs[i].setEnd(seqs[i].findPosition(seqs[i].getLength()));
175 String[] annot = new String[rna.length()];
176 Annotation[] ann = new Annotation[rna.length()];
178 for (int j = 0; j < rna.length(); j++) {
179 annot[j] = "" + rna.charAt(j);
182 for (int k = 0; k < rna.length(); k++) {
183 ann[k] = new Annotation(annot[k], "",
184 jalview.schemes.ResidueProperties.getRNASecStrucState(
185 annot[k]).charAt(0), 0f);
188 AlignmentAnnotation align = new AlignmentAnnotation("Sec. str.",
189 current.getID(), ann);
191 seqs[i].addAlignmentAnnotation(align);
192 seqs[i].setRNA(result.get(i));
194 char[] struc = align.getRNAStruc().toCharArray();
195 for (int y = 0; y < struc.length; y++) {
196 strucinarray.add(struc[y]);
197 // System.out.println("structy"+struc[y]);
199 // System.out.println("Sequence"+i+"\t : "+strucinarray.toString());
200 allarray.add(strucinarray);
202 this.annotations.addElement(align);
203 System.out.println(rna);
205 System.out.println(align.bps.size());
208 int[] tab = SecStrConsensus.extractConsensus(BP);
215 public static String print(SequenceI[] s) {
216 return "not yet implemented";
219 public String print() {
220 System.out.print("print :");
221 return print(getSeqsAsArray());
224 public ArrayList getRNA() {
228 public Vector getAnnot() {
232 // public static void main(String[] args) {
233 // Pattern p= Pattern.compile("(.+)[.][^.]+");
234 // Matcher m = p.matcher("toto.xml.zip");
235 // System.out.println(m.matches());
236 // System.out.println(m.group(1));
238 public String safeName(String namefile, int id) {
240 Pattern p = Pattern.compile("(.*)[.][^.]+");
241 Matcher m = p.matcher(namefile);
242 name = m.group(1) + "_" + id;