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