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