updated to jalview 2.1 and begun ArchiveClient/VamsasClient/VamsasStore updates.
[jalview.git] / src / jalview / io / PIRFile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.io;
20
21 import java.io.*;
22 import java.util.*;
23
24 import jalview.datamodel.*;
25
26 public class PIRFile
27     extends AlignFile
28 {
29   public static boolean useModellerOutput = false;
30
31   Vector words = new Vector(); //Stores the words in a line after splitting
32
33   public PIRFile()
34   {
35   }
36
37   public PIRFile(String inFile, String type)
38       throws IOException
39   {
40     super(inFile, type);
41   }
42
43   public void parse() throws IOException
44   {
45       StringBuffer sequence;
46       String line = null;
47       ModellerDescription md;
48
49       while ( (line = nextLine()) != null)
50       {
51         if (line.length() == 0)
52         {
53           //System.out.println("blank line");
54           continue;
55         }
56         if (line.indexOf("C;") == 0 || line.indexOf("#") == 0)
57         {
58           continue;
59         }
60         Sequence newSeq = parseId(line.substring(line.indexOf(";") + 1));
61
62         sequence = new StringBuffer();
63
64         newSeq.setDescription(nextLine()); // this is the title line
65
66         boolean starFound = false;
67
68         while(!starFound)
69         {
70           line = nextLine();
71           sequence.append(line);
72
73           if (line == null)
74             break;
75
76           if (line.indexOf("*") > -1)
77           {
78             starFound = true;
79           }
80         }
81
82         if (sequence.length() > 0)
83         {
84           sequence.setLength(sequence.length() - 1);
85           newSeq.setSequence(sequence.toString());
86           if (!isValidProteinSequence(newSeq.getSequence()))
87           {
88             throw new IOException(AppletFormatAdapter.INVALID_CHARACTERS
89                                   +" : "+ newSeq.getName()
90                                   +" : "+invalidCharacter);
91           }
92
93           seqs.addElement(newSeq);
94
95           md = new ModellerDescription(newSeq.
96                 getDescription());
97           md.updateSequenceI(newSeq);
98         }
99       }
100   }
101
102   public String print()
103   {
104     return print(getSeqsAsArray());
105   }
106
107   public String print(SequenceI[] s)
108   {
109     boolean is_NA = jalview.util.Comparison.isNucleotide(s);
110     int len = 72;
111     StringBuffer out = new StringBuffer();
112     int i = 0;
113     ModellerDescription md;
114
115     while ( (i < s.length) && (s[i] != null))
116     {
117       String seq = s[i].getSequence();
118       seq = seq + "*";
119
120
121       if (is_NA)
122       {
123           // modeller doesn't really do nucleotides, so we don't do anything fancy
124           // Official tags area as follows, for now we'll use P1 and DL
125           // Protein (complete) P1
126           // Protein (fragment) F1
127           // DNA (linear) Dl
128           // DNA (circular) DC
129           // RNA (linear) RL
130           // RNA (circular) RC
131           // tRNA N3
132           // other functional RNA N1
133
134         out.append(">N1;" + s[i].getName() + "\n");
135         if (s[i].getDescription() == null)
136         {
137           out.append(s[i].getName() + " " +
138                      (s[i].getEnd() - s[i].getStart() + 1));
139           out.append(is_NA ? " bases\n" : " residues\n");
140         }
141         else
142         {
143           out.append(s[i].getDescription()+"\n");
144         }
145       }
146       else
147       {
148
149        if(useModellerOutput)
150        {
151          out.append(">P1;" + s[i].getName() + "\n");
152          md = new ModellerDescription(s[i]);
153          out.append(md.getDescriptionLine() + "\n");
154        }
155        else
156        {
157          out.append(">P1;" + printId(s[i]) + "\n");
158          if (s[i].getDescription() != null)
159            out.append(s[i].getDescription() + "\n");
160          else
161            out.append(s[i].getName() + " "
162                       + (s[i].getEnd() - s[i].getStart() + 1)
163                       + " residues\n");
164        }
165       }
166       int nochunks = (seq.length() / len) + 1;
167
168       for (int j = 0; j < nochunks; j++)
169       {
170         int start = j * len;
171         int end = start + len;
172
173         if (end < seq.length())
174         {
175           out.append(seq.substring(start, end) + "\n");
176         }
177         else if (start < seq.length())
178         {
179           out.append(seq.substring(start) + "\n");
180         }
181       }
182
183       i++;
184     }
185
186     return out.toString();
187   }
188
189 }