2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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.api.FeatureColourI;
24 import jalview.datamodel.AlignmentI;
25 import jalview.io.AppletFormatAdapter;
26 import jalview.io.FileFormatI;
27 import jalview.io.FileParse;
28 import jalview.io.FormatAdapter;
29 import jalview.io.IdentifyFile;
30 import jalview.io.packed.DataProvider.JvDataType;
32 import java.io.BufferedReader;
33 import java.io.IOException;
34 import java.util.ArrayList;
35 import java.util.HashMap;
36 import java.util.List;
38 public class ParsePackedSet
42 * return results as a series of jalview.datamodel objects suitable for
46 * - context which is updated with new data
49 * @return list of data objects added to context
52 public Object[] getAlignment(JalviewDataset context,
53 Iterable<DataProvider> files) throws Exception
55 List<Object> rslt = new ArrayList<Object>();
58 context = new JalviewDataset();
60 boolean deuniquify = false;
61 for (DataProvider dta : files)
63 Exception exerror = null;
65 FileParse src = dta.getDataSource();
66 if (dta.getType().equals(DataProvider.JvDataType.ALIGNMENT))
68 FileFormatI fmt = null;
71 fmt = new IdentifyFile().identify(src, false);
72 } catch (Exception ex)
75 errmsg = "Couldn't identify alignment format.";
80 // parse the alignment
84 al = new FormatAdapter().readFromFile(src, fmt);
87 errmsg = "Failed to parse alignment from result set";
92 // deuniquify and construct/merge additional dataset entries if
94 context.addAlignment(al);
95 context.updateSetModified(true);
101 if (dta.getType().equals(JvDataType.ANNOTATION))
103 if (!context.hasAlignments())
105 errmsg = "No alignment or sequence dataset to associate annotation with.";
106 // could duplicate the dataset reference here as default behaviour for
107 // sequence associated annotation ?
112 if (src.getReader() instanceof BufferedReader)
114 br = (BufferedReader) src.getReader();
118 br = new BufferedReader(src.getReader());
120 // TODO: add columnSelection to context
121 if (new jalview.io.AnnotationFile().parseAnnotationFrom(
122 context.getLastAlignment(), null, br))
124 context.updateSetModified(true);
128 errmsg = "Annotation file contained no data.";
131 } catch (Exception e)
133 errmsg = ((errmsg == null) ? "" : errmsg)
134 + "Failed to parse the annotation file associated with the alignment.";
138 if (dta.getType().equals(JvDataType.SEQASSOCATED))
140 if (!context.hasSequenceAssoc())
142 errmsg = "No sequence to associate data with.";
145 errmsg = "parsing of sequence associated data is not implemented";
146 exerror = new Exception(errmsg);
148 if (dta.getType().equals(JvDataType.FEATURES))
150 // check the context has a place to store feature rendering definitions,
151 // if not, create one.
152 if (context.featureColours == null)
154 context.featureColours = new HashMap<String, FeatureColourI>();
158 jalview.io.FeaturesFile ff = new jalview.io.FeaturesFile(src);
159 context.updateSetModified(ff.parse(context.getLastAlignment(),
160 context.featureColours, false,
161 context.relaxedIdMatching));
162 } catch (Exception e)
164 errmsg = ("Failed to parse the Features file associated with the alignment.");
168 if (dta.getType().equals(JvDataType.TREE))
172 jalview.io.NewickFile nf = new jalview.io.NewickFile(src);
180 // do association to current alignment.
182 context.addTreeFromFile(nf);
184 context.updateSetModified(true);
186 } catch (Exception e)
188 errmsg = ("Failed to parse the treeFile associated with the result.");
195 if (errmsg != null && errmsg.length() > 0)
197 throw new IOException(errmsg, exerror);
201 throw new IOException(errmsg, exerror);
206 if (errmsg != null && errmsg.length() > 0)
208 throw new IOException(errmsg);
214 context.getLastAlignmentSet().deuniquifyAlignment();
216 return rslt.toArray();
220 * simple command line test. Arguments should be one or more pairs of
221 * <DataProvider.JvDataType> <Filename> arguments. The routine will attempt to
222 * read each source in turn, and report what kind of Jalview datamodel objects
227 public static void main(String args[])
229 // make data providers from the set of keys/files
231 List<DataProvider> dp = new ArrayList<DataProvider>();
232 while ((i + 1) < args.length)
234 String type = args[i++];
235 final String file = args[i++];
236 final JvDataType jtype = DataProvider.JvDataType
237 .valueOf(type.toUpperCase());
243 fp = new FileParse(file, AppletFormatAdapter.checkProtocol(file));
244 } catch (Exception e)
246 System.err.println("Couldn't handle datasource of type " + jtype
247 + " using URI " + file);
251 dp.add(new SimpleDataProvider(jtype, fp, null));
255 System.out.println("Couldn't parse source type token '"
256 + type.toUpperCase() + "'");
261 System.out.print("** WARNING\nIgnoring unused arguments:\n");
262 while (i < args.length)
264 System.out.print(" " + args[i]);
266 System.out.print("\n");
269 System.out.println("Now trying to parse set:");
270 JalviewDataset context;
275 newdm = (pps = new ParsePackedSet())
276 .getAlignment(context = new JalviewDataset(), dp);
277 } catch (Exception e)
279 System.out.println("Test failed for these arguments.\n");
280 e.printStackTrace(System.out);
285 for (Object o : newdm)
287 System.out.println("Will need to create an " + o.getClass());
290 // now test uniquify/deuniquify stuff
291 // uniquify alignment and write alignment, annotation, features, and trees
293 // import with deuniquify info, and compare results to input.
298 if (context.getLastAlignmentSet().isModified())
301 "Initial alignment set was modified and any associated views should be updated.");