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