Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.io

File AlignFile.java

 
testFileParser: Problem opening 1GAQ.txt : FILE CANNOT BE OPENED FOR READING
 

Coverage histogram

../../img/srcFileCovDistChart8.png
19% of files have more coverage

Code metrics

30
71
25
1
436
218
42
0.59
2.84
25
1.68

Classes

Class Line # Actions
AlignFile 43 71 42 25
0.801587380.2%
 

Contributing tests

This file is covered by 170 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
7    * Jalview is free software: you can redistribute it and/or
8    * modify it under the terms of the GNU General Public License
9    * as published by the Free Software Foundation, either version 3
10    * of the License, or (at your option) any later version.
11    *
12    * Jalview is distributed in the hope that it will be useful, but
13    * WITHOUT ANY WARRANTY; without even the implied warranty
14    * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15    * PURPOSE. See the GNU General Public License for more details.
16    *
17    * You should have received a copy of the GNU General Public License
18    * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19    * The Jalview Authors are detailed in the 'AUTHORS' file.
20    */
21    package jalview.io;
22   
23    import jalview.datamodel.AlignmentAnnotation;
24    import jalview.datamodel.AlignmentI;
25    import jalview.datamodel.Sequence;
26    import jalview.datamodel.SequenceGroup;
27    import jalview.datamodel.SequenceI;
28    import jalview.util.MessageManager;
29   
30    import java.io.IOException;
31    import java.util.ArrayList;
32    import java.util.Enumeration;
33    import java.util.Hashtable;
34    import java.util.List;
35    import java.util.Vector;
36   
37    /**
38    * DOCUMENT ME!
39    *
40    * @author $author$
41    * @version $Revision$
42    */
 
