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