JAL-2089 patch broken merge to master for Release 2.10.0b1
[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, String type) throws IOException
40   {
41     super(inFile, type);
42   }
43
44   public PIRFile(FileParse source) throws IOException
45   {
46     super(source);
47   }
48
49   @Override
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   @Override
105   public String print()
106   {
107     return print(getSeqsAsArray());
108   }
109
110   public String print(SequenceI[] s)
111   {
112     boolean is_NA = jalview.util.Comparison.isNucleotide(s);
113     int len = 72;
114     StringBuffer out = new StringBuffer();
115     int i = 0;
116     ModellerDescription md;
117
118     while ((i < s.length) && (s[i] != null))
119     {
120       String seq = s[i].getSequenceAsString();
121       seq = seq + "*";
122
123       if (is_NA)
124       {
125         // modeller doesn't really do nucleotides, so we don't do anything fancy
126         // Official tags area as follows, for now we'll use P1 and DL
127         // Protein (complete) P1
128         // Protein (fragment) F1
129         // DNA (linear) Dl
130         // DNA (circular) DC
131         // RNA (linear) RL
132         // RNA (circular) RC
133         // tRNA N3
134         // other functional RNA N1
135
136         out.append(">N1;" + s[i].getName());
137         out.append(newline);
138         if (s[i].getDescription() == null)
139         {
140           out.append(s[i].getName() + " "
141                   + (s[i].getEnd() - s[i].getStart() + 1));
142           out.append(is_NA ? " bases" : " residues");
143           out.append(newline);
144         }
145         else
146         {
147           out.append(s[i].getDescription());
148           out.append(newline);
149         }
150       }
151       else
152       {
153
154         if (useModellerOutput)
155         {
156           out.append(">P1;" + s[i].getName());
157           out.append(newline);
158           md = new ModellerDescription(s[i]);
159           out.append(md.getDescriptionLine());
160           out.append(newline);
161         }
162         else
163         {
164           out.append(">P1;" + printId(s[i]));
165           out.append(newline);
166           if (s[i].getDescription() != null)
167           {
168             out.append(s[i].getDescription());
169             out.append(newline);
170           }
171           else
172           {
173             out.append(s[i].getName() + " "
174                     + (s[i].getEnd() - s[i].getStart() + 1) + " residues");
175             out.append(newline);
176           }
177         }
178       }
179       int nochunks = (seq.length() / len)
180               + (seq.length() % len > 0 ? 1 : 0);
181
182       for (int j = 0; j < nochunks; j++)
183       {
184         int start = j * len;
185         int end = start + len;
186
187         if (end < seq.length())
188         {
189           out.append(seq.substring(start, end));
190           out.append(newline);
191         }
192         else if (start < seq.length())
193         {
194           out.append(seq.substring(start));
195           out.append(newline);
196         }
197       }
198
199       i++;
200     }
201
202     return out.toString();
203   }
204
205 }