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