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.List;
14 public class ParsePackedSet
18 * return results as a series of jalview.datamodel objects suitable for
22 * - context which is updated with new data
25 * @return list of data objects added to context
28 public Object[] getAlignment(JalviewDataset context,
29 Iterable<DataProvider> files) throws Exception
31 List<Object> rslt = new ArrayList<Object>();
34 context = new JalviewDataset();
36 boolean deuniquify = false;
37 for (DataProvider dta : files)
39 Exception exerror = null;
41 FileParse src = dta.getDataSource();
42 if (dta.getType().equals(DataProvider.JvDataType.ALIGNMENT))
47 fmt = new IdentifyFile().Identify(src, false);
48 } catch (Exception ex)
51 errmsg = "Couldn't identify alignment format.";
56 if (!FormatAdapter.isValidIOFormat(fmt, false))
63 // parse the alignment
67 al = new FormatAdapter().readFromFile(src, fmt);
70 errmsg = "Failed to parse alignment from result set";
75 // deuniquify and construct/merge additional dataset entries if
77 context.addAlignment(al);
84 if (dta.getType().equals(JvDataType.ANNOTATION))
86 if (!context.hasAlignments())
88 errmsg = "No alignment or sequence dataset to associate annotation with.";
89 // could duplicate the dataset reference here as default behaviour for
90 // sequence associated annotation ?
95 if (src.getReader() instanceof BufferedReader)
97 br = (BufferedReader) src.getReader();
101 br = new BufferedReader(src.getReader());
103 new jalview.io.AnnotationFile().parseAnnotationFrom(
104 context.getLastAlignment(), br);
106 } catch (Exception e)
108 errmsg = ((errmsg == null) ? "" : errmsg)
109 + "Failed to parse the annotation file associated with the alignment.";
113 if (dta.getType().equals(JvDataType.SEQASSOCATED))
115 if (!context.hasSequenceAssoc())
117 errmsg = "No sequence to associate data with.";
120 errmsg = "parsing of sequence associated data is not implemented";
121 exerror = new Exception(errmsg);
123 if (dta.getType().equals(JvDataType.FEATURES))
127 jalview.io.FeaturesFile ff = new jalview.io.FeaturesFile(src);
128 ff.parse(context.getLastAlignment(), context.featureColours,
130 } catch (Exception e)
132 errmsg = ("Failed to parse the Features file associated with the alignment.");
136 if (dta.getType().equals(JvDataType.TREE))
140 jalview.io.NewickFile nf = new jalview.io.NewickFile(src);
148 // do association to current alignment.
150 context.addTreeFromFile(nf);
153 } catch (Exception e)
155 errmsg = ("Failed to parse the treeFile associated with the result.");
164 context.getLastAlignmentSet().deuniquifyAlignment();
166 return rslt.toArray();
170 * simple command line test. Arguments should be one or more pairs of
171 * <DataProvider.JvDataType> <Filename> arguments. The routine will attempt to
172 * read each source in turn, and report what kind of Jalview datamodel objects
177 public static void main(String args[])
179 // make data providers from the set of keys/files
181 List<DataProvider> dp = new ArrayList<DataProvider>();
182 while ((i + 1) < args.length)
184 String type = args[i++];
185 final String file = args[i++];
186 final JvDataType jtype = DataProvider.JvDataType.valueOf(type
193 fp = new FileParse(file, AppletFormatAdapter.checkProtocol(file));
194 } catch (Exception e)
196 System.err.println("Couldn't handle datasource of type " + jtype
197 + " using URI " + file);
201 dp.add(new SimpleDataProvider(jtype, fp, null));
205 System.out.println("Couldn't parse source type token '"
206 + type.toUpperCase() + "'");
211 System.out.print("** WARNING\nIgnoring unused arguments:\n");
212 while (i < args.length)
214 System.out.print(" " + args[i]);
216 System.out.print("\n");
219 System.out.println("Now trying to parse set:");
220 JalviewDataset context;
225 newdm = (pps = new ParsePackedSet()).getAlignment(
226 context = new JalviewDataset(), dp);
227 } catch (Exception e)
229 System.out.println("Test failed for these arguments.\n");
230 e.printStackTrace(System.out);
235 for (Object o : newdm)
237 System.out.println("Will need to create an " + o.getClass());
240 // now test uniquify/deuniquify stuff
241 // uniquify alignment and write alignment, annotation, features, and trees
243 // import with deuniquify info, and compare results to input.