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);
85 if (dta.getType().equals(JvDataType.ANNOTATION))
87 if (!context.hasAlignments())
89 errmsg = "No alignment or sequence dataset to associate annotation with.";
90 // could duplicate the dataset reference here as default behaviour for
91 // sequence associated annotation ?
96 if (src.getReader() instanceof BufferedReader)
98 br = (BufferedReader) src.getReader();
102 br = new BufferedReader(src.getReader());
104 new jalview.io.AnnotationFile().parseAnnotationFrom(
105 context.getLastAlignment(), br);
107 } catch (Exception e)
109 errmsg = ((errmsg == null) ? "" : errmsg)
110 + "Failed to parse the annotation file associated with the alignment.";
114 if (dta.getType().equals(JvDataType.SEQASSOCATED))
116 if (!context.hasSequenceAssoc())
118 errmsg = "No sequence to associate data with.";
121 errmsg = "parsing of sequence associated data is not implemented";
122 exerror = new Exception(errmsg);
124 if (dta.getType().equals(JvDataType.FEATURES))
126 // check the context has a place to store feature rendering definitions, if not, create one.
127 if (context.featureColours==null)
129 context.featureColours = new Hashtable();
133 jalview.io.FeaturesFile ff = new jalview.io.FeaturesFile(src);
134 ff.parse(context.getLastAlignment(), context.featureColours,
136 } catch (Exception e)
138 errmsg = ("Failed to parse the Features file associated with the alignment.");
142 if (dta.getType().equals(JvDataType.TREE))
146 jalview.io.NewickFile nf = new jalview.io.NewickFile(src);
154 // do association to current alignment.
156 context.addTreeFromFile(nf);
159 } catch (Exception e)
161 errmsg = ("Failed to parse the treeFile associated with the result.");
170 context.getLastAlignmentSet().deuniquifyAlignment();
172 return rslt.toArray();
176 * simple command line test. Arguments should be one or more pairs of
177 * <DataProvider.JvDataType> <Filename> arguments. The routine will attempt to
178 * read each source in turn, and report what kind of Jalview datamodel objects
183 public static void main(String args[])
185 // make data providers from the set of keys/files
187 List<DataProvider> dp = new ArrayList<DataProvider>();
188 while ((i + 1) < args.length)
190 String type = args[i++];
191 final String file = args[i++];
192 final JvDataType jtype = DataProvider.JvDataType.valueOf(type
199 fp = new FileParse(file, AppletFormatAdapter.checkProtocol(file));
200 } catch (Exception e)
202 System.err.println("Couldn't handle datasource of type " + jtype
203 + " using URI " + file);
207 dp.add(new SimpleDataProvider(jtype, fp, null));
211 System.out.println("Couldn't parse source type token '"
212 + type.toUpperCase() + "'");
217 System.out.print("** WARNING\nIgnoring unused arguments:\n");
218 while (i < args.length)
220 System.out.print(" " + args[i]);
222 System.out.print("\n");
225 System.out.println("Now trying to parse set:");
226 JalviewDataset context;
231 newdm = (pps = new ParsePackedSet()).getAlignment(
232 context = new JalviewDataset(), dp);
233 } catch (Exception e)
235 System.out.println("Test failed for these arguments.\n");
236 e.printStackTrace(System.out);
241 for (Object o : newdm)
243 System.out.println("Will need to create an " + o.getClass());
246 // now test uniquify/deuniquify stuff
247 // uniquify alignment and write alignment, annotation, features, and trees
249 // import with deuniquify info, and compare results to input.