Clover icon

Coverage Report

  1. Project Clover database Wed Dec 3 2025 17:03:17 GMT
  2. Package jalview.io.packed

File ParsePackedSet.java

 

Coverage histogram

../../../img/srcFileCovDistChart0.png
60% of files have more coverage

Code metrics

50
105
2
1
309
241
36
0.34
52.5
2
18

Classes

Class Line # Actions
ParsePackedSet 39 105 36
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

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