Add support RNAML format
[jalview.git] / src / jalview / io / PIRFile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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 java.io.*;
21 import java.util.*;
22
23 import javax.xml.parsers.ParserConfigurationException;
24
25 import org.xml.sax.SAXException;
26
27 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
28 import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
29 import fr.orsay.lri.varna.exceptions.ExceptionPermissionDenied;
30
31 import jalview.datamodel.*;
32
33 public class PIRFile extends AlignFile
34 {
35   public static boolean useModellerOutput = false;
36
37   Vector words = new Vector(); // Stores the words in a line after splitting
38
39   public PIRFile()
40   {
41   }
42
43   public PIRFile(String inFile, String type) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed
44   {
45     super(inFile, type);
46   }
47
48   public PIRFile(FileParse source) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed
49   {
50     super(source);
51   }
52
53   public void parse() throws IOException
54   {
55     StringBuffer sequence;
56     String line = null;
57     ModellerDescription md;
58
59     while ((line = nextLine()) != null)
60     {
61       if (line.length() == 0)
62       {
63         // System.out.println("blank line");
64         continue;
65       }
66       if (line.indexOf("C;") == 0 || line.indexOf("#") == 0)
67       {
68         continue;
69       }
70       Sequence newSeq = parseId(line.substring(line.indexOf(";") + 1));
71
72       sequence = new StringBuffer();
73
74       newSeq.setDescription(nextLine()); // this is the title line
75
76       boolean starFound = false;
77
78       while (!starFound)
79       {
80         line = nextLine();
81         sequence.append(line);
82
83         if (line == null)
84         {
85           break;
86         }
87
88         if (line.indexOf("*") > -1)
89         {
90           starFound = true;
91         }
92       }
93
94       if (sequence.length() > 0)
95       {
96         sequence.setLength(sequence.length() - 1);
97         newSeq.setSequence(sequence.toString());
98
99         seqs.addElement(newSeq);
100
101         md = new ModellerDescription(newSeq.getDescription());
102         md.updateSequenceI(newSeq);
103       }
104     }
105   }
106
107   public String print()
108   {
109     return print(getSeqsAsArray());
110   }
111
112   public String print(SequenceI[] s)
113   {
114     boolean is_NA = jalview.util.Comparison.isNucleotide(s);
115     int len = 72;
116     StringBuffer out = new StringBuffer();
117     int i = 0;
118     ModellerDescription md;
119
120     while ((i < s.length) && (s[i] != null))
121     {
122       String seq = s[i].getSequenceAsString();
123       seq = seq + "*";
124
125       if (is_NA)
126       {
127         // modeller doesn't really do nucleotides, so we don't do anything fancy
128         // Official tags area as follows, for now we'll use P1 and DL
129         // Protein (complete) P1
130         // Protein (fragment) F1
131         // DNA (linear) Dl
132         // DNA (circular) DC
133         // RNA (linear) RL
134         // RNA (circular) RC
135         // tRNA N3
136         // other functional RNA N1
137
138         out.append(">N1;" + s[i].getName());
139         out.append(newline);
140         if (s[i].getDescription() == null)
141         {
142           out.append(s[i].getName() + " "
143                   + (s[i].getEnd() - s[i].getStart() + 1));
144           out.append(is_NA ? " bases" : " residues");
145           out.append(newline);
146         }
147         else
148         {
149           out.append(s[i].getDescription());
150           out.append(newline);
151         }
152       }
153       else
154       {
155
156         if (useModellerOutput)
157         {
158           out.append(">P1;" + s[i].getName());
159           out.append(newline);
160           md = new ModellerDescription(s[i]);
161           out.append(md.getDescriptionLine());
162           out.append(newline);
163         }
164         else
165         {
166           out.append(">P1;" + printId(s[i]));
167           out.append(newline);
168           if (s[i].getDescription() != null)
169           {
170             out.append(s[i].getDescription());
171             out.append(newline);
172           }
173           else
174           {
175             out.append(s[i].getName() + " "
176                     + (s[i].getEnd() - s[i].getStart() + 1) + " residues");
177             out.append(newline);
178           }
179         }
180       }
181       int nochunks = (seq.length() / len) + 1;
182
183       for (int j = 0; j < nochunks; j++)
184       {
185         int start = j * len;
186         int end = start + len;
187
188         if (end < seq.length())
189         {
190           out.append(seq.substring(start, end));
191           out.append(newline);
192         }
193         else if (start < seq.length())
194         {
195           out.append(seq.substring(start));
196           out.append(newline);
197         }
198       }
199
200       i++;
201     }
202
203     return out.toString();
204   }
205
206 }