1 package jalview.io.packed;
3 import jalview.datamodel.AlignmentI;
4 import jalview.io.AppletFormatAdapter;
5 import jalview.io.FileParse;
6 import jalview.io.FormatAdapter;
7 import jalview.io.IdentifyFile;
8 import jalview.io.packed.DataProvider.JvDataType;
10 import java.io.BufferedReader;
11 import java.io.IOException;
12 import java.util.ArrayList;
13 import java.util.Hashtable;
14 import java.util.List;
16 public class ParsePackedSet
20 * return results as a series of jalview.datamodel objects suitable for
24 * - context which is updated with new data
27 * @return list of data objects added to context
30 public Object[] getAlignment(JalviewDataset context,
31 Iterable<DataProvider> files) throws Exception
33 List<Object> rslt = new ArrayList<Object>();
36 context = new JalviewDataset();
38 boolean deuniquify = false;
39 for (DataProvider dta : files)
41 Exception exerror = null;
43 FileParse src = dta.getDataSource();
44 if (dta.getType().equals(DataProvider.JvDataType.ALIGNMENT))
49 fmt = new IdentifyFile().Identify(src, false);
50 } catch (Exception ex)
53 errmsg = "Couldn't identify alignment format.";
58 if (!FormatAdapter.isValidIOFormat(fmt, false))
65 // parse the alignment
69 al = new FormatAdapter().readFromFile(src, fmt);
72 errmsg = "Failed to parse alignment from result set";
77 // deuniquify and construct/merge additional dataset entries if
79 context.addAlignment(al);
80 context.updateSetModified(true);
87 if (dta.getType().equals(JvDataType.ANNOTATION))
89 if (!context.hasAlignments())
91 errmsg = "No alignment or sequence dataset to associate annotation with.";
92 // could duplicate the dataset reference here as default behaviour for
93 // sequence associated annotation ?
98 if (src.getReader() instanceof BufferedReader)
100 br = (BufferedReader) src.getReader();
104 br = new BufferedReader(src.getReader());
106 if (new jalview.io.AnnotationFile()
107 .parseAnnotationFrom(context.getLastAlignment(), br))
109 context.updateSetModified(true);
111 errmsg = "Annotation file contained no data.";
114 } catch (Exception e)
116 errmsg = ((errmsg == null) ? "" : errmsg)
117 + "Failed to parse the annotation file associated with the alignment.";
121 if (dta.getType().equals(JvDataType.SEQASSOCATED))
123 if (!context.hasSequenceAssoc())
125 errmsg = "No sequence to associate data with.";
128 errmsg = "parsing of sequence associated data is not implemented";
129 exerror = new Exception(errmsg);
131 if (dta.getType().equals(JvDataType.FEATURES))
133 // check the context has a place to store feature rendering definitions,
134 // if not, create one.
135 if (context.featureColours == null)
137 context.featureColours = new Hashtable();
141 jalview.io.FeaturesFile ff = new jalview.io.FeaturesFile(src);
142 context.updateSetModified(ff.parse(context.getLastAlignment(),
143 context.featureColours, false, context.relaxedIdMatching));
144 } catch (Exception e)
146 errmsg = ("Failed to parse the Features file associated with the alignment.");
150 if (dta.getType().equals(JvDataType.TREE))
154 jalview.io.NewickFile nf = new jalview.io.NewickFile(src);
162 // do association to current alignment.
164 context.addTreeFromFile(nf);
166 context.updateSetModified(true);
168 } catch (Exception e)
170 errmsg = ("Failed to parse the treeFile associated with the result.");
177 if (errmsg!=null && errmsg.length()>0)
179 throw new IOException(errmsg,exerror);
181 throw new IOException(errmsg,exerror);
184 if (errmsg!=null && errmsg.length()>0)
186 throw new IOException(errmsg);
192 context.getLastAlignmentSet().deuniquifyAlignment();
194 return rslt.toArray();
198 * simple command line test. Arguments should be one or more pairs of
199 * <DataProvider.JvDataType> <Filename> arguments. The routine will attempt to
200 * read each source in turn, and report what kind of Jalview datamodel objects
205 public static void main(String args[])
207 // make data providers from the set of keys/files
209 List<DataProvider> dp = new ArrayList<DataProvider>();
210 while ((i + 1) < args.length)
212 String type = args[i++];
213 final String file = args[i++];
214 final JvDataType jtype = DataProvider.JvDataType.valueOf(type
221 fp = new FileParse(file, AppletFormatAdapter.checkProtocol(file));
222 } catch (Exception e)
224 System.err.println("Couldn't handle datasource of type " + jtype
225 + " using URI " + file);
229 dp.add(new SimpleDataProvider(jtype, fp, null));
233 System.out.println("Couldn't parse source type token '"
234 + type.toUpperCase() + "'");
239 System.out.print("** WARNING\nIgnoring unused arguments:\n");
240 while (i < args.length)
242 System.out.print(" " + args[i]);
244 System.out.print("\n");
247 System.out.println("Now trying to parse set:");
248 JalviewDataset context;
253 newdm = (pps = new ParsePackedSet()).getAlignment(
254 context = new JalviewDataset(), dp);
255 } catch (Exception e)
257 System.out.println("Test failed for these arguments.\n");
258 e.printStackTrace(System.out);
263 for (Object o : newdm)
265 System.out.println("Will need to create an " + o.getClass());
268 // now test uniquify/deuniquify stuff
269 // uniquify alignment and write alignment, annotation, features, and trees
271 // import with deuniquify info, and compare results to input.
274 if (context.getLastAlignmentSet().isModified())
276 System.err.println("Initial alignment set was modified and any associated views should be updated.");