43    public abstract class AlignFile extends FileParse
44    implements AlignmentFileReaderI, AlignmentFileWriterI
45    {
46    int noSeqs = 0;
47   
48    int maxLength = 0;
49   
50    /**
51    * Sequences to be added to form a new alignment. TODO: remove vector in this
52    * class
53    */
54    protected Vector<SequenceI> seqs;
55   
56    /**
57    * annotation to be added to generated alignment object
58    */
59    protected Vector<AlignmentAnnotation> annotations;
60   
61    /**
62    * SequenceGroups to be added to the alignment object
63    */
64    protected List<SequenceGroup> seqGroups;
65   
66    /**
67    * Properties to be added to generated alignment object
68    */
69    private Hashtable properties;
70   
71    long start;
72   
73    long end;
74   
75    /**
76    * true if parse() has been called
77    */
78    private boolean parseCalled = false;
79   
80    private boolean parseImmediately = true;
81   
82    /**
83    * @return if doParse() was called at construction time
84    */
 
85  92 toggle protected boolean isParseImmediately()
86    {
87  92 return parseImmediately;
88    }
89   
90    /**
91    * Creates a new AlignFile object.
92    */
 
93  88 toggle public AlignFile()
94    {
95    // Shouldn't we init data structures (JBPNote: not sure - initData is for
96    // initialising the structures used for reading from a datasource, and the
97    // bare constructor hasn't got any datasource)
98  88 initData();
99    }
100   
 
101  0 toggle public AlignFile(SequenceI[] seqs)
102    {
103  0 this();
104  0 setSeqs(seqs);
105    }
106   
107    /**
108    * Constructor which parses the data from a file of some specified type.
109    *
110    * @param dataObject
111    * Filename, URL or Pasted String to read from.
112    * @param sourceType
113    * What type of file to read from (File, URL, Pasted String)
114    */
 
115  26 toggle public AlignFile(Object dataObject, DataSourceType sourceType)
116    throws IOException
117    {
118  26 this(true, dataObject, sourceType);
119    }
120   
121    /**
122    * Constructor which (optionally delays) parsing of data from a file of some
123    * specified type.
124    *
125    * @param parseImmediately
126    * if false, need to call 'doParse()' to begin parsing data
127    * @param dataObject
128    * Filename, URL or Pasted String to read from.
129    * @param sourceType
130    * What type of file to read from (File, URL)
131    * @throws IOException
132    */
 
133  120 toggle public AlignFile(boolean parseImmediately, Object dataObject,
134    DataSourceType sourceType) throws IOException
135    {
136    // BH allows File or String
137  120 Test failure here super(dataObject, sourceType);
138  120 initData();
139  120 if (parseImmediately)
140    {
141  27 doParse();
142    }
143    }
144   
145    /**
146    * Attempt to read from the position where some other parsing process left
147    * off.
148    *
149    * @param source
150    * @throws IOException
151    */
 
152  222 toggle public AlignFile(FileParse source) throws IOException
153    {
154  222 this(true, source);
155    }
156   
157    /**
158    * Construct a new parser to read from the position where some other parsing
159    * process left
160    *
161    * @param parseImmediately
162    * if false, need to call 'doParse()' to begin parsing data
163    * @param source
164    */
 
165  249 toggle public AlignFile(boolean parseImmediately, FileParse source)
166    throws IOException
167    {
168  249 super(source);
169  249 initData();
170   
171    // stash flag in case parse needs to know if it has to autoconfigure or was
172    // configured after construction
173  249 this.parseImmediately = parseImmediately;
174   
175  249 if (parseImmediately)
176    {
177  222 doParse();
178    }
179    }
180   
181    /**
182    * called if parsing was delayed till after parser was constructed
183    *
184    * @throws IOException
185    */
 
186  332 toggle public void doParse() throws IOException
187    {
188  332 if (parseCalled)
189    {
190  0 throw new IOException(
191    "Implementation error: Parser called twice for same data.\n"
192    + "Need to call initData() again before parsing can be reattempted.");
193    }
194  332 parseCalled = true;
195  332 parse();
196    }
197   
198    /**
199    * Return the seqs Vector
200    */
 
201  25 toggle public Vector<SequenceI> getSeqs()
202    {
203  25 return seqs;
204    }
205   
 
206  227 toggle public List<SequenceGroup> getSeqGroups()
207    {
208  227 return seqGroups;
209    }
210   
211    /**
212    * Return the Sequences in the seqs Vector as an array of Sequences
213    */
 
214  236 toggle @Override
215    public SequenceI[] getSeqsAsArray()
216    {
217  236 SequenceI[] s = new SequenceI[seqs.size()];
218   
219  2563 for (int i = 0; i < seqs.size(); i++)
220    {
221  2327 s[i] = seqs.elementAt(i);
222    }
223   
224  236 return s;
225    }
226   
227    /**
228    * called by AppletFormatAdapter to generate an annotated alignment, rather
229    * than bare sequences.
230    *
231    * @param al
232    */
 
233  233 toggle @Override
234    public void addAnnotations(AlignmentI al)
235    {
236  233 addProperties(al);
237  642 for (int i = 0; i < annotations.size(); i++)
238    {
239    // detect if annotations.elementAt(i) rna secondary structure
240    // if so then do:
241    /*
242    * SequenceFeature[] pairArray =
243    * Rna.GetBasePairsFromAlignmentAnnotation(annotations.elementAt(i));
244    * Rna.HelixMap(pairArray);
245    */
246  409 AlignmentAnnotation an = annotations.elementAt(i);
247  409 an.validateRangeAndDisplay();
248  409 al.addAnnotation(an);
249    }
250   
251    }
252   
253    /**
254    * register sequence groups on the alignment for **output**
255    *
256    * @param al
257    */
 
258  0 toggle public void addSeqGroups(AlignmentI al)
259    {
260  0 this.seqGroups = al.getGroups();
261   
262    }
263   
264    /**
265    * Add any additional information extracted from the file to the alignment
266    * properties.
267    *
268    * @note implicitly called by addAnnotations()
269    * @param al
270    */
 
271  234 toggle public void addProperties(AlignmentI al)
272    {
273  234 if (properties != null && properties.size() > 0)
274    {
275  7 Enumeration keys = properties.keys();
276  7 Enumeration vals = properties.elements();
277  116 while (keys.hasMoreElements())
278    {
279  109 al.setProperty(keys.nextElement(), vals.nextElement());
280    }
281    }
282    }
283   
284    /**
285    * Store a non-null key-value pair in a hashtable used to set alignment
286    * properties note: null keys will raise an error, null values will result in
287    * the key/value pair being silently ignored.
288    *
289    * @param key
290    * - non-null key object
291    * @param value
292    * - non-null value
293    */
 
294  151 toggle protected void setAlignmentProperty(Object key, Object value)
295    {
296  151 if (key == null)
297    {
298  0 throw new Error(MessageManager.getString(
299    "error.implementation_error_cannot_have_null_alignment"));
300    }
301  151 if (value == null)
302    {
303  0 return; // null properties are ignored.
304    }
305  151 if (properties == null)
306    {
307  7 properties = new Hashtable();
308    }
309  151 properties.put(key, value);
310    }
311   
 
312  43 toggle protected Object getAlignmentProperty(Object key)
313    {
314  43 if (properties != null && key != null)
315    {
316  12 return properties.get(key);
317    }
318  31 return null;
319    }
320   
321    /**
322    * Initialise objects to store sequence data in.
323    */
 
324  457 toggle protected void initData()
325    {
326  457 seqs = new Vector<SequenceI>();
327  457 annotations = new Vector<AlignmentAnnotation>();
328  457 seqGroups = new ArrayList<SequenceGroup>();
329  457 parseCalled = false;
330    }
331   
332    /**
333    * DOCUMENT ME!
334    *
335    * @param s
336    * DOCUMENT ME!
337    */
 
338  3 toggle @Override
339    public void setSeqs(SequenceI[] s)
340    {
341  3 seqs = new Vector<SequenceI>();
342   
343  9 for (int i = 0; i < s.length; i++)
344    {
345  6 seqs.addElement(s[i]);
346    }
347    }
348   
349    /**
350    * This method must be implemented to parse the contents of the file.
351    */
352    public abstract void parse() throws IOException;
353   
354    /**
355    * A general parser for ids.
356    *
357    * @String id Id to be parsed
358    */
 
359  1332 toggle Sequence parseId(String id)
360    {
361  1332 Sequence seq = null;
362  1332 id = id.trim();
363  1332 int space = id.indexOf(" ");
364  1332 if (space > -1)
365    {
366  941 seq = new Sequence(id.substring(0, space), "");
367  941 String desc = id.substring(space + 1);
368  941 seq.setDescription(desc);
369   
370    /*
371    * it is tempting to parse Ensembl style gene description e.g.
372    * chromosome:GRCh38:7:140696688:140721955:1 and set the
373    * start position of the sequence, but this causes much confusion
374    * for reverse strand feature locations
375    */
376    }
377    else
378    {
379  391 seq = new Sequence(id, "");
380    }
381   
382  1332 return seq;
383    }
384   
385    /**
386    * Creates the output id. Adds prefix Uniprot format source|id and optionally
387    * suffix Jalview /start-end
388    *
389    * @param jvsuffix
390    *
391    * @String id Id to be parsed
392    */
 
393  2896 toggle String printId(SequenceI seq, boolean jvsuffix)
394    {
395  2896 return seq.getDisplayId(jvsuffix);
396    }
397   
 
398  0 toggle String printId(SequenceI seq)
399    {
400  0 return printId(seq, true);
401    }
402   
403    /**
404    * vector of String[] treeName, newickString pairs
405    */
406    Vector<String[]> newickStrings = null;
407   
 
408  0 toggle protected void addNewickTree(String treeName, String newickString)
409    {
410  0 if (newickStrings == null)
411    {
412  0 newickStrings = new Vector<String[]>();
413    }
414  0 newickStrings.addElement(new String[] { treeName, newickString });
415    }
416   
 
417  0 toggle protected int getTreeCount()
418    {
419  0 return newickStrings == null ? 0 : newickStrings.size();
420    }
421   
 
422  227 toggle @Override
423    public void addGroups(AlignmentI al)
424    {
425   
426  227 for (SequenceGroup sg : getSeqGroups())
427    {
428  5 al.addGroup(sg);
429    }
430    }
431   
 
432  0 toggle protected void addSequence(SequenceI seq)
433    {
434  0 seqs.add(seq);
435    }
436    }