2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9.0b2)
3 * Copyright (C) 2015 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 // TODO: add columnSelection to context
127 if (new jalview.io.AnnotationFile().parseAnnotationFrom(
128 context.getLastAlignment(), null, br))
130 context.updateSetModified(true);
134 errmsg = "Annotation file contained no data.";
137 } catch (Exception e)
139 errmsg = ((errmsg == null) ? "" : errmsg)
140 + "Failed to parse the annotation file associated with the alignment.";
144 if (dta.getType().equals(JvDataType.SEQASSOCATED))
146 if (!context.hasSequenceAssoc())
148 errmsg = "No sequence to associate data with.";
151 errmsg = "parsing of sequence associated data is not implemented";
152 exerror = new Exception(errmsg);
154 if (dta.getType().equals(JvDataType.FEATURES))
156 // check the context has a place to store feature rendering definitions,
157 // if not, create one.
158 if (context.featureColours == null)
160 context.featureColours = new Hashtable();
164 jalview.io.FeaturesFile ff = new jalview.io.FeaturesFile(src);
165 context.updateSetModified(ff.parse(context.getLastAlignment(),
166 context.featureColours, false, context.relaxedIdMatching));
167 } catch (Exception e)
169 errmsg = ("Failed to parse the Features file associated with the alignment.");
173 if (dta.getType().equals(JvDataType.TREE))
177 jalview.io.NewickFile nf = new jalview.io.NewickFile(src);
185 // do association to current alignment.
187 context.addTreeFromFile(nf);
189 context.updateSetModified(true);
191 } catch (Exception e)
193 errmsg = ("Failed to parse the treeFile associated with the result.");
200 if (errmsg != null && errmsg.length() > 0)
202 throw new IOException(errmsg, exerror);
206 throw new IOException(errmsg, exerror);
211 if (errmsg != null && errmsg.length() > 0)
213 throw new IOException(errmsg);
219 context.getLastAlignmentSet().deuniquifyAlignment();
221 return rslt.toArray();
225 * simple command line test. Arguments should be one or more pairs of
226 * <DataProvider.JvDataType> <Filename> arguments. The routine will attempt to
227 * read each source in turn, and report what kind of Jalview datamodel objects
232 public static void main(String args[])
234 // make data providers from the set of keys/files
236 List<DataProvider> dp = new ArrayList<DataProvider>();
237 while ((i + 1) < args.length)
239 String type = args[i++];
240 final String file = args[i++];
241 final JvDataType jtype = DataProvider.JvDataType.valueOf(type
248 fp = new FileParse(file, AppletFormatAdapter.checkProtocol(file));
249 } catch (Exception e)
251 System.err.println("Couldn't handle datasource of type " + jtype
252 + " using URI " + file);
256 dp.add(new SimpleDataProvider(jtype, fp, null));
260 System.out.println("Couldn't parse source type token '"
261 + type.toUpperCase() + "'");
266 System.out.print("** WARNING\nIgnoring unused arguments:\n");
267 while (i < args.length)
269 System.out.print(" " + args[i]);
271 System.out.print("\n");
274 System.out.println("Now trying to parse set:");
275 JalviewDataset context;
280 newdm = (pps = new ParsePackedSet()).getAlignment(
281 context = new JalviewDataset(), dp);
282 } catch (Exception e)
284 System.out.println("Test failed for these arguments.\n");
285 e.printStackTrace(System.out);
290 for (Object o : newdm)
292 System.out.println("Will need to create an " + o.getClass());
295 // now test uniquify/deuniquify stuff
296 // uniquify alignment and write alignment, annotation, features, and trees
298 // import with deuniquify info, and compare results to input.
303 if (context.getLastAlignmentSet().isModified())
306 .println("Initial alignment set was modified and any associated views should be updated.");