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