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.FileParse;
27 import jalview.io.FormatAdapter;
28 import jalview.io.IdentifyFile;
29 import jalview.io.packed.DataProvider.JvDataType;
31 import java.io.BufferedReader;
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.List;
37 public class ParsePackedSet
41 * return results as a series of jalview.datamodel objects suitable for
45 * - context which is updated with new data
48 * @return list of data objects added to context
51 public Object[] getAlignment(JalviewDataset context,
52 Iterable<DataProvider> files) throws Exception
54 List<Object> rslt = new ArrayList<Object>();
57 context = new JalviewDataset();
59 boolean deuniquify = false;
60 for (DataProvider dta : files)
62 Exception exerror = null;
64 FileParse src = dta.getDataSource();
65 if (dta.getType().equals(DataProvider.JvDataType.ALIGNMENT))
70 fmt = new IdentifyFile().identify(src, false);
71 } catch (Exception ex)
74 errmsg = "Couldn't identify alignment format.";
79 if (!FormatAdapter.isValidIOFormat(fmt, false))
86 // parse the alignment
90 al = new FormatAdapter().readFromFile(src, fmt);
93 errmsg = "Failed to parse alignment from result set";
98 // deuniquify and construct/merge additional dataset entries if
100 context.addAlignment(al);
101 context.updateSetModified(true);
108 if (dta.getType().equals(JvDataType.ANNOTATION))
110 if (!context.hasAlignments())
112 errmsg = "No alignment or sequence dataset to associate annotation with.";
113 // could duplicate the dataset reference here as default behaviour for
114 // sequence associated annotation ?
119 if (src.getReader() instanceof BufferedReader)
121 br = (BufferedReader) src.getReader();
125 br = new BufferedReader(src.getReader());
127 // TODO: add columnSelection to context
128 if (new jalview.io.AnnotationFile().parseAnnotationFrom(
129 context.getLastAlignment(), null, br))
131 context.updateSetModified(true);
135 errmsg = "Annotation file contained no data.";
138 } catch (Exception e)
140 errmsg = ((errmsg == null) ? "" : errmsg)
141 + "Failed to parse the annotation file associated with the alignment.";
145 if (dta.getType().equals(JvDataType.SEQASSOCATED))
147 if (!context.hasSequenceAssoc())
149 errmsg = "No sequence to associate data with.";
152 errmsg = "parsing of sequence associated data is not implemented";
153 exerror = new Exception(errmsg);
155 if (dta.getType().equals(JvDataType.FEATURES))
157 // check the context has a place to store feature rendering definitions,
158 // if not, create one.
159 if (context.featureColours == null)
161 context.featureColours = new HashMap<String, FeatureColourI>();
165 jalview.io.FeaturesFile ff = new jalview.io.FeaturesFile(src);
166 context.updateSetModified(ff.parse(context.getLastAlignment(),
167 context.featureColours, false, context.relaxedIdMatching));
168 } catch (Exception e)
170 errmsg = ("Failed to parse the Features file associated with the alignment.");
174 if (dta.getType().equals(JvDataType.TREE))
178 jalview.io.NewickFile nf = new jalview.io.NewickFile(src);
186 // do association to current alignment.
188 context.addTreeFromFile(nf);
190 context.updateSetModified(true);
192 } catch (Exception e)
194 errmsg = ("Failed to parse the treeFile associated with the result.");
201 if (errmsg != null && errmsg.length() > 0)
203 throw new IOException(errmsg, exerror);
207 throw new IOException(errmsg, exerror);
212 if (errmsg != null && errmsg.length() > 0)
214 throw new IOException(errmsg);
220 context.getLastAlignmentSet().deuniquifyAlignment();
222 return rslt.toArray();
226 * simple command line test. Arguments should be one or more pairs of
227 * <DataProvider.JvDataType> <Filename> arguments. The routine will attempt to
228 * read each source in turn, and report what kind of Jalview datamodel objects
233 public static void main(String args[])
235 // make data providers from the set of keys/files
237 List<DataProvider> dp = new ArrayList<DataProvider>();
238 while ((i + 1) < args.length)
240 String type = args[i++];
241 final String file = args[i++];
242 final JvDataType jtype = DataProvider.JvDataType.valueOf(type
249 fp = new FileParse(file, AppletFormatAdapter.checkProtocol(file));
250 } catch (Exception e)
252 System.err.println("Couldn't handle datasource of type " + jtype
253 + " using URI " + file);
257 dp.add(new SimpleDataProvider(jtype, fp, null));
261 System.out.println("Couldn't parse source type token '"
262 + type.toUpperCase() + "'");
267 System.out.print("** WARNING\nIgnoring unused arguments:\n");
268 while (i < args.length)
270 System.out.print(" " + args[i]);
272 System.out.print("\n");
275 System.out.println("Now trying to parse set:");
276 JalviewDataset context;
281 newdm = (pps = new ParsePackedSet()).getAlignment(
282 context = new JalviewDataset(), dp);
283 } catch (Exception e)
285 System.out.println("Test failed for these arguments.\n");
286 e.printStackTrace(System.out);
291 for (Object o : newdm)
293 System.out.println("Will need to create an " + o.getClass());
296 // now test uniquify/deuniquify stuff
297 // uniquify alignment and write alignment, annotation, features, and trees
299 // import with deuniquify info, and compare results to input.
304 if (context.getLastAlignmentSet().isModified())
307 .println("Initial alignment set was modified and any associated views should be updated.");