JAL-1140 use the Reader provided by the jalview.io.FileParse framework
[jalview.git] / src / jalview / io / RnamlFile.java
1 package jalview.io;
2
3
4
5
6
7 import jalview.datamodel.AlignmentAnnotation;
8 import jalview.datamodel.Annotation;
9 import jalview.datamodel.SecondaryStructureAnnotation;
10 import jalview.datamodel.Sequence;
11 import jalview.datamodel.SequenceFeature;
12 import jalview.datamodel.SequenceI;
13 import jalview.ws.jws1.Annotate3D;
14
15 import java.io.BufferedReader;
16 import java.io.FileNotFoundException;
17 import java.io.IOException;
18 import java.io.Reader;
19 import java.io.FileReader;
20 import java.lang.reflect.Array;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.Hashtable;
24 import java.util.List;
25 import java.util.Vector;
26
27 import javax.xml.parsers.ParserConfigurationException;
28 import javax.xml.parsers.SAXParser;
29 import javax.xml.parsers.SAXParserFactory;
30
31 import org.xml.sax.InputSource;
32 import org.xml.sax.SAXException;
33
34 import com.stevesoft.pat.Regex;
35
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.exceptions.ExceptionUnmatchedClosingParentheses;
40 import fr.orsay.lri.varna.factories.RNAFactory;
41 import fr.orsay.lri.varna.factories.RNAFactory.RNAFileType;
42 import fr.orsay.lri.varna.models.rna.RNA;
43 import fr.orsay.lri.varna.utils.RNAMLParser;
44
45
46
47
48 public class RnamlFile extends AlignFile
49 {
50          public String id;
51          protected ArrayList<RNA> result;
52          
53          public RnamlFile()
54           {
55                  super();
56                 
57           }
58          
59          public RnamlFile(String inFile, String type) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException, ExceptionUnmatchedClosingParentheses
60           {
61             super(inFile, type);
62             
63           }
64
65           public RnamlFile(FileParse source) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException, ExceptionUnmatchedClosingParentheses
66           {
67             super(source);
68             
69           }
70           
71 //        public RnamlFile(BufferedReader r) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed
72 //        {
73 //                      super();            
74 //                  parse(r);
75 //                  // sets the index of each sequence in the alignment
76 //                  for( int i=0,c=seqs.size(); i<c; i++ ) {  
77 //                      seqs.get(i).setIndex(i);  
78 //                  }
79 //                
80 //          
81 //        }
82           
83         public BufferedReader CreateReader() throws FileNotFoundException
84         {
85                  FileReader fr = null;
86                   fr = new FileReader(inFile); 
87                   
88                   BufferedReader r = new BufferedReader (fr);
89                   return r;
90         }
91         
92         
93         @SuppressWarnings("unchecked")
94         public void parse() throws FileNotFoundException, ExceptionPermissionDenied, ExceptionLoadingFailed, ExceptionFileFormatOrSyntax
95         {
96                  result = RNAFactory.loadSecStrRNAML(getReader());
97                   
98                  System.out.println("this is the secondary scructure:" +result.size());
99                   //System.out.println("this is the secondary scructure:" +result.toString());
100                   //System.out.println("this is the secondary scructure:" +result.size());
101                  SequenceI[] seqs = new SequenceI[result.size()];
102                   System.out.println(type); //the type is "File"
103                   System.out.println(inFile );//inFile is the path
104                   
105                  for(int i=0;i<result.size();i++)
106                         {
107                          RNA current = result.get(i);
108                   System.out.println(current.getSeq());
109                   //System.out.println(result.get(i).getStructBPSEQ());
110                   System.out.println(result.get(i).getStructDBN(true));
111                   System.out.println(i);
112                   String rna =current.getStructDBN(true);
113                   //System.out.println(result.get(0).getAnnotations());
114                   String seq = current.getSeq();
115         
116                   int begin=0;
117                   int end = seq.length()-1;
118                   
119                   id = safeName(getDataName());
120                   seqs[i] = new Sequence(id, seq, begin, end);
121                   //System.out.println(seq);
122                   System.out.println("Rna is still "+rna);
123                   //SequenceFeature feat = new SequenceFeature(type, "",5, 13, 0f, null);
124                   //seqs[i].addSequenceFeature(feat);
125                   
126                   String[] annot=new String[rna.length()] ;
127                   Annotation[] ann = new Annotation[rna.length()];
128                 
129                   for(int j=0;j<rna.length();j++)
130                         {
131                           annot[j] =rna.substring(j,j+1);
132                   
133                         }
134                   
135                   for(int k=0;k<rna.length();k++)
136                 {
137                           ann[k] = new Annotation(annot[k], "", jalview.schemes.ResidueProperties.getRNASecStrucState(annot[k]).charAt(0), 0f);
138                                                   
139                 }
140                   AlignmentAnnotation align = new AlignmentAnnotation("Sec. str.",current.getID(),ann);
141                   
142                   seqs[i].addAlignmentAnnotation(align);
143                   seqs[i].setRNA(result.get(i));
144                   this.annotations.addElement(align);
145                 }
146           this.setSeqs(seqs);
147                  
148                  
149                 
150
151         }
152           
153         
154           public void parse(BufferedReader r) throws ExceptionPermissionDenied, ExceptionLoadingFailed, ExceptionFileFormatOrSyntax 
155           {
156                    System.out.println("parse with Buffer (RnamlFile");
157                   Hashtable seqhash = new Hashtable();
158                   Vector headers = new Vector();
159                   //ArrayList<String> seq =new ArrayList();
160                   //System.out.println(r);
161                   
162                   result = RNAFactory.loadSecStr(r,RNAFileType.FILE_TYPE_RNAML);
163                   
164                   System.out.println("this is the secondary scructure:" +result.toArray());
165                   System.out.println("this is the secondary scructure:" +result.toString());
166                   System.out.println("this is the secondary scructure:" +result.size());
167                         for(int i=0;i<result.size();i++)
168                         {
169                   System.out.println(result.get(0).getSeq());
170                   System.out.println(result.get(0).getStructBPSEQ());
171                   System.out.println(result.get(0).getStructDBN(true));
172                   String rna1 =result.get(0).getStructDBN(true);
173                   System.out.println(result.get(0).getAnnotations());
174                   String seq = result.get(0).getSeq();
175                   System.out.println(type); //the type is "File"
176                   System.out.println(inFile );//inFile is the path
177                   int begin=0;
178                   int end = seq.length()-1;
179                   SequenceI[] seqs = new SequenceI[1];
180                   id = safeName(getDataName());
181                   seqs[0] = new Sequence(id, seq, begin, end);
182                   SequenceFeature feat = new SequenceFeature(type, "",5, 13, 0f, null);
183                   seqs[0].addSequenceFeature(feat);
184                   this.setSeqs(seqs);
185                   
186                   String[] annot=new String[10000] ;
187                   Annotation[] ann = new Annotation[1000];
188                   for(int j=0;j<rna1.length();j++)
189                         {
190                   annot[j] =rna1.substring(j,j+1);
191                   //System.out.println(annot[j]);
192                         }
193                   
194                   for(int k=0;k<rna1.length();k++)
195                 {
196                           ann[k] = new Annotation(annot[k], "", ' ', 0f);
197                           
198                           ann[k].secondaryStructure = jalview.schemes.ResidueProperties.getRNASecStrucState(annot[k]).charAt(0);
199                           
200                 }
201                   AlignmentAnnotation align = new AlignmentAnnotation("Secondary structure","",ann);
202                   seqs[0].addAlignmentAnnotation(align);
203                   this.annotations.addElement(align);
204                 }
205                 //Annotate3D tintin = new Annotate3D();
206                   
207                 //System.out.println(tintin);  
208                   
209                   
210                   
211                   
212                   
213                   
214                  
215         
216 //                                      for(RNAMLParser.RNATmp rnaTmp : RNAMLData.getMolecules())
217 //                                      {
218 //                                              // Retrieving parsed data
219 //                                              seq = rnaTmp.getSequence(); //contient la séquence sous forme de liste
220 //                                              System.out.println(seq);
221 //                                              String seqAsStr = "";
222 //                                              for(String s:seq)
223 //                                              {
224 //                                                      // Only consider last character, e.g. in case of modified nucleotide
225 //                                                      seqAsStr += s.charAt(s.length()-1);
226 //                                              }
227 //                                              int begin=0;
228 //                                              int end = seqAsStr.length()-1;
229 //                                              SequenceI[] seqs = new SequenceI[1];
230 //                                              id = safeName(getDataName());
231 //                                              seqs[0] = new Sequence(id, seqAsStr, begin, end);
232 //                                              SequenceFeature feat = new SequenceFeature(type, "",5, 13, 0f, null);
233 //
234 //                          seqs[0].addSequenceFeature(feat);
235 //                                              this.setSeqs(seqs);
236                                                 
237                                                 //String pos = annot.substring(1, 2);
238                                                 //System.out.println(annot);
239                                                 //System.out.println(pos);
240                                                 
241                                            // ann[1] = new Annotation(pos, "", ' ', 0f);
242                                            // ann[1].secondaryStructure = 'A';
243                                             //System.out.println(ann[1]);
244                                             
245                                            // toto.annotations=ann;
246                                             
247
248                                             
249 ////                                             Annotation[] els = new Annotation[seq.size()];
250 //                                              
251 //                                              //System.err.println(""+seq.size());
252 //                                          Vector<RNAMLParser.BPTemp> allbpsTmp = rnaTmp.getStructure();
253 //                                          String[] annot = new String[10000];
254 //                                        
255 //                                              //Annotation[] ann = new Annotation[allbpsTmp.size()];
256 //                                              for (int i = 0; i < allbpsTmp.size(); i++) {
257 //                                              RNAMLParser.BPTemp bp = allbpsTmp.get(i);
258 //                                              //System.err.println("toto ="+bp);
259 //                                              int bp5 = bp.pos5;
260 //                                              annot[bp5]= "(";
261 //                                              //annot[bp5]= '(';
262 //                                              System.out.println(annot[bp5]);
263 //                                              //System.out.println("bp5 ="+bp5);
264 //                                              int bp3 = bp.pos3;
265 //                                      
266 //                                              annot[bp3]= ")";
267 //                                              System.out.println(annot[bp3]);
268 //                                              //System.out.println(annot.get(bp3));
269 //                                              
270 //                                              //String pos1 = annot.substring(bp5, bp5+1);
271 //                                              //String pos2 = annot.substring(bp3, bp3+1);
272 //                                              //ann[i] = new Annotation(pos1, "", ' ', 0f);
273 //                                              //ann[i+1] = new Annotation(pos2, "", ' ', 0f);
274 //                                              //System.out.println(pos1);
275 //                                              
276 //                                              //System.out.println("bp3 ="+bp3);
277 //                                              //ModeleBP newStyle = bp.createBPStyle(mb, part);
278 //                                              }       
279 //                                              Annotation[] ann = new Annotation[annot.length+1];
280 //                                              for(int i=0;i<annot.length;i++)
281 //                                              {               
282 //                                                      if(annot[i]==null)
283 //                                                      {
284 //                                                              annot[i]=".";
285 //                                                      }
286 //                                                      
287 //                                                      ann[i] = new Annotation(annot[i], "", ' ', 0f);
288 //                                              //System.out.print(annot[i]);
289 //                                              }
290 //                                              
291 //                                              AlignmentAnnotation toto = new AlignmentAnnotation("Secondary structure","",ann);
292 //                                              this.annotations.addElement(toto);
293                                         
294                                                 // Creating empty structure of suitable size
295 //                                              int[] str = new int[seq.size()];
296 //                                              for (int i=0;i<str.length;i++)
297 //                                              { str[i] = -1; }
298 //                                              Vector<RNAMLParser.BPTemp> allbpsTmp = rnaTmp.getStructure();
299 //                                              for (int i = 0; i < allbpsTmp.size(); i++) {
300 //                                                      RNAMLParser.BPTemp bp = allbpsTmp.get(i);
301 //                                                      //System.err.println("toto ="+bp);
302 //                                                      int bp5 = bp.pos5;
303 //                                                      //System.out.println("bp5 ="+bp5);
304 //                                                      int bp3 = bp.pos3;
305                                                         //System.out.println("bp3 ="+bp3);
306                                                         //ModeleBP newStyle = bp.createBPStyle(mb, part);
307 //                                              }
308                                                 //Annotation ann;
309                                             //ann = new Annotation(bp5, "", ' ', 0f);
310                                 
311                                         //}
312
313                         
314
315 //                              return result;
316           
317 }
318
319           
320           public static String print(SequenceI[] s)
321           {
322             return "not yet implemented";
323           }
324           
325           public String print()
326           {
327                 System.out.print("affiche :");
328             return print(getSeqsAsArray());
329           }
330           
331           /**
332            * make a friendly ID string.
333            * 
334            * @param dataName
335            * @return truncated dataName to after last '/'
336            */
337           private String safeName(String dataName)
338           {
339             int b = 0;
340             while ((b = dataName.indexOf("/")) > -1 && b < dataName.length() )
341             {              
342                 dataName = dataName.substring(b + 1).trim();
343                 
344             }
345             int e = (dataName.length() - dataName.indexOf("."))+1;
346             dataName = dataName.substring(1,e).trim();
347             return dataName;
348           }
349           
350           public ArrayList getRNA()
351           {
352                   return result;
353           }
354           
355           public Vector getAnnot()
356           {
357                   return annotations;
358           }
359 }