1ec2805c43daa7c53046c90a9356b69565747a19
[jalview.git] / src / jalview / io / AlignFile.java
1 /*\r
2 * Jalview - A Sequence Alignment Editor and Viewer\r
3 * Copyright (C) 2005 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 headers;\r
40     long start;\r
41     long end;\r
42     boolean dbPrefix = true;\r
43     boolean jvSuffix = true;\r
44 \r
45     /**\r
46      * Creates a new AlignFile object.\r
47      */\r
48     public AlignFile()\r
49     {\r
50     }\r
51 \r
52     /**\r
53      * Creates a new AlignFile object.\r
54      *\r
55      * @param inStr DOCUMENT ME!\r
56      */\r
57     public AlignFile(String inStr)\r
58     {\r
59         initData();\r
60         System.out.println("is this ever called??");\r
61 \r
62         try\r
63         {\r
64             parse();\r
65         }\r
66         catch (Exception ex)\r
67         {\r
68         }\r
69     }\r
70 \r
71     /**\r
72      * Constructor which parses the data from a file of some specified type.\r
73      * @param inFile Filename to read from.\r
74      * @param type   What type of file to read from (File, URL)\r
75      */\r
76     public AlignFile(String inFile, String type) throws IOException\r
77     {\r
78         super(inFile, type);\r
79 \r
80         initData();\r
81 \r
82         parse();\r
83     }\r
84 \r
85     /**\r
86      * Return the seqs Vector\r
87      */\r
88     public Vector getSeqs()\r
89     {\r
90         return seqs;\r
91     }\r
92 \r
93     /**\r
94      * Return the Sequences in the seqs Vector as an array of Sequences\r
95      */\r
96     public SequenceI[] getSeqsAsArray()\r
97     {\r
98         SequenceI[] s = new SequenceI[seqs.size()];\r
99 \r
100         for (int i = 0; i < seqs.size(); i++)\r
101         {\r
102             s[i] = (SequenceI) seqs.elementAt(i);\r
103         }\r
104 \r
105         return s;\r
106     }\r
107 \r
108     /**\r
109      * Initialise objects to store sequence data in.\r
110      */\r
111     protected void initData()\r
112     {\r
113         seqs = new Vector();\r
114         headers = new Vector();\r
115     }\r
116 \r
117     /**\r
118      * DOCUMENT ME!\r
119      *\r
120      * @param s DOCUMENT ME!\r
121      */\r
122     protected void setSeqs(SequenceI[] s)\r
123     {\r
124         seqs = new Vector();\r
125 \r
126         for (int i = 0; i < s.length; i++)\r
127         {\r
128             seqs.addElement(s[i]);\r
129         }\r
130     }\r
131 \r
132     // Checks whether sequence is valid aa characters\r
133     protected boolean isValidProteinSequence(String sequence)\r
134     {\r
135         for (int i = 0; i < sequence.length(); i++)\r
136             if (!jalview.schemes.ResidueProperties.aaHash.containsKey(\r
137                         String.valueOf(sequence.charAt(i))))\r
138             {\r
139                 return false;\r
140             }\r
141 \r
142         return true;\r
143     }\r
144 \r
145     /**\r
146      * This method must be implemented to parse the contents of the file.\r
147      */\r
148     public abstract void parse() throws IOException;\r
149 \r
150     /**\r
151      * Print out in alignment file format the Sequences in the seqs Vector.\r
152      */\r
153     public abstract String print();\r
154 \r
155     public void addDBPrefix(boolean b)\r
156     {\r
157       dbPrefix = b;\r
158     }\r
159 \r
160     public void addJVSuffix(boolean b)\r
161     {\r
162       jvSuffix = b;\r
163     }\r
164 \r
165     /**\r
166      * A general parser for ids. Will look for dbrefs in\r
167      * Uniprot format source|id\r
168      * And also Jalview /start-end\r
169      *\r
170      * @String id Id to be parsed\r
171      */\r
172     Sequence parseId(String id)\r
173     {\r
174       Sequence seq = null;\r
175       id = id.trim();\r
176       int space = id.indexOf(" ");\r
177       if(space>-1)\r
178       {\r
179         seq = new Sequence(id.substring(0, space),"");\r
180         seq.setDescription(id.substring(space+1));\r
181       }\r
182       else\r
183       {\r
184         seq = new Sequence(id, "");\r
185       }\r
186 \r
187 \r
188       return seq;\r
189     }\r
190 \r
191     /**\r
192      * Creates the output id.\r
193      * Adds prefix Uniprot format source|id\r
194      * And suffix Jalview /start-end\r
195      *\r
196      * @String id Id to be parsed\r
197      */\r
198     String printId(SequenceI seq)\r
199     {\r
200       return seq.getDisplayId(dbPrefix, jvSuffix);\r
201     }\r
202 \r
203 }\r