update author list in license for (JAL-826)
[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 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());
131         out.append(newline);
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" : " residues");
137           out.append(newline);
138         }
139         else
140         {
141           out.append(s[i].getDescription());
142           out.append(newline);
143         }
144       }
145       else
146       {
147
148         if (useModellerOutput)
149         {
150           out.append(">P1;" + s[i].getName());
151           out.append(newline);
152           md = new ModellerDescription(s[i]);
153           out.append(md.getDescriptionLine());
154           out.append(newline);
155         }
156         else
157         {
158           out.append(">P1;" + printId(s[i]));
159           out.append(newline);
160           if (s[i].getDescription() != null)
161           {
162             out.append(s[i].getDescription());
163             out.append(newline);
164           }
165           else
166           {
167             out.append(s[i].getName() + " "
168                     + (s[i].getEnd() - s[i].getStart() + 1) + " residues");
169             out.append(newline);
170           }
171         }
172       }
173       int nochunks = (seq.length() / len) + 1;
174
175       for (int j = 0; j < nochunks; j++)
176       {
177         int start = j * len;
178         int end = start + len;
179
180         if (end < seq.length())
181         {
182           out.append(seq.substring(start, end));
183           out.append(newline);
184         }
185         else if (start < seq.length())
186         {
187           out.append(seq.substring(start));
188           out.append(newline);
189         }
190       }
191
192       i++;
193     }
194
195     return out.toString();
196   }
197
198 }