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