JAL-1807 explicit imports (jalview.io.*)
[jalview.git] / src / jalview / io / RnamlFile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10  * of the License, or (at your option) any later version.
11  *  
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.
16  * 
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.
20  */
21 package jalview.io;
22
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.schemes.ResidueProperties;
29 import jalview.util.MessageManager;
30
31 import java.io.BufferedReader;
32 import java.io.FileNotFoundException;
33 import java.io.FileReader;
34 import java.io.IOException;
35 import java.util.ArrayList;
36
37 import com.stevesoft.pat.Regex;
38
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;
44
45 public class RnamlFile extends AlignFile
46 {
47   public String id;
48
49   protected ArrayList<RNA> result;
50
51   public RnamlFile()
52   {
53     super();
54
55   }
56
57   public RnamlFile(String inFile, String type) throws IOException
58   {
59     super(inFile, type);
60
61   }
62
63   public RnamlFile(FileParse source) throws IOException
64   {
65     super(source);
66
67   }
68
69   public BufferedReader CreateReader() throws FileNotFoundException
70   {
71     FileReader fr = null;
72     fr = new FileReader(inFile);
73
74     BufferedReader r = new BufferedReader(fr);
75     return r;
76   }
77
78   /*
79    * (non-Javadoc)
80    * 
81    * @see jalview.io.AlignFile#parse()
82    */
83   public void parse() throws IOException
84   {
85     if (System.getProperty("java.version").indexOf("1.6") > -1
86             || System.getProperty("java.version").indexOf("1.5") > -1)
87     {
88       // patch for 'This parser does not support specification "null" version
89       // "null"' error
90       // this hack ensures we get a properly updated SAXParserFactory on older
91       // JVMs
92       // thanks to Stefan Birkner over at https://coderwall.com/p/kqsrrw
93       System.setProperty("javax.xml.parsers.SAXParserFactory",
94               "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl");
95     }
96     // rather than lose exception semantics whilst parsing RNAML with VARNA we
97     // wrap the routine and catch all exceptions before passing them up the
98     // chain as an IOException
99     try
100     {
101       _parse();
102     } catch (ExceptionPermissionDenied pdx)
103     {
104       errormessage = MessageManager.formatMessage("exception.rnaml_couldnt_access_datasource", new String[]{pdx.getMessage()});
105       throw new IOException(pdx);
106     } catch (ExceptionLoadingFailed lf)
107     {
108       errormessage = MessageManager.formatMessage("exception.ranml_couldnt_process_data", new String[]{lf.getMessage()});
109       throw new IOException(lf);
110     } catch (ExceptionFileFormatOrSyntax iff)
111     {
112         errormessage = MessageManager.formatMessage("exception.ranml_invalid_file", new String[]{iff.getMessage()});
113       throw new IOException(iff);
114     } catch (Exception x)
115     {
116       error = true;
117       errormessage = MessageManager.formatMessage("exception.ranml_problem_parsing_data", new String[]{x.getMessage()});
118       throw new IOException(errormessage , x);
119     }
120   }
121
122   @SuppressWarnings("unchecked")
123   public void _parse() throws FileNotFoundException,
124           ExceptionPermissionDenied, ExceptionLoadingFailed,
125           ExceptionFileFormatOrSyntax
126   {
127
128     result = RNAFactory.loadSecStrRNAML(getReader());
129
130     ArrayList<ArrayList> allarray = new ArrayList();
131     ArrayList<ArrayList<SimpleBP>> BP = new ArrayList();
132     ArrayList strucinarray = new ArrayList();
133     SequenceI[] seqs = new SequenceI[result.size()];
134
135     for (int i = 0; i < result.size(); i++)
136     {
137
138       RNA current = result.get(i);
139       String rna = current.getStructDBN(true);
140       String seq = current.getSeq();
141       int begin = 1;
142       int end = seq.length();
143
144       id = current.getName();
145       if (id == null || id.trim().length() == 0)
146       {
147         id = safeName(getDataName());
148         if (result.size() > 1)
149         {
150           id += "." + i;
151         }
152       }
153       seqs[i] = new Sequence(id, seq, begin, end);
154
155       seqs[i].setEnd(seqs[i].findPosition(seqs[i].getLength()));
156       String[] annot = new String[rna.length()];
157       Annotation[] ann = new Annotation[rna.length()];
158
159       for (int j = 0; j < rna.length(); j++)
160       {
161         annot[j] = "" + rna.charAt(j);
162
163       }
164       for (int k = 0; k < rna.length(); k++)
165       {
166         ann[k] = new Annotation(annot[k], "",
167                 ResidueProperties.getRNASecStrucState(
168                         annot[k]).charAt(0), 0f);
169       }
170
171       AlignmentAnnotation align = new AlignmentAnnotation(
172               "Secondary Structure",
173               current.getID().trim().length() > 0 ? "Secondary Structure for "
174                       + current.getID()
175                       : "", ann);
176
177       seqs[i].addAlignmentAnnotation(align);
178       seqs[i].setRNA(result.get(i));
179
180       allarray.add(strucinarray);
181
182       annotations.addElement(align);
183       BP.add(align.bps);
184
185     }
186
187     setSeqs(seqs);
188   }
189
190   public static String print(SequenceI[] s)
191   {
192     return "not yet implemented";
193   }
194
195   public String print()
196   {
197     System.out.print("print :");
198     return print(getSeqsAsArray());
199   }
200
201   public ArrayList getRNA()
202   {
203     return result;
204   }
205
206   // public static void main(String[] args) {
207   // Pattern p= Pattern.compile("(.+)[.][^.]+");
208   // Matcher m = p.matcher("toto.xml.zip");
209   // System.out.println(m.matches());
210   // System.out.println(m.group(1));
211   // }
212   /**
213    * make a friendly ID string.
214    * 
215    * @param dataName
216    * @return truncated dataName to after last '/'
217    */
218   private String safeName(String dataName)
219   {
220     int b = 0;
221     if ((b = dataName.lastIndexOf(".")) > 0)
222     {
223       dataName = dataName.substring(0, b - 1);
224     }
225     b = 0;
226     Regex m = new Regex("[\\/]?([-A-Za-z0-9]+)\\.?");
227     String mm = dataName;
228     while (m.searchFrom(dataName, b))
229     {
230       mm = m.stringMatched();
231       b = m.matchedTo();
232     }
233     return mm;
234   }
235 }