JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / src / jalview / io / PIRFile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
3  * Copyright (C) 2015 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   public void parse() throws IOException
50   {
51     StringBuffer sequence;
52     String line = null;
53     ModellerDescription md;
54
55     while ((line = nextLine()) != null)
56     {
57       if (line.length() == 0)
58       {
59         // System.out.println("blank line");
60         continue;
61       }
62       if (line.indexOf("C;") == 0 || line.indexOf("#") == 0)
63       {
64         continue;
65       }
66       Sequence newSeq = parseId(line.substring(line.indexOf(";") + 1));
67
68       sequence = new StringBuffer();
69
70       newSeq.setDescription(nextLine()); // this is the title line
71
72       boolean starFound = false;
73
74       while (!starFound)
75       {
76         line = nextLine();
77         sequence.append(line);
78
79         if (line == null)
80         {
81           break;
82         }
83
84         if (line.indexOf("*") > -1)
85         {
86           starFound = true;
87         }
88       }
89
90       if (sequence.length() > 0)
91       {
92         sequence.setLength(sequence.length() - 1);
93         newSeq.setSequence(sequence.toString());
94
95         seqs.addElement(newSeq);
96
97         md = new ModellerDescription(newSeq.getDescription());
98         md.updateSequenceI(newSeq);
99       }
100     }
101   }
102
103   public String print()
104   {
105     return print(getSeqsAsArray());
106   }
107
108   public String print(SequenceI[] s)
109   {
110     boolean is_NA = jalview.util.Comparison.isNucleotide(s);
111     int len = 72;
112     StringBuffer out = new StringBuffer();
113     int i = 0;
114     ModellerDescription md;
115
116     while ((i < s.length) && (s[i] != null))
117     {
118       String seq = s[i].getSequenceAsString();
119       seq = seq + "*";
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());
135         out.append(newline);
136         if (s[i].getDescription() == null)
137         {
138           out.append(s[i].getName() + " "
139                   + (s[i].getEnd() - s[i].getStart() + 1));
140           out.append(is_NA ? " bases" : " residues");
141           out.append(newline);
142         }
143         else
144         {
145           out.append(s[i].getDescription());
146           out.append(newline);
147         }
148       }
149       else
150       {
151
152         if (useModellerOutput)
153         {
154           out.append(">P1;" + s[i].getName());
155           out.append(newline);
156           md = new ModellerDescription(s[i]);
157           out.append(md.getDescriptionLine());
158           out.append(newline);
159         }
160         else
161         {
162           out.append(">P1;" + printId(s[i]));
163           out.append(newline);
164           if (s[i].getDescription() != null)
165           {
166             out.append(s[i].getDescription());
167             out.append(newline);
168           }
169           else
170           {
171             out.append(s[i].getName() + " "
172                     + (s[i].getEnd() - s[i].getStart() + 1) + " residues");
173             out.append(newline);
174           }
175         }
176       }
177       int nochunks = (seq.length() / len) + 1;
178
179       for (int j = 0; j < nochunks; j++)
180       {
181         int start = j * len;
182         int end = start + len;
183
184         if (end < seq.length())
185         {
186           out.append(seq.substring(start, end));
187           out.append(newline);
188         }
189         else if (start < seq.length())
190         {
191           out.append(seq.substring(start));
192           out.append(newline);
193         }
194       }
195
196       i++;
197     }
198
199     return out.toString();
200   }
201
202 }