FileParse object can be re-used to read different files concatenated together
[jalview.git] / src / jalview / io / PIRFile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.io;
20
21 import java.io.*;
22 import java.util.*;
23
24 import jalview.datamodel.*;
25
26 public class PIRFile
27     extends AlignFile
28 {
29   public static boolean useModellerOutput = false;
30
31   Vector words = new Vector(); //Stores the words in a line after splitting
32
33   public PIRFile()
34   {
35   }
36
37   public PIRFile(String inFile, String type)
38       throws IOException
39   {
40     super(inFile, type);
41   }
42   public PIRFile(FileParse source) throws IOException
43   {
44     super(source);
45   }
46   public void parse()
47       throws IOException
48   {
49     StringBuffer sequence;
50     String line = null;
51     ModellerDescription md;
52
53     while ( (line = nextLine()) != null)
54     {
55       if (line.length() == 0)
56       {
57         //System.out.println("blank line");
58         continue;
59       }
60       if (line.indexOf("C;") == 0 || line.indexOf("#") == 0)
61       {
62         continue;
63       }
64       Sequence newSeq = parseId(line.substring(line.indexOf(";") + 1));
65
66       sequence = new StringBuffer();
67
68       newSeq.setDescription(nextLine()); // this is the title line
69
70       boolean starFound = false;
71
72       while (!starFound)
73       {
74         line = nextLine();
75         sequence.append(line);
76
77         if (line == null)
78         {
79           break;
80         }
81
82         if (line.indexOf("*") > -1)
83         {
84           starFound = true;
85         }
86       }
87
88       if (sequence.length() > 0)
89       {
90         sequence.setLength(sequence.length() - 1);
91         newSeq.setSequence(sequence.toString());
92
93         seqs.addElement(newSeq);
94
95         md = new ModellerDescription(newSeq.
96                                      getDescription());
97         md.updateSequenceI(newSeq);
98       }
99     }
100   }
101
102   public String print()
103   {
104     return print(getSeqsAsArray());
105   }
106
107   public String print(SequenceI[] s)
108   {
109     boolean is_NA = jalview.util.Comparison.isNucleotide(s);
110     int len = 72;
111     StringBuffer out = new StringBuffer();
112     int i = 0;
113     ModellerDescription md;
114
115     while ( (i < s.length) && (s[i] != null))
116     {
117       String seq = s[i].getSequenceAsString();
118       seq = seq + "*";
119
120       if (is_NA)
121       {
122         // modeller doesn't really do nucleotides, so we don't do anything fancy
123         // Official tags area as follows, for now we'll use P1 and DL
124         // Protein (complete) P1
125         // Protein (fragment) F1
126         // DNA (linear) Dl
127         // DNA (circular) DC
128         // RNA (linear) RL
129         // RNA (circular) RC
130         // tRNA N3
131         // other functional RNA N1
132
133         out.append(">N1;" + s[i].getName() + "\n");
134         if (s[i].getDescription() == null)
135         {
136           out.append(s[i].getName() + " " +
137                      (s[i].getEnd() - s[i].getStart() + 1));
138           out.append(is_NA ? " bases\n" : " residues\n");
139         }
140         else
141         {
142           out.append(s[i].getDescription() + "\n");
143         }
144       }
145       else
146       {
147
148         if (useModellerOutput)
149         {
150           out.append(">P1;" + s[i].getName() + "\n");
151           md = new ModellerDescription(s[i]);
152           out.append(md.getDescriptionLine() + "\n");
153         }
154         else
155         {
156           out.append(">P1;" + printId(s[i]) + "\n");
157           if (s[i].getDescription() != null)
158           {
159             out.append(s[i].getDescription() + "\n");
160           }
161           else
162           {
163             out.append(s[i].getName() + " "
164                        + (s[i].getEnd() - s[i].getStart() + 1)
165                        + " residues\n");
166           }
167         }
168       }
169       int nochunks = (seq.length() / len) + 1;
170
171       for (int j = 0; j < nochunks; j++)
172       {
173         int start = j * len;
174         int end = start + len;
175
176         if (end < seq.length())
177         {
178           out.append(seq.substring(start, end) + "\n");
179         }
180         else if (start < seq.length())
181         {
182           out.append(seq.substring(start) + "\n");
183         }
184       }
185
186       i++;
187     }
188
189     return out.toString();
190   }
191
192 }