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