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 java.util.Locale;
25 import jalview.api.FeatureColourI;
26 import jalview.datamodel.AlignmentI;
27 import jalview.io.AppletFormatAdapter;
28 import jalview.io.FileFormatI;
29 import jalview.io.FileParse;
30 import jalview.io.FormatAdapter;
31 import jalview.io.IdentifyFile;
32 import jalview.io.packed.DataProvider.JvDataType;
34 import java.io.BufferedReader;
35 import java.io.IOException;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.List;
40 public class ParsePackedSet
44 * return results as a series of jalview.datamodel objects suitable for
48 * - context which is updated with new data
51 * @return list of data objects added to context
54 public Object[] getAlignment(JalviewDataset context,
55 Iterable<DataProvider> files) throws Exception
57 List<Object> rslt = new ArrayList<>();
60 context = new JalviewDataset();
62 boolean deuniquify = false;
63 for (DataProvider dta : files)
65 Exception exerror = null;
67 FileParse src = dta.getDataSource();
68 if (dta.getType().equals(DataProvider.JvDataType.ALIGNMENT))
70 FileFormatI fmt = null;
73 fmt = new IdentifyFile().identify(src, false);
74 } catch (Exception ex)
77 errmsg = "Couldn't identify alignment format.";
82 // parse the alignment
86 al = new FormatAdapter().readFromFile(src, fmt);
89 errmsg = "Failed to parse alignment from result set";
94 // deuniquify and construct/merge additional dataset entries if
96 context.addAlignment(al);
97 context.updateSetModified(true);
103 if (dta.getType().equals(JvDataType.ANNOTATION))
105 if (!context.hasAlignments())
107 errmsg = "No alignment or sequence dataset to associate annotation with.";
108 // could duplicate the dataset reference here as default behaviour for
109 // sequence associated annotation ?
114 if (src.getReader() instanceof BufferedReader)
116 br = (BufferedReader) src.getReader();
120 br = new BufferedReader(src.getReader());
122 // TODO: add columnSelection to context
123 if (new jalview.io.AnnotationFile().parseAnnotationFrom(
124 context.getLastAlignment(), null, br))
126 context.updateSetModified(true);
130 errmsg = "Annotation file contained no data.";
133 } catch (Exception e)
135 errmsg = ((errmsg == null) ? "" : errmsg)
136 + "Failed to parse the annotation file associated with the alignment.";
140 if (dta.getType().equals(JvDataType.SEQASSOCATED))
142 if (!context.hasSequenceAssoc())
144 errmsg = "No sequence to associate data with.";
147 errmsg = "parsing of sequence associated data is not implemented";
148 exerror = new Exception(errmsg);
150 if (dta.getType().equals(JvDataType.FEATURES))
152 // check the context has a place to store feature rendering definitions,
153 // if not, create one.
154 if (context.featureColours == null)
156 context.featureColours = new HashMap<>();
160 jalview.io.FeaturesFile ff = new jalview.io.FeaturesFile(src);
161 context.updateSetModified(ff.parse(context.getLastAlignment(),
162 context.featureColours, false,
163 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
230 public static void main(String args[])
232 // make data providers from the set of keys/files
234 List<DataProvider> dp = new ArrayList<>();
235 while ((i + 1) < args.length)
237 String type = args[i++];
238 final String file = args[i++];
239 final JvDataType jtype = DataProvider.JvDataType
240 .valueOf(type.toUpperCase(Locale.ROOT));
246 fp = new FileParse(file, AppletFormatAdapter.checkProtocol(file));
247 } catch (Exception e)
250 .errPrintln("Couldn't handle datasource of type " + jtype
251 + " using URI " + file);
255 dp.add(new SimpleDataProvider(jtype, fp, null));
259 jalview.bin.Console.outPrintln("Couldn't parse source type token '"
260 + type.toUpperCase(Locale.ROOT) + "'");
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 jalview.bin.Console.outPrintln("Now trying to parse set:");
274 JalviewDataset context;
279 newdm = (pps = new ParsePackedSet())
280 .getAlignment(context = new JalviewDataset(), dp);
281 } catch (Exception e)
283 jalview.bin.Console.outPrintln("Test failed for these arguments.\n");
284 e.printStackTrace(System.out);
289 for (Object o : newdm)
292 .outPrintln("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())
305 jalview.bin.Console.errPrintln(
306 "Initial alignment set was modified and any associated views should be updated.");