2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.io.packed;
20 import jalview.datamodel.AlignmentI;
21 import jalview.io.AppletFormatAdapter;
22 import jalview.io.FileParse;
23 import jalview.io.FormatAdapter;
24 import jalview.io.IdentifyFile;
25 import jalview.io.packed.DataProvider.JvDataType;
27 import java.io.BufferedReader;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.Hashtable;
31 import java.util.List;
33 public class ParsePackedSet
37 * return results as a series of jalview.datamodel objects suitable for
41 * - context which is updated with new data
44 * @return list of data objects added to context
47 public Object[] getAlignment(JalviewDataset context,
48 Iterable<DataProvider> files) throws Exception
50 List<Object> rslt = new ArrayList<Object>();
53 context = new JalviewDataset();
55 boolean deuniquify = false;
56 for (DataProvider dta : files)
58 Exception exerror = null;
60 FileParse src = dta.getDataSource();
61 if (dta.getType().equals(DataProvider.JvDataType.ALIGNMENT))
66 fmt = new IdentifyFile().Identify(src, false);
67 } catch (Exception ex)
70 errmsg = "Couldn't identify alignment format.";
75 if (!FormatAdapter.isValidIOFormat(fmt, false))
82 // parse the alignment
86 al = new FormatAdapter().readFromFile(src, fmt);
89 errmsg = "Failed to parse alignment from result set";
94 // deuniquify and construct/merge additional dataset entries if
96 context.addAlignment(al);
97 context.updateSetModified(true);
104 if (dta.getType().equals(JvDataType.ANNOTATION))
106 if (!context.hasAlignments())
108 errmsg = "No alignment or sequence dataset to associate annotation with.";
109 // could duplicate the dataset reference here as default behaviour for
110 // sequence associated annotation ?
115 if (src.getReader() instanceof BufferedReader)
117 br = (BufferedReader) src.getReader();
121 br = new BufferedReader(src.getReader());
123 if (new jalview.io.AnnotationFile().parseAnnotationFrom(
124 context.getLastAlignment(), br))
126 context.updateSetModified(true);
130 errmsg = "Annotation file contained no data.";
133 } catch (Exception e)
135 errmsg = ((errmsg == null) ? "" : errmsg)
136 + "Failed to parse the annotation file associated with the alignment.";
140 if (dta.getType().equals(JvDataType.SEQASSOCATED))
142 if (!context.hasSequenceAssoc())
144 errmsg = "No sequence to associate data with.";
147 errmsg = "parsing of sequence associated data is not implemented";
148 exerror = new Exception(errmsg);
150 if (dta.getType().equals(JvDataType.FEATURES))
152 // check the context has a place to store feature rendering definitions,
153 // if not, create one.
154 if (context.featureColours == null)
156 context.featureColours = new Hashtable();
160 jalview.io.FeaturesFile ff = new jalview.io.FeaturesFile(src);
161 context.updateSetModified(ff.parse(context.getLastAlignment(),
162 context.featureColours, false, context.relaxedIdMatching));
163 } catch (Exception e)
165 errmsg = ("Failed to parse the Features file associated with the alignment.");
169 if (dta.getType().equals(JvDataType.TREE))
173 jalview.io.NewickFile nf = new jalview.io.NewickFile(src);
181 // do association to current alignment.
183 context.addTreeFromFile(nf);
185 context.updateSetModified(true);
187 } catch (Exception e)
189 errmsg = ("Failed to parse the treeFile associated with the result.");
196 if (errmsg != null && errmsg.length() > 0)
198 throw new IOException(errmsg, exerror);
202 throw new IOException(errmsg, exerror);
207 if (errmsg != null && errmsg.length() > 0)
209 throw new IOException(errmsg);
215 context.getLastAlignmentSet().deuniquifyAlignment();
217 return rslt.toArray();
221 * simple command line test. Arguments should be one or more pairs of
222 * <DataProvider.JvDataType> <Filename> arguments. The routine will attempt to
223 * read each source in turn, and report what kind of Jalview datamodel objects
228 public static void main(String args[])
230 // make data providers from the set of keys/files
232 List<DataProvider> dp = new ArrayList<DataProvider>();
233 while ((i + 1) < args.length)
235 String type = args[i++];
236 final String file = args[i++];
237 final JvDataType jtype = DataProvider.JvDataType.valueOf(type
244 fp = new FileParse(file, AppletFormatAdapter.checkProtocol(file));
245 } catch (Exception e)
247 System.err.println("Couldn't handle datasource of type " + jtype
248 + " using URI " + file);
252 dp.add(new SimpleDataProvider(jtype, fp, null));
256 System.out.println("Couldn't parse source type token '"
257 + type.toUpperCase() + "'");
262 System.out.print("** WARNING\nIgnoring unused arguments:\n");
263 while (i < args.length)
265 System.out.print(" " + args[i]);
267 System.out.print("\n");
270 System.out.println("Now trying to parse set:");
271 JalviewDataset context;
276 newdm = (pps = new ParsePackedSet()).getAlignment(
277 context = new JalviewDataset(), dp);
278 } catch (Exception e)
280 System.out.println("Test failed for these arguments.\n");
281 e.printStackTrace(System.out);
286 for (Object o : newdm)
288 System.out.println("Will need to create an " + o.getClass());
291 // now test uniquify/deuniquify stuff
292 // uniquify alignment and write alignment, annotation, features, and trees
294 // import with deuniquify info, and compare results to input.
299 if (context.getLastAlignmentSet().isModified())
302 .println("Initial alignment set was modified and any associated views should be updated.");