2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.io.packed;
21 import jalview.datamodel.AlignmentI;
22 import jalview.io.AppletFormatAdapter;
23 import jalview.io.FileParse;
24 import jalview.io.FormatAdapter;
25 import jalview.io.IdentifyFile;
26 import jalview.io.packed.DataProvider.JvDataType;
28 import java.io.BufferedReader;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.Hashtable;
32 import java.util.List;
34 public class ParsePackedSet
38 * return results as a series of jalview.datamodel objects suitable for
42 * - context which is updated with new data
45 * @return list of data objects added to context
48 public Object[] getAlignment(JalviewDataset context,
49 Iterable<DataProvider> files) throws Exception
51 List<Object> rslt = new ArrayList<Object>();
54 context = new JalviewDataset();
56 boolean deuniquify = false;
57 for (DataProvider dta : files)
59 Exception exerror = null;
61 FileParse src = dta.getDataSource();
62 if (dta.getType().equals(DataProvider.JvDataType.ALIGNMENT))
67 fmt = new IdentifyFile().Identify(src, false);
68 } catch (Exception ex)
71 errmsg = "Couldn't identify alignment format.";
76 if (!FormatAdapter.isValidIOFormat(fmt, false))
83 // parse the alignment
87 al = new FormatAdapter().readFromFile(src, fmt);
90 errmsg = "Failed to parse alignment from result set";
95 // deuniquify and construct/merge additional dataset entries if
97 context.addAlignment(al);
98 context.updateSetModified(true);
105 if (dta.getType().equals(JvDataType.ANNOTATION))
107 if (!context.hasAlignments())
109 errmsg = "No alignment or sequence dataset to associate annotation with.";
110 // could duplicate the dataset reference here as default behaviour for
111 // sequence associated annotation ?
116 if (src.getReader() instanceof BufferedReader)
118 br = (BufferedReader) src.getReader();
122 br = new BufferedReader(src.getReader());
124 if (new jalview.io.AnnotationFile().parseAnnotationFrom(
125 context.getLastAlignment(), br))
127 context.updateSetModified(true);
131 errmsg = "Annotation file contained no data.";
134 } catch (Exception e)
136 errmsg = ((errmsg == null) ? "" : errmsg)
137 + "Failed to parse the annotation file associated with the alignment.";
141 if (dta.getType().equals(JvDataType.SEQASSOCATED))
143 if (!context.hasSequenceAssoc())
145 errmsg = "No sequence to associate data with.";
148 errmsg = "parsing of sequence associated data is not implemented";
149 exerror = new Exception(errmsg);
151 if (dta.getType().equals(JvDataType.FEATURES))
153 // check the context has a place to store feature rendering definitions,
154 // if not, create one.
155 if (context.featureColours == null)
157 context.featureColours = new Hashtable();
161 jalview.io.FeaturesFile ff = new jalview.io.FeaturesFile(src);
162 context.updateSetModified(ff.parse(context.getLastAlignment(),
163 context.featureColours, false, context.relaxedIdMatching));
164 } catch (Exception e)
166 errmsg = ("Failed to parse the Features file associated with the alignment.");
170 if (dta.getType().equals(JvDataType.TREE))
174 jalview.io.NewickFile nf = new jalview.io.NewickFile(src);
182 // do association to current alignment.
184 context.addTreeFromFile(nf);
186 context.updateSetModified(true);
188 } catch (Exception e)
190 errmsg = ("Failed to parse the treeFile associated with the result.");
197 if (errmsg != null && errmsg.length() > 0)
199 throw new IOException(errmsg, exerror);
203 throw new IOException(errmsg, exerror);
208 if (errmsg != null && errmsg.length() > 0)
210 throw new IOException(errmsg);
216 context.getLastAlignmentSet().deuniquifyAlignment();
218 return rslt.toArray();
222 * simple command line test. Arguments should be one or more pairs of
223 * <DataProvider.JvDataType> <Filename> arguments. The routine will attempt to
224 * read each source in turn, and report what kind of Jalview datamodel objects
229 public static void main(String args[])
231 // make data providers from the set of keys/files
233 List<DataProvider> dp = new ArrayList<DataProvider>();
234 while ((i + 1) < args.length)
236 String type = args[i++];
237 final String file = args[i++];
238 final JvDataType jtype = DataProvider.JvDataType.valueOf(type
245 fp = new FileParse(file, AppletFormatAdapter.checkProtocol(file));
246 } catch (Exception e)
248 System.err.println("Couldn't handle datasource of type " + jtype
249 + " using URI " + file);
253 dp.add(new SimpleDataProvider(jtype, fp, null));
257 System.out.println("Couldn't parse source type token '"
258 + type.toUpperCase() + "'");
263 System.out.print("** WARNING\nIgnoring unused arguments:\n");
264 while (i < args.length)
266 System.out.print(" " + args[i]);
268 System.out.print("\n");
271 System.out.println("Now trying to parse set:");
272 JalviewDataset context;
277 newdm = (pps = new ParsePackedSet()).getAlignment(
278 context = new JalviewDataset(), dp);
279 } catch (Exception e)
281 System.out.println("Test failed for these arguments.\n");
282 e.printStackTrace(System.out);
287 for (Object o : newdm)
289 System.out.println("Will need to create an " + o.getClass());
292 // now test uniquify/deuniquify stuff
293 // uniquify alignment and write alignment, annotation, features, and trees
295 // import with deuniquify info, and compare results to input.
300 if (context.getLastAlignmentSet().isModified())
303 .println("Initial alignment set was modified and any associated views should be updated.");