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