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