JAL-1807 explicit imports (jalview.io.*)
[jalview.git] / src / jalview / io / packed / JalviewDataset.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 jalview.analysis.NJTree;
24 import jalview.analysis.SeqsetUtils;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.SequenceI;
27 import jalview.io.NewickFile;
28
29 import java.util.ArrayList;
30 import java.util.Hashtable;
31 import java.util.List;
32
33 public class JalviewDataset
34 {
35   /**
36    * dataset that new data (sequences, alignments) will be added to
37    */
38   AlignmentI parentDataset;
39
40   /**
41    * @return the parentDataset
42    */
43   public AlignmentI getParentDataset()
44   {
45     return parentDataset;
46   }
47
48   /**
49    * @param parentDataset
50    *          the parentDataset to set
51    */
52   public void setParentDataset(AlignmentI parentDataset)
53   {
54     this.parentDataset = parentDataset;
55   }
56
57   /**
58    * @return the featureColours
59    */
60   public Hashtable getFeatureColours()
61   {
62     return featureColours;
63   }
64
65   /**
66    * @param featureColours
67    *          the featureColours to set
68    */
69   public void setFeatureColours(Hashtable featureColours)
70   {
71     this.featureColours = featureColours;
72   }
73
74   /**
75    * @return the seqDetails
76    */
77   public Hashtable getSeqDetails()
78   {
79     return seqDetails;
80   }
81
82   /**
83    * @param seqDetails
84    *          the seqDetails to set
85    */
86   public void setSeqDetails(Hashtable seqDetails)
87   {
88     this.seqDetails = seqDetails;
89   }
90
91   /**
92    * @return the al
93    */
94   public List<AlignmentSet> getAl()
95   {
96     return (al == null) ? new ArrayList<AlignmentSet>() : al;
97   }
98
99   /**
100    * current alignment being worked on.
101    */
102   List<AlignmentSet> al;
103
104   public class AlignmentSet
105   {
106     public AlignmentI al;
107
108     public List<NewickFile> trees;
109
110     AlignmentSet(AlignmentI a)
111     {
112       al = a;
113       trees = new ArrayList<NewickFile>();
114     }
115
116     /**
117      * deuniquify the current alignment in the context, merging any new
118      * annotation/features with the existing set
119      * 
120      * @param context
121      */
122     void deuniquifyAlignment()
123     {
124       if (seqDetails == null || seqDetails.size() == 0)
125       {
126         // nothing to do
127         return;
128       }
129       // 1. recover correct names and attributes for each sequence in alignment.
130       /*
131        * TODO: housekeeping w.r.t. recovery of dataset and annotation references
132        * for input sequences, and then dataset sequence creation for new
133        * sequences retrieved from service // finally, attempt to de-uniquify to
134        * recover input sequence identity, and try to map back onto dataset Note:
135        * this jalview.analysis.SeqsetUtils.deuniquify(SeqNames, alseqs, true);
136        * will NOT WORK - the returned alignment may contain multiple versions of
137        * the input sequence, each being a subsequence of the original.
138        * deuniquify also removes existing annotation and features added in the
139        * previous step... al.setDataset(dataset); // add in new sequences
140        * retrieved from sequence search which are not already in dataset. //
141        * trigger a 'fetchDBids' to annotate sequences with database ids...
142        */
143       // jalview.analysis.SeqsetUtils.deuniquifyAndMerge(parentDataset,
144       // seqDetails, al,true);
145
146       SeqsetUtils.deuniquify(seqDetails, al.getSequencesArray(), true);
147       // 2. Update names of associated nodes in any trees
148       for (NewickFile nf : trees)
149       {
150         // the following works because all trees are already had node/SequenceI
151         // associations created.
152         NJTree njt = new NJTree(al.getSequencesArray(), nf);
153         // this just updates the displayed leaf name on the tree according to
154         // the SequenceIs.
155         njt.renameAssociatedNodes();
156       }
157
158     }
159
160     /**
161      * set modification flag. If anything modifies the alignment in the current
162      * set, this flag should be true
163      */
164     private boolean modified = false;
165
166     /**
167      * @return the modified
168      */
169     public boolean isModified()
170     {
171       return modified;
172     }
173
174     /**
175      * or the modification state with the given state
176      * 
177      * @param modifiedFromAction
178      */
179     public void updateSetModified(boolean modifiedFromAction)
180     {
181       modified |= modifiedFromAction;
182     }
183   }
184
185   /**
186    * current set of feature colours
187    */
188   Hashtable featureColours;
189
190   /**
191    * original identity of each sequence in results
192    */
193   Hashtable seqDetails;
194
195   public boolean relaxedIdMatching = false;
196
197   public JalviewDataset()
198   {
199     seqDetails = new Hashtable();
200     al = new ArrayList<AlignmentSet>();
201     parentDataset = null;
202     featureColours = new Hashtable();
203   }
204
205   /**
206    * context created from an existing alignment.
207    * 
208    * @param parentAlignment
209    */
210   public JalviewDataset(AlignmentI aldataset, Hashtable fc,
211           Hashtable seqDets)
212   {
213     this(aldataset, fc, seqDets, null);
214   }
215
216   /**
217    * 
218    * @param aldataset
219    *          - parent dataset for any new alignment/sequence data (must not be
220    *          null)
221    * @param fc
222    *          (may be null) feature settings for the alignment where new feature
223    *          renderstyles are stored
224    * @param seqDets
225    *          - (may be null) anonymised sequence information created by
226    *          Sequence uniquifier
227    * @param parentAlignment
228    *          (may be null) alignment to associate new annotation and trees
229    *          with.
230    */
231   public JalviewDataset(AlignmentI aldataset, Hashtable fc,
232           Hashtable seqDets, AlignmentI parentAlignment)
233   {
234     this();
235     parentDataset = aldataset;
236     if (parentAlignment != null)
237     {
238       parentDataset = parentAlignment.getDataset();
239       if (parentDataset == null)
240       {
241         parentDataset = parentAlignment;
242       }
243       else
244       {
245         addAlignment(parentAlignment);
246       }
247     }
248     if (seqDets != null)
249     {
250       seqDetails = seqDets;
251     }
252     if (fc != null)
253     {
254       featureColours = fc;
255     }
256
257   }
258
259   public boolean hasAlignments()
260   {
261     return al != null && al.size() > 0;
262   }
263
264   public AlignmentI getLastAlignment()
265   {
266     return (al == null || al.size() < 1) ? null : al.get(al.size() - 1).al;
267   }
268
269   public AlignmentSet getLastAlignmentSet()
270   {
271     return (al == null || al.size() < 1) ? null : al.get(al.size() - 1);
272   }
273
274   /**
275    * post process (deuniquify) the current alignment and its dependent data, and
276    * then add newal to the dataset.
277    * 
278    * @param newal
279    */
280   public void addAlignment(AlignmentI newal)
281   {
282     if (!hasAlignments())
283     {
284       al = new ArrayList<AlignmentSet>();
285     }
286     AlignmentSet last = getLastAlignmentSet();
287     if (last != null)
288     {
289       System.err.println("Deuniquifying last alignment set.");
290       last.deuniquifyAlignment();
291     }
292     al.add(new AlignmentSet(newal));
293   }
294
295   public void addTreeFromFile(NewickFile nf)
296   {
297     AlignmentSet lal = getLastAlignmentSet();
298     lal.trees.add(nf);
299   }
300
301   public boolean hasSequenceAssoc()
302   {
303     // TODO: discover where sequence associated data should be put.
304     return false;
305   }
306
307   public SequenceI getLastAssociatedSequence()
308   {
309     // TODO: delineate semantics for associating uniquified data with
310     // potentially de-uniquified sequence.
311     return null;
312   }
313
314   /**
315    * update the modified state flag for the current set with the given
316    * modification state
317    * 
318    * @param modified
319    *          - this will be ored with current modification state
320    */
321   public void updateSetModified(boolean modified)
322   {
323     getLastAlignmentSet().updateSetModified(modified);
324   }
325 }