3fa4ac2b01e6b889664462ad976fef63ea6b616f
[jalview.git] / src / jalview / io / AlignFile.java
1 /*\r
2 * Jalview - A Sequence Alignment Editor and Viewer\r
3 * Copyright (C) 2006 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 jalview.datamodel.*;\r
22 \r
23 import java.io.*;\r
24 \r
25 import java.util.*;\r
26 \r
27 \r
28 /**\r
29  * DOCUMENT ME!\r
30  *\r
31  * @author $author$\r
32  * @version $Revision$\r
33  */\r
34 public abstract class AlignFile extends FileParse\r
35 {\r
36     int noSeqs = 0;\r
37     int maxLength = 0;\r
38     Vector seqs;\r
39     Vector annotations;\r
40     long start;\r
41     long end;\r
42     boolean jvSuffix = true;\r
43 \r
44     /**\r
45      * Creates a new AlignFile object.\r
46      */\r
47     public AlignFile()\r
48     {\r
49     }\r
50 \r
51 \r
52     /**\r
53      * Constructor which parses the data from a file of some specified type.\r
54      * @param inFile Filename to read from.\r
55      * @param type   What type of file to read from (File, URL)\r
56      */\r
57     public AlignFile(String inFile, String type) throws IOException\r
58     {\r
59         super(inFile, type);\r
60 \r
61         initData();\r
62 \r
63         parse();\r
64     }\r
65 \r
66     /**\r
67      * Return the seqs Vector\r
68      */\r
69     public Vector getSeqs()\r
70     {\r
71         return seqs;\r
72     }\r
73 \r
74     /**\r
75      * Return the Sequences in the seqs Vector as an array of Sequences\r
76      */\r
77     public SequenceI[] getSeqsAsArray()\r
78     {\r
79         SequenceI[] s = new SequenceI[seqs.size()];\r
80 \r
81         for (int i = 0; i < seqs.size(); i++)\r
82         {\r
83             s[i] = (SequenceI) seqs.elementAt(i);\r
84         }\r
85 \r
86         return s;\r
87     }\r
88 \r
89     public void addAnnotations(Alignment al)\r
90     {\r
91       for(int i=0; i<annotations.size(); i++)\r
92       {\r
93         al.addAnnotation(\r
94             (AlignmentAnnotation)annotations.elementAt(i)\r
95             );\r
96       }\r
97 \r
98     }\r
99 \r
100     /**\r
101      * Initialise objects to store sequence data in.\r
102      */\r
103     protected void initData()\r
104     {\r
105         seqs = new Vector();\r
106         annotations = new Vector();\r
107     }\r
108 \r
109     /**\r
110      * DOCUMENT ME!\r
111      *\r
112      * @param s DOCUMENT ME!\r
113      */\r
114     protected void setSeqs(SequenceI[] s)\r
115     {\r
116         seqs = new Vector();\r
117 \r
118         for (int i = 0; i < s.length; i++)\r
119         {\r
120             seqs.addElement(s[i]);\r
121         }\r
122     }\r
123 \r
124 \r
125 \r
126     /**\r
127      * This method must be implemented to parse the contents of the file.\r
128      */\r
129     public abstract void parse() throws IOException;\r
130 \r
131     /**\r
132      * Print out in alignment file format the Sequences in the seqs Vector.\r
133      */\r
134     public abstract String print();\r
135 \r
136     public void addJVSuffix(boolean b)\r
137     {\r
138       jvSuffix = b;\r
139     }\r
140 \r
141     /**\r
142      * A general parser for ids.\r
143      *\r
144      * @String id Id to be parsed\r
145      */\r
146     Sequence parseId(String id)\r
147     {\r
148       Sequence seq = null;\r
149       id = id.trim();\r
150       int space = id.indexOf(" ");\r
151       if(space>-1)\r
152       {\r
153         seq = new Sequence(id.substring(0, space),"");\r
154         seq.setDescription(id.substring(space+1));\r
155       }\r
156       else\r
157       {\r
158         seq = new Sequence(id, "");\r
159       }\r
160 \r
161       return seq;\r
162     }\r
163 \r
164     /**\r
165      * Creates the output id.\r
166      * Adds prefix Uniprot format source|id\r
167      * And suffix Jalview /start-end\r
168      *\r
169      * @String id Id to be parsed\r
170      */\r
171     String printId(SequenceI seq)\r
172     {\r
173      return seq.getDisplayId(jvSuffix);\r
174     }\r
175 \r
176 }\r