JAL-1140 - handle VARNA specific RNA exceptions within the IO class and generate...
[jalview.git] / src / jalview / io / RnamlFile.java
1 /*
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
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 of the License, or (at your option) any later version.
10  *  
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.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.io;
19
20 import jalview.datamodel.AlignmentAnnotation;
21 import jalview.datamodel.Annotation;
22 import jalview.datamodel.Sequence;
23 import jalview.datamodel.SequenceI;
24
25 import java.io.BufferedReader;
26 import java.io.FileNotFoundException;
27 import java.io.FileReader;
28 import java.io.IOException;
29 import java.util.ArrayList;
30
31 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
32 import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
33 import fr.orsay.lri.varna.exceptions.ExceptionPermissionDenied;
34 import fr.orsay.lri.varna.factories.RNAFactory;
35 import fr.orsay.lri.varna.models.rna.RNA;
36
37 public class RnamlFile extends AlignFile
38 {
39   public String id;
40
41   protected ArrayList<RNA> result;
42
43   public RnamlFile()
44   {
45     super();
46
47   }
48
49   public RnamlFile(String inFile, String type) throws IOException
50   {
51     super(inFile, type);
52
53   }
54
55   public RnamlFile(FileParse source) throws IOException
56   {
57     super(source);
58
59   }
60
61   // public RnamlFile(BufferedReader r) throws IOException,
62   // ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException,
63   // ExceptionPermissionDenied, ExceptionLoadingFailed
64   // {
65   // super();
66   // parse(r);
67   // // sets the index of each sequence in the alignment
68   // for( int i=0,c=seqs.size(); i<c; i++ ) {
69   // seqs.get(i).setIndex(i);
70   // }
71   //
72   //
73   // }
74
75   public BufferedReader CreateReader() throws FileNotFoundException
76   {
77     FileReader fr = null;
78     fr = new FileReader(inFile);
79
80     BufferedReader r = new BufferedReader(fr);
81     return r;
82   }
83
84   public void parse() throws IOException
85   {
86     result = null;
87     try
88     {
89       result = RNAFactory.loadSecStrRNAML(getReader());
90
91     } catch (ExceptionPermissionDenied pdx)
92     {
93       errormessage = "Couldn't access datasource (" + pdx.getMessage()
94               + ")";
95       throw new IOException(pdx);
96     } catch (ExceptionLoadingFailed lf)
97     {
98       errormessage = "Couldn't process data as RNAML file ("
99               + lf.getMessage() + ")";
100       throw new IOException(lf);
101     } catch (ExceptionFileFormatOrSyntax iff)
102     {
103       errormessage = "Invalid RNAML file (" + iff.getMessage() + ")";
104       throw new IOException(iff);
105     }
106
107     SequenceI[] seqs = new SequenceI[result.size()];
108
109     for (int i = 0; i < result.size(); i++)
110     {
111       // DEBUG System.err.println("RNAML entry "+i);
112       RNA current = result.get(i);
113       String seq = current.getSeq();
114       // DEBUG System.err.println(seq);
115       String rna = current.getStructDBN(true);
116       // DEBUG System.err.println(rna);
117
118       int begin = 1;
119       int end = seq.length(); // TODO: compute non-gapped length for sequence
120
121       id = current.getName();
122       seqs[i] = new Sequence(id, seq, begin, end);
123
124       String[] annot = new String[rna.length()];
125       Annotation[] ann = new Annotation[rna.length()];
126
127       for (int j = 0; j < rna.length(); j++)
128       {
129         annot[j] = rna.substring(j, j + 1);
130
131       }
132
133       for (int k = 0; k < rna.length(); k++)
134       {
135         ann[k] = new Annotation(annot[k], "",
136                 jalview.schemes.ResidueProperties.getRNASecStrucState(
137                         annot[k]).charAt(0), 0f);
138
139       }
140       AlignmentAnnotation align = new AlignmentAnnotation("Sec. str.",
141               current.getID(), ann);
142
143       seqs[i].addAlignmentAnnotation(align);
144       seqs[i].setRNA(result.get(i));
145       annotations.addElement(align);
146     }
147     setSeqs(seqs);
148   }
149
150   public static String print(SequenceI[] s)
151   {
152     // TODO: implement RNAML export option
153     return "not yet implemented";
154   }
155
156   public String print()
157   {
158     return print(getSeqsAsArray());
159   }
160
161   /**
162    * make a friendly ID string.
163    * 
164    * @param dataName
165    * @return truncated dataName to after last '/'
166    */
167   private String safeName(String dataName)
168   {
169     int b = 0;
170     while ((b = dataName.indexOf("/")) > -1 && b < dataName.length())
171     {
172       dataName = dataName.substring(b + 1).trim();
173
174     }
175     int e = (dataName.length() - dataName.indexOf(".")) + 1;
176     dataName = dataName.substring(1, e).trim();
177     return dataName;
178   }
179 }