Formatting
[jalview.git] / src / jalview / io / AlignFile.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  *\r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  *\r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 package jalview.io;\r
20 \r
21 import java.io.*;\r
22 import java.util.*;\r
23 \r
24 import jalview.datamodel.*;\r
25 \r
26 /**\r
27  * DOCUMENT ME!\r
28  *\r
29  * @author $author$\r
30  * @version $Revision$\r
31  */\r
32 public abstract class AlignFile\r
33     extends FileParse\r
34 {\r
35   int noSeqs = 0;\r
36   int maxLength = 0;\r
37   Vector seqs;\r
38   Vector annotations;\r
39   long start;\r
40   long end;\r
41   boolean jvSuffix = true;\r
42 \r
43   /**\r
44    * Creates a new AlignFile object.\r
45    */\r
46   public AlignFile()\r
47   {\r
48   }\r
49 \r
50   /**\r
51    * Constructor which parses the data from a file of some specified type.\r
52    * @param inFile Filename to read from.\r
53    * @param type   What type of file to read from (File, URL)\r
54    */\r
55   public AlignFile(String inFile, String type)\r
56       throws IOException\r
57   {\r
58     super(inFile, type);\r
59 \r
60     initData();\r
61 \r
62     parse();\r
63   }\r
64 \r
65   /**\r
66    * Return the seqs Vector\r
67    */\r
68   public Vector getSeqs()\r
69   {\r
70     return seqs;\r
71   }\r
72 \r
73   /**\r
74    * Return the Sequences in the seqs Vector as an array of Sequences\r
75    */\r
76   public SequenceI[] getSeqsAsArray()\r
77   {\r
78     SequenceI[] s = new SequenceI[seqs.size()];\r
79 \r
80     for (int i = 0; i < seqs.size(); i++)\r
81     {\r
82       s[i] = (SequenceI) seqs.elementAt(i);\r
83     }\r
84 \r
85     return s;\r
86   }\r
87 \r
88   public void addAnnotations(Alignment al)\r
89   {\r
90     for (int i = 0; i < annotations.size(); i++)\r
91     {\r
92       al.addAnnotation(\r
93           (AlignmentAnnotation) annotations.elementAt(i)\r
94           );\r
95     }\r
96 \r
97   }\r
98 \r
99   /**\r
100    * Initialise objects to store sequence data in.\r
101    */\r
102   protected void initData()\r
103   {\r
104     seqs = new Vector();\r
105     annotations = new Vector();\r
106   }\r
107 \r
108   /**\r
109    * DOCUMENT ME!\r
110    *\r
111    * @param s DOCUMENT ME!\r
112    */\r
113   protected void setSeqs(SequenceI[] s)\r
114   {\r
115     seqs = new Vector();\r
116 \r
117     for (int i = 0; i < s.length; i++)\r
118     {\r
119       seqs.addElement(s[i]);\r
120     }\r
121   }\r
122 \r
123   /**\r
124    * This method must be implemented to parse the contents of the file.\r
125    */\r
126   public abstract void parse()\r
127       throws IOException;\r
128 \r
129   /**\r
130    * Print out in alignment file format the Sequences in the seqs Vector.\r
131    */\r
132   public abstract String print();\r
133 \r
134   public void addJVSuffix(boolean b)\r
135   {\r
136     jvSuffix = b;\r
137   }\r
138 \r
139   /**\r
140    * A general parser for ids.\r
141    *\r
142    * @String id Id to be parsed\r
143    */\r
144   Sequence parseId(String id)\r
145   {\r
146     Sequence seq = null;\r
147     id = id.trim();\r
148     int space = id.indexOf(" ");\r
149     if (space > -1)\r
150     {\r
151       seq = new Sequence(id.substring(0, space), "");\r
152       seq.setDescription(id.substring(space + 1));\r
153     }\r
154     else\r
155     {\r
156       seq = new Sequence(id, "");\r
157     }\r
158 \r
159     return seq;\r
160   }\r
161 \r
162   /**\r
163    * Creates the output id.\r
164    * Adds prefix Uniprot format source|id\r
165    * And suffix Jalview /start-end\r
166    *\r
167    * @String id Id to be parsed\r
168    */\r
169   String printId(SequenceI seq)\r
170   {\r
171     return seq.getDisplayId(jvSuffix);\r
172   }\r
173 \r
174 }\r