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.util.ArrayList;
12 import java.util.Hashtable;
13 import java.util.List;
15 public class ParsePackedSet
19 * return results as a series of jalview.datamodel objects suitable for
23 * - context which is updated with new data
26 * @return list of data objects added to context
29 public Object[] getAlignment(JalviewDataset context,
30 Iterable<DataProvider> files) throws Exception
32 List<Object> rslt = new ArrayList<Object>();
35 context = new JalviewDataset();
37 boolean deuniquify = false;
38 for (DataProvider dta : files)
40 Exception exerror = null;
42 FileParse src = dta.getDataSource();
43 if (dta.getType().equals(DataProvider.JvDataType.ALIGNMENT))
48 fmt = new IdentifyFile().Identify(src, false);
49 } catch (Exception ex)
52 errmsg = "Couldn't identify alignment format.";
57 if (!FormatAdapter.isValidIOFormat(fmt, false))
64 // parse the alignment
68 al = new FormatAdapter().readFromFile(src, fmt);
71 errmsg = "Failed to parse alignment from result set";
76 // deuniquify and construct/merge additional dataset entries if
78 context.addAlignment(al);
79 context.updateSetModified(true);
86 if (dta.getType().equals(JvDataType.ANNOTATION))
88 if (!context.hasAlignments())
90 errmsg = "No alignment or sequence dataset to associate annotation with.";
91 // could duplicate the dataset reference here as default behaviour for
92 // sequence associated annotation ?
97 if (src.getReader() instanceof BufferedReader)
99 br = (BufferedReader) src.getReader();
103 br = new BufferedReader(src.getReader());
105 context.updateSetModified(new jalview.io.AnnotationFile()
106 .parseAnnotationFrom(context.getLastAlignment(), br));
108 } catch (Exception e)
110 errmsg = ((errmsg == null) ? "" : errmsg)
111 + "Failed to parse the annotation file associated with the alignment.";
115 if (dta.getType().equals(JvDataType.SEQASSOCATED))
117 if (!context.hasSequenceAssoc())
119 errmsg = "No sequence to associate data with.";
122 errmsg = "parsing of sequence associated data is not implemented";
123 exerror = new Exception(errmsg);
125 if (dta.getType().equals(JvDataType.FEATURES))
127 // check the context has a place to store feature rendering definitions,
128 // if not, create one.
129 if (context.featureColours == null)
131 context.featureColours = new Hashtable();
135 jalview.io.FeaturesFile ff = new jalview.io.FeaturesFile(src);
136 context.updateSetModified(ff.parse(context.getLastAlignment(),
137 context.featureColours, false, context.relaxedIdMatching));
138 } catch (Exception e)
140 errmsg = ("Failed to parse the Features file associated with the alignment.");
144 if (dta.getType().equals(JvDataType.TREE))
148 jalview.io.NewickFile nf = new jalview.io.NewickFile(src);
156 // do association to current alignment.
158 context.addTreeFromFile(nf);
160 context.updateSetModified(true);
162 } catch (Exception e)
164 errmsg = ("Failed to parse the treeFile associated with the result.");
173 context.getLastAlignmentSet().deuniquifyAlignment();
175 return rslt.toArray();
179 * simple command line test. Arguments should be one or more pairs of
180 * <DataProvider.JvDataType> <Filename> arguments. The routine will attempt to
181 * read each source in turn, and report what kind of Jalview datamodel objects
186 public static void main(String args[])
188 // make data providers from the set of keys/files
190 List<DataProvider> dp = new ArrayList<DataProvider>();
191 while ((i + 1) < args.length)
193 String type = args[i++];
194 final String file = args[i++];
195 final JvDataType jtype = DataProvider.JvDataType.valueOf(type
202 fp = new FileParse(file, AppletFormatAdapter.checkProtocol(file));
203 } catch (Exception e)
205 System.err.println("Couldn't handle datasource of type " + jtype
206 + " using URI " + file);
210 dp.add(new SimpleDataProvider(jtype, fp, null));
214 System.out.println("Couldn't parse source type token '"
215 + type.toUpperCase() + "'");
220 System.out.print("** WARNING\nIgnoring unused arguments:\n");
221 while (i < args.length)
223 System.out.print(" " + args[i]);
225 System.out.print("\n");
228 System.out.println("Now trying to parse set:");
229 JalviewDataset context;
234 newdm = (pps = new ParsePackedSet()).getAlignment(
235 context = new JalviewDataset(), dp);
236 } catch (Exception e)
238 System.out.println("Test failed for these arguments.\n");
239 e.printStackTrace(System.out);
244 for (Object o : newdm)
246 System.out.println("Will need to create an " + o.getClass());
249 // now test uniquify/deuniquify stuff
250 // uniquify alignment and write alignment, annotation, features, and trees
252 // import with deuniquify info, and compare results to input.
255 if (context.getLastAlignmentSet().isModified())
257 System.err.println("Initial alignment set was modified and any associated views should be updated.");