2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
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
10 * of the License, or (at your option) any later version.
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.
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.
21 package jalview.io.packed;
23 import jalview.datamodel.AlignmentI;
24 import jalview.io.AppletFormatAdapter;
25 import jalview.io.FileParse;
26 import jalview.io.FormatAdapter;
27 import jalview.io.IdentifyFile;
28 import jalview.io.packed.DataProvider.JvDataType;
30 import java.io.BufferedReader;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.Hashtable;
34 import java.util.List;
36 public class ParsePackedSet
40 * return results as a series of jalview.datamodel objects suitable for
44 * - context which is updated with new data
47 * @return list of data objects added to context
50 public Object[] getAlignment(JalviewDataset context,
51 Iterable<DataProvider> files) throws Exception
53 List<Object> rslt = new ArrayList<Object>();
56 context = new JalviewDataset();
58 boolean deuniquify = false;
59 for (DataProvider dta : files)
61 Exception exerror = null;
63 FileParse src = dta.getDataSource();
64 if (dta.getType().equals(DataProvider.JvDataType.ALIGNMENT))
69 fmt = new IdentifyFile().Identify(src, false);
70 } catch (Exception ex)
73 errmsg = "Couldn't identify alignment format.";
78 if (!FormatAdapter.isValidIOFormat(fmt, false))
85 // parse the alignment
89 al = new FormatAdapter().readFromFile(src, fmt);
92 errmsg = "Failed to parse alignment from result set";
97 // deuniquify and construct/merge additional dataset entries if
99 context.addAlignment(al);
100 context.updateSetModified(true);
107 if (dta.getType().equals(JvDataType.ANNOTATION))
109 if (!context.hasAlignments())
111 errmsg = "No alignment or sequence dataset to associate annotation with.";
112 // could duplicate the dataset reference here as default behaviour for
113 // sequence associated annotation ?
118 if (src.getReader() instanceof BufferedReader)
120 br = (BufferedReader) src.getReader();
124 br = new BufferedReader(src.getReader());
126 if (new jalview.io.AnnotationFile().parseAnnotationFrom(
127 context.getLastAlignment(), br))
129 context.updateSetModified(true);
133 errmsg = "Annotation file contained no data.";
136 } catch (Exception e)
138 errmsg = ((errmsg == null) ? "" : errmsg)
139 + "Failed to parse the annotation file associated with the alignment.";
143 if (dta.getType().equals(JvDataType.SEQASSOCATED))
145 if (!context.hasSequenceAssoc())
147 errmsg = "No sequence to associate data with.";
150 errmsg = "parsing of sequence associated data is not implemented";
151 exerror = new Exception(errmsg);
153 if (dta.getType().equals(JvDataType.FEATURES))
155 // check the context has a place to store feature rendering definitions,
156 // if not, create one.
157 if (context.featureColours == null)
159 context.featureColours = new Hashtable();
163 jalview.io.FeaturesFile ff = new jalview.io.FeaturesFile(src);
164 context.updateSetModified(ff.parse(context.getLastAlignment(),
165 context.featureColours, false, context.relaxedIdMatching));
166 } catch (Exception e)
168 errmsg = ("Failed to parse the Features file associated with the alignment.");
172 if (dta.getType().equals(JvDataType.TREE))
176 jalview.io.NewickFile nf = new jalview.io.NewickFile(src);
184 // do association to current alignment.
186 context.addTreeFromFile(nf);
188 context.updateSetModified(true);
190 } catch (Exception e)
192 errmsg = ("Failed to parse the treeFile associated with the result.");
199 if (errmsg != null && errmsg.length() > 0)
201 throw new IOException(errmsg, exerror);
205 throw new IOException(errmsg, exerror);
210 if (errmsg != null && errmsg.length() > 0)
212 throw new IOException(errmsg);
218 context.getLastAlignmentSet().deuniquifyAlignment();
220 return rslt.toArray();
224 * simple command line test. Arguments should be one or more pairs of
225 * <DataProvider.JvDataType> <Filename> arguments. The routine will attempt to
226 * read each source in turn, and report what kind of Jalview datamodel objects
231 public static void main(String args[])
233 // make data providers from the set of keys/files
235 List<DataProvider> dp = new ArrayList<DataProvider>();
236 while ((i + 1) < args.length)
238 String type = args[i++];
239 final String file = args[i++];
240 final JvDataType jtype = DataProvider.JvDataType.valueOf(type
247 fp = new FileParse(file, AppletFormatAdapter.checkProtocol(file));
248 } catch (Exception e)
250 System.err.println("Couldn't handle datasource of type " + jtype
251 + " using URI " + file);
255 dp.add(new SimpleDataProvider(jtype, fp, null));
259 System.out.println("Couldn't parse source type token '"
260 + type.toUpperCase() + "'");
265 System.out.print("** WARNING\nIgnoring unused arguments:\n");
266 while (i < args.length)
268 System.out.print(" " + args[i]);
270 System.out.print("\n");
273 System.out.println("Now trying to parse set:");
274 JalviewDataset context;
279 newdm = (pps = new ParsePackedSet()).getAlignment(
280 context = new JalviewDataset(), dp);
281 } catch (Exception e)
283 System.out.println("Test failed for these arguments.\n");
284 e.printStackTrace(System.out);
289 for (Object o : newdm)
291 System.out.println("Will need to create an " + o.getClass());
294 // now test uniquify/deuniquify stuff
295 // uniquify alignment and write alignment, annotation, features, and trees
297 // import with deuniquify info, and compare results to input.
302 if (context.getLastAlignmentSet().isModified())
305 .println("Initial alignment set was modified and any associated views should be updated.");