JAL-3446 unused imports removed
[jalview.git] / src / jalview / io / packed / ParsePackedSet.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.io.packed;
22
23 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28
29 import jalview.datamodel.AlignmentI;
30 import jalview.io.AppletFormatAdapter;
31 import jalview.io.FileFormatI;
32 import jalview.io.FileParse;
33 import jalview.io.FormatAdapter;
34 import jalview.io.IdentifyFile;
35 import jalview.io.packed.DataProvider.JvDataType;
36
37 public class ParsePackedSet
38 {
39
40   /**
41    * return results as a series of jalview.datamodel objects suitable for
42    * display
43    * 
44    * @param context
45    *          - context which is updated with new data
46    * @param files
47    *          - source data
48    * @return list of data objects added to context
49    * @throws Exception
50    */
51   public Object[] getAlignment(JalviewDataset context,
52           Iterable<DataProvider> files) throws Exception
53   {
54     List<Object> rslt = new ArrayList<>();
55     if (context == null)
56     {
57       context = new JalviewDataset();
58     }
59     boolean deuniquify = false;
60     for (DataProvider dta : files)
61     {
62       Exception exerror = null;
63       String errmsg = null;
64       FileParse src = dta.getDataSource();
65       if (dta.getType().equals(DataProvider.JvDataType.ALIGNMENT))
66       {
67         FileFormatI fmt = null;
68         try
69         {
70           fmt = new IdentifyFile().identify(src, false);
71         } catch (Exception ex)
72         {
73           exerror = ex;
74           errmsg = "Couldn't identify alignment format.";
75         }
76
77         if (fmt != null)
78         {
79           // parse the alignment
80           AlignmentI al = null;
81           try
82           {
83             al = new FormatAdapter().readFromFile(src, fmt);
84           } catch (Exception e)
85           {
86             errmsg = "Failed to parse alignment from result set";
87             exerror = e;
88           }
89           if (al != null)
90           {
91             // deuniquify and construct/merge additional dataset entries if
92             // necessary.
93             context.addAlignment(al);
94             context.updateSetModified(true);
95             rslt.add(al);
96             deuniquify = true;
97           }
98         }
99       }
100       if (dta.getType().equals(JvDataType.ANNOTATION))
101       {
102         if (!context.hasAlignments())
103         {
104           errmsg = "No alignment or sequence dataset to associate annotation with.";
105           // could duplicate the dataset reference here as default behaviour for
106           // sequence associated annotation ?
107         }
108         try
109         {
110           BufferedReader br;
111           if (src.getReader() instanceof BufferedReader)
112           {
113             br = (BufferedReader) src.getReader();
114           }
115           else
116           {
117             br = new BufferedReader(src.getReader());
118           }
119           // TODO: add columnSelection to context
120           if (new jalview.io.AnnotationFile().parseAnnotationFrom(
121                   context.getLastAlignment(), null, br))
122           {
123             context.updateSetModified(true);
124           }
125           else
126           {
127             errmsg = "Annotation file contained no data.";
128           }
129
130         } catch (Exception e)
131         {
132           errmsg = ((errmsg == null) ? "" : errmsg)
133                   + "Failed to parse the annotation file associated with the alignment.";
134           exerror = e;
135         }
136       }
137       if (dta.getType().equals(JvDataType.SEQASSOCATED))
138       {
139         if (!context.hasSequenceAssoc())
140         {
141           errmsg = "No sequence to associate data with.";
142
143         }
144         errmsg = "parsing of sequence associated data is not implemented";
145         exerror = new Exception(errmsg);
146       }
147       if (dta.getType().equals(JvDataType.FEATURES))
148       {
149         // check the context has a place to store feature rendering definitions,
150         // if not, create one.
151         if (context.featureColours == null)
152         {
153           context.featureColours = new HashMap<>();
154         }
155         try
156         {
157           jalview.io.FeaturesFile ff = new jalview.io.FeaturesFile(src);
158           context.updateSetModified(ff.parse(context.getLastAlignment(),
159                   context.featureColours, false,
160                   context.relaxedIdMatching));
161         } catch (Exception e)
162         {
163           errmsg = ("Failed to parse the Features file associated with the alignment.");
164           exerror = e;
165         }
166       }
167       if (dta.getType().equals(JvDataType.TREE))
168       {
169         try
170         {
171           jalview.io.NewickFile nf = new jalview.io.NewickFile(src);
172           if (!nf.isValid())
173           {
174             nf.close();
175             nf = null;
176           }
177           else
178           {
179             // do association to current alignment.
180
181             context.addTreeFromFile(nf);
182             rslt.add(nf);
183             context.updateSetModified(true);
184           }
185         } catch (Exception e)
186         {
187           errmsg = ("Failed to parse the treeFile associated with the result.");
188           exerror = e;
189         }
190
191       }
192       if (exerror != null)
193       {
194         if (errmsg != null && errmsg.length() > 0)
195         {
196           throw new IOException(errmsg, exerror);
197         }
198         else
199         {
200           throw new IOException(errmsg, exerror);
201         }
202       }
203       else
204       {
205         if (errmsg != null && errmsg.length() > 0)
206         {
207           throw new IOException(errmsg);
208         }
209       }
210     }
211     if (deuniquify)
212     {
213       context.getLastAlignmentSet().deuniquifyAlignment();
214     }
215     return rslt.toArray();
216   }
217
218   /**
219    * simple command line test. Arguments should be one or more pairs of
220    * <DataProvider.JvDataType> <Filename> arguments. The routine will attempt to
221    * read each source in turn, and report what kind of Jalview datamodel objects
222    * would be created.
223    * 
224    * @param args
225    * @j2sIgnore
226    */
227   public static void main(String args[])
228   {
229     // make data providers from the set of keys/files
230     int i = 0;
231     List<DataProvider> dp = new ArrayList<>();
232     while ((i + 1) < args.length)
233     {
234       String type = args[i++];
235       final String file = args[i++];
236       final JvDataType jtype = DataProvider.JvDataType
237               .valueOf(type.toUpperCase());
238       if (jtype != null)
239       {
240         final FileParse fp;
241         try
242         {
243           fp = new FileParse(file, AppletFormatAdapter.checkProtocol(file));
244         } catch (Exception e)
245         {
246           System.err.println("Couldn't handle datasource of type " + jtype
247                   + " using URI " + file);
248           e.printStackTrace();
249           return;
250         }
251         dp.add(new SimpleDataProvider(jtype, fp, null));
252       }
253       else
254       {
255         System.out.println("Couldn't parse source type token '"
256                 + type.toUpperCase() + "'");
257       }
258     }
259     if (i < args.length)
260     {
261       System.out.print("** WARNING\nIgnoring unused arguments:\n");
262       while (i < args.length)
263       {
264         System.out.print(" " + args[i]);
265       }
266       System.out.print("\n");
267
268     }
269     System.out.println("Now trying to parse set:");
270     JalviewDataset context;
271     Object[] newdm;
272     ParsePackedSet pps;
273     try
274     {
275       newdm = (pps = new ParsePackedSet())
276               .getAlignment(context = new JalviewDataset(), dp);
277     } catch (Exception e)
278     {
279       System.out.println("Test failed for these arguments.\n");
280       e.printStackTrace(System.out);
281       return;
282     }
283     if (newdm != null)
284     {
285       for (Object o : newdm)
286       {
287         System.out.println("Will need to create an " + o.getClass());
288       }
289
290       // now test uniquify/deuniquify stuff
291       // uniquify alignment and write alignment, annotation, features, and trees
292       // to buffers.
293       // import with deuniquify info, and compare results to input.
294
295     }
296     else
297     {
298       if (context.getLastAlignmentSet().isModified())
299       {
300         System.err.println(
301                 "Initial alignment set was modified and any associated views should be updated.");
302       }
303     }
304   }
305 }