last version stay many bugs ..
[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                  FileReader fr = null;
97                   fr = new FileReader(inFile); 
98                   
99                   BufferedReader r = new BufferedReader (fr);
100         
101                   //ArrayList<String> seq =new ArrayList();
102                   //System.out.println(r);
103                    
104                   
105                  result = RNAFactory.loadSecStrRNAML(r);
106                   
107                  System.out.println("this is the secondary scructure:" +result.size());
108                   //System.out.println("this is the secondary scructure:" +result.toString());
109                   //System.out.println("this is the secondary scructure:" +result.size());
110                  SequenceI[] seqs = new SequenceI[result.size()];
111                   System.out.println(type); //the type is "File"
112                   System.out.println(inFile );//inFile is the path
113                   
114                  for(int i=0;i<result.size();i++)
115                         {
116                          RNA current = result.get(i);
117                   System.out.println(current.getSeq());
118                   //System.out.println(result.get(i).getStructBPSEQ());
119                   System.out.println(result.get(i).getStructDBN(true));
120                   System.out.println(i);
121                   String rna =current.getStructDBN(true);
122                   //System.out.println(result.get(0).getAnnotations());
123                   String seq = current.getSeq();
124         
125                   int begin=0;
126                   int end = seq.length()-1;
127                   
128                   id = safeName(getDataName());
129                   seqs[i] = new Sequence(id, seq, begin, end);
130                   //System.out.println(seq);
131                   System.out.println("Rna is still "+rna);
132                   //SequenceFeature feat = new SequenceFeature(type, "",5, 13, 0f, null);
133                   //seqs[i].addSequenceFeature(feat);
134                   
135                   String[] annot=new String[rna.length()] ;
136                   Annotation[] ann = new Annotation[rna.length()];
137                 
138                   for(int j=0;j<rna.length();j++)
139                         {
140                           annot[j] =rna.substring(j,j+1);
141                   
142                         }
143                   
144                   for(int k=0;k<rna.length();k++)
145                 {
146                           ann[k] = new Annotation(annot[k], "", jalview.schemes.ResidueProperties.getRNASecStrucState(annot[k]).charAt(0), 0f);
147                                                   
148                 }
149                   AlignmentAnnotation align = new AlignmentAnnotation("Sec. str.",current.getID(),ann);
150                   
151                   seqs[i].addAlignmentAnnotation(align);
152                   seqs[i].setRNA(result.get(i));
153                   this.annotations.addElement(align);
154                 }
155           this.setSeqs(seqs);
156                  
157                  
158                 
159
160         }
161           
162         
163           public void parse(BufferedReader r) throws ExceptionPermissionDenied, ExceptionLoadingFailed, ExceptionFileFormatOrSyntax 
164           {
165                    System.out.println("parse with Buffer (RnamlFile");
166                   Hashtable seqhash = new Hashtable();
167                   Vector headers = new Vector();
168                   //ArrayList<String> seq =new ArrayList();
169                   //System.out.println(r);
170                   
171                   result = RNAFactory.loadSecStr(r,RNAFileType.FILE_TYPE_RNAML);
172                   
173                   System.out.println("this is the secondary scructure:" +result.toArray());
174                   System.out.println("this is the secondary scructure:" +result.toString());
175                   System.out.println("this is the secondary scructure:" +result.size());
176                         for(int i=0;i<result.size();i++)
177                         {
178                   System.out.println(result.get(0).getSeq());
179                   System.out.println(result.get(0).getStructBPSEQ());
180                   System.out.println(result.get(0).getStructDBN(true));
181                   String rna1 =result.get(0).getStructDBN(true);
182                   System.out.println(result.get(0).getAnnotations());
183                   String seq = result.get(0).getSeq();
184                   System.out.println(type); //the type is "File"
185                   System.out.println(inFile );//inFile is the path
186                   int begin=0;
187                   int end = seq.length()-1;
188                   SequenceI[] seqs = new SequenceI[1];
189                   id = safeName(getDataName());
190                   seqs[0] = new Sequence(id, seq, begin, end);
191                   SequenceFeature feat = new SequenceFeature(type, "",5, 13, 0f, null);
192                   seqs[0].addSequenceFeature(feat);
193                   this.setSeqs(seqs);
194                   
195                   String[] annot=new String[10000] ;
196                   Annotation[] ann = new Annotation[1000];
197                   for(int j=0;j<rna1.length();j++)
198                         {
199                   annot[j] =rna1.substring(j,j+1);
200                   //System.out.println(annot[j]);
201                         }
202                   
203                   for(int k=0;k<rna1.length();k++)
204                 {
205                           ann[k] = new Annotation(annot[k], "", ' ', 0f);
206                           
207                           ann[k].secondaryStructure = jalview.schemes.ResidueProperties.getRNASecStrucState(annot[k]).charAt(0);
208                           
209                 }
210                   AlignmentAnnotation align = new AlignmentAnnotation("Secondary structure","",ann);
211                   seqs[0].addAlignmentAnnotation(align);
212                   this.annotations.addElement(align);
213                 }
214                 //Annotate3D tintin = new Annotate3D();
215                   
216                 //System.out.println(tintin);  
217                   
218                   
219                   
220                   
221                   
222                   
223                  
224         
225 //                                      for(RNAMLParser.RNATmp rnaTmp : RNAMLData.getMolecules())
226 //                                      {
227 //                                              // Retrieving parsed data
228 //                                              seq = rnaTmp.getSequence(); //contient la séquence sous forme de liste
229 //                                              System.out.println(seq);
230 //                                              String seqAsStr = "";
231 //                                              for(String s:seq)
232 //                                              {
233 //                                                      // Only consider last character, e.g. in case of modified nucleotide
234 //                                                      seqAsStr += s.charAt(s.length()-1);
235 //                                              }
236 //                                              int begin=0;
237 //                                              int end = seqAsStr.length()-1;
238 //                                              SequenceI[] seqs = new SequenceI[1];
239 //                                              id = safeName(getDataName());
240 //                                              seqs[0] = new Sequence(id, seqAsStr, begin, end);
241 //                                              SequenceFeature feat = new SequenceFeature(type, "",5, 13, 0f, null);
242 //
243 //                          seqs[0].addSequenceFeature(feat);
244 //                                              this.setSeqs(seqs);
245                                                 
246                                                 //String pos = annot.substring(1, 2);
247                                                 //System.out.println(annot);
248                                                 //System.out.println(pos);
249                                                 
250                                            // ann[1] = new Annotation(pos, "", ' ', 0f);
251                                            // ann[1].secondaryStructure = 'A';
252                                             //System.out.println(ann[1]);
253                                             
254                                            // toto.annotations=ann;
255                                             
256
257                                             
258 ////                                             Annotation[] els = new Annotation[seq.size()];
259 //                                              
260 //                                              //System.err.println(""+seq.size());
261 //                                          Vector<RNAMLParser.BPTemp> allbpsTmp = rnaTmp.getStructure();
262 //                                          String[] annot = new String[10000];
263 //                                        
264 //                                              //Annotation[] ann = new Annotation[allbpsTmp.size()];
265 //                                              for (int i = 0; i < allbpsTmp.size(); i++) {
266 //                                              RNAMLParser.BPTemp bp = allbpsTmp.get(i);
267 //                                              //System.err.println("toto ="+bp);
268 //                                              int bp5 = bp.pos5;
269 //                                              annot[bp5]= "(";
270 //                                              //annot[bp5]= '(';
271 //                                              System.out.println(annot[bp5]);
272 //                                              //System.out.println("bp5 ="+bp5);
273 //                                              int bp3 = bp.pos3;
274 //                                      
275 //                                              annot[bp3]= ")";
276 //                                              System.out.println(annot[bp3]);
277 //                                              //System.out.println(annot.get(bp3));
278 //                                              
279 //                                              //String pos1 = annot.substring(bp5, bp5+1);
280 //                                              //String pos2 = annot.substring(bp3, bp3+1);
281 //                                              //ann[i] = new Annotation(pos1, "", ' ', 0f);
282 //                                              //ann[i+1] = new Annotation(pos2, "", ' ', 0f);
283 //                                              //System.out.println(pos1);
284 //                                              
285 //                                              //System.out.println("bp3 ="+bp3);
286 //                                              //ModeleBP newStyle = bp.createBPStyle(mb, part);
287 //                                              }       
288 //                                              Annotation[] ann = new Annotation[annot.length+1];
289 //                                              for(int i=0;i<annot.length;i++)
290 //                                              {               
291 //                                                      if(annot[i]==null)
292 //                                                      {
293 //                                                              annot[i]=".";
294 //                                                      }
295 //                                                      
296 //                                                      ann[i] = new Annotation(annot[i], "", ' ', 0f);
297 //                                              //System.out.print(annot[i]);
298 //                                              }
299 //                                              
300 //                                              AlignmentAnnotation toto = new AlignmentAnnotation("Secondary structure","",ann);
301 //                                              this.annotations.addElement(toto);
302                                         
303                                                 // Creating empty structure of suitable size
304 //                                              int[] str = new int[seq.size()];
305 //                                              for (int i=0;i<str.length;i++)
306 //                                              { str[i] = -1; }
307 //                                              Vector<RNAMLParser.BPTemp> allbpsTmp = rnaTmp.getStructure();
308 //                                              for (int i = 0; i < allbpsTmp.size(); i++) {
309 //                                                      RNAMLParser.BPTemp bp = allbpsTmp.get(i);
310 //                                                      //System.err.println("toto ="+bp);
311 //                                                      int bp5 = bp.pos5;
312 //                                                      //System.out.println("bp5 ="+bp5);
313 //                                                      int bp3 = bp.pos3;
314                                                         //System.out.println("bp3 ="+bp3);
315                                                         //ModeleBP newStyle = bp.createBPStyle(mb, part);
316 //                                              }
317                                                 //Annotation ann;
318                                             //ann = new Annotation(bp5, "", ' ', 0f);
319                                 
320                                         //}
321
322                         
323
324 //                              return result;
325           
326 }
327
328           
329           public static String print(SequenceI[] s)
330           {
331             return "not yet implemented";
332           }
333           
334           public String print()
335           {
336                 System.out.print("affiche :");
337             return print(getSeqsAsArray());
338           }
339           
340           /**
341            * make a friendly ID string.
342            * 
343            * @param dataName
344            * @return truncated dataName to after last '/'
345            */
346           private String safeName(String dataName)
347           {
348             int b = 0;
349             while ((b = dataName.indexOf("/")) > -1 && b < dataName.length() )
350             {              
351                 dataName = dataName.substring(b + 1).trim();
352                 
353             }
354             int e = (dataName.length() - dataName.indexOf("."))+1;
355             dataName = dataName.substring(1,e).trim();
356             return dataName;
357           }
358           
359           public ArrayList getRNA()
360           {
361                   return result;
362           }
363           
364           public Vector getAnnot()
365           {
366                   return annotations;
367           }
368 }