JAL-1807 test
[jalviewjs.git] / bin / jalview / util / MappingUtils.js
1 Clazz.declarePackage ("jalview.util");
2 Clazz.load (null, "jalview.util.MappingUtils", ["jalview.analysis.AlignmentSorter", "jalview.commands.EditCommand", "$.OrderCommand", "jalview.datamodel.AlignmentOrder", "$.ColumnSelection", "$.SearchResults", "$.Sequence", "$.SequenceGroup", "jalview.util.Comparison", "$.StringUtils", "java.util.ArrayList", "$.Collections", "$.HashMap"], function () {
3 c$ = Clazz.declareType (jalview.util, "MappingUtils");
4 c$.mapCutOrPaste = Clazz.defineMethod (c$, "mapCutOrPaste", 
5 function (edit, undo, targetSeqs, result, mappings) {
6 var action = edit.getAction ();
7 if (undo) {
8 action = action.getUndoAction ();
9 }System.err.println ("MappingUtils.mapCutOrPaste not yet implemented");
10 }, "jalview.commands.EditCommand.Edit,~B,java.util.List,jalview.commands.EditCommand,java.util.Set");
11 c$.mapEditCommand = Clazz.defineMethod (c$, "mapEditCommand", 
12 function (command, undo, mapTo, gapChar, mappings) {
13 if (!mapTo.isNucleotide ()) {
14 return null;
15 }var targetCopies =  new java.util.HashMap ();
16 for (var seq, $seq = mapTo.getSequences ().iterator (); $seq.hasNext () && ((seq = $seq.next ()) || true);) {
17 var ds = seq.getDatasetSequence ();
18 if (ds != null) {
19 var copy =  new jalview.datamodel.Sequence (seq);
20 copy.setDatasetSequence (ds);
21 targetCopies.put (ds, copy);
22 }}
23 var originalSequences = command.priorState (undo);
24 var result =  new jalview.commands.EditCommand ();
25 var edits = command.getEditIterator (!undo);
26 while (edits.hasNext ()) {
27 var edit = edits.next ();
28 if (edit.getAction () === jalview.commands.EditCommand.Action.CUT || edit.getAction () === jalview.commands.EditCommand.Action.PASTE) {
29 jalview.util.MappingUtils.mapCutOrPaste (edit, undo, mapTo.getSequences (), result, mappings);
30 } else if (edit.getAction () === jalview.commands.EditCommand.Action.INSERT_GAP || edit.getAction () === jalview.commands.EditCommand.Action.DELETE_GAP) {
31 jalview.util.MappingUtils.mapInsertOrDelete (edit, undo, originalSequences, mapTo.getSequences (), targetCopies, gapChar, result, mappings);
32 }}
33 return result.getSize () > 0 ? result : null;
34 }, "jalview.commands.EditCommand,~B,jalview.datamodel.AlignmentI,~S,java.util.Set");
35 c$.mapInsertOrDelete = Clazz.defineMethod (c$, "mapInsertOrDelete", 
36 function (edit, undo, originalSequences, targetSeqs, targetCopies, gapChar, result, mappings) {
37 var action = edit.getAction ();
38 if (undo) {
39 action = action.getUndoAction ();
40 }var count = edit.getNumber ();
41 var editPos = edit.getPosition ();
42 for (var seq, $seq = 0, $$seq = edit.getSequences (); $seq < $$seq.length && ((seq = $$seq[$seq]) || true); $seq++) {
43 var ds = seq.getDatasetSequence ();
44 if (ds == null) {
45 continue;
46 }var actedOn = originalSequences.get (ds);
47 var seqpos = actedOn.findPosition (editPos);
48 var sr = jalview.util.MappingUtils.buildSearchResults (seq, seqpos, mappings);
49 if (!sr.isEmpty ()) {
50 for (var targetSeq, $targetSeq = targetSeqs.iterator (); $targetSeq.hasNext () && ((targetSeq = $targetSeq.next ()) || true);) {
51 ds = targetSeq.getDatasetSequence ();
52 if (ds == null) {
53 continue;
54 }var copyTarget = targetCopies.get (ds);
55 var match = sr.getResults (copyTarget, 0, copyTarget.getLength ());
56 if (match != null) {
57 var ratio = 3;
58 var mappedCount = count * 3;
59 var mappedEditPos = action === jalview.commands.EditCommand.Action.DELETE_GAP ? match[0] - mappedCount : match[0];
60 var e = resultClazz.innerTypeInstance (jalview.commands.EditCommand.Edit, this, null, action,  Clazz.newArray (-1, [targetSeq]), mappedEditPos, mappedCount, gapChar);
61 result.addEdit (e);
62 if (action === jalview.commands.EditCommand.Action.INSERT_GAP) {
63 copyTarget.setSequence ( String.instantialize (jalview.util.StringUtils.insertCharAt (copyTarget.getSequence (), mappedEditPos, mappedCount, gapChar)));
64 } else if (action === jalview.commands.EditCommand.Action.DELETE_GAP) {
65 copyTarget.setSequence ( String.instantialize (jalview.util.StringUtils.deleteChars (copyTarget.getSequence (), mappedEditPos, mappedEditPos + mappedCount)));
66 }}}
67 }if (action === jalview.commands.EditCommand.Action.INSERT_GAP) {
68 actedOn.setSequence ( String.instantialize (jalview.util.StringUtils.insertCharAt (actedOn.getSequence (), editPos, count, gapChar)));
69 } else if (action === jalview.commands.EditCommand.Action.DELETE_GAP) {
70 actedOn.setSequence ( String.instantialize (jalview.util.StringUtils.deleteChars (actedOn.getSequence (), editPos, editPos + count)));
71 }}
72 }, "jalview.commands.EditCommand.Edit,~B,java.util.Map,java.util.List,java.util.Map,~S,jalview.commands.EditCommand,java.util.Set");
73 c$.buildSearchResults = Clazz.defineMethod (c$, "buildSearchResults", 
74 function (seq, index, seqmappings) {
75 var results =  new jalview.datamodel.SearchResults ();
76 jalview.util.MappingUtils.addSearchResults (results, seq, index, seqmappings);
77 return results;
78 }, "jalview.datamodel.SequenceI,~N,java.util.Set");
79 c$.addSearchResults = Clazz.defineMethod (c$, "addSearchResults", 
80 function (results, seq, index, seqmappings) {
81 if (index >= seq.getStart () && index <= seq.getEnd ()) {
82 for (var acf, $acf = seqmappings.iterator (); $acf.hasNext () && ((acf = $acf.next ()) || true);) {
83 acf.markMappedRegion (seq, index, results);
84 }
85 }}, "jalview.datamodel.SearchResults,jalview.datamodel.SequenceI,~N,java.util.Set");
86 c$.mapSequenceGroup = Clazz.defineMethod (c$, "mapSequenceGroup", 
87 function (sg, mapFrom, mapTo) {
88 var targetIsNucleotide = mapTo.isNucleotide ();
89 var protein = targetIsNucleotide ? mapFrom : mapTo;
90 var codonFrames = protein.getAlignment ().getCodonFrames ();
91 var mappedGroup =  new jalview.datamodel.SequenceGroup (sg);
92 mappedGroup.cs = mapTo.getGlobalColourScheme ();
93 mappedGroup.clear ();
94 var minStartCol = -1;
95 var maxEndCol = -1;
96 var selectionStartRes = sg.getStartRes ();
97 var selectionEndRes = sg.getEndRes ();
98 for (var selected, $selected = sg.getSequences ().iterator (); $selected.hasNext () && ((selected = $selected.next ()) || true);) {
99 var firstUngappedPos = selectionStartRes;
100 while (firstUngappedPos <= selectionEndRes && jalview.util.Comparison.isGap (selected.getCharAt (firstUngappedPos))) {
101 firstUngappedPos++;
102 }
103 if (firstUngappedPos > selectionEndRes) {
104 continue;
105 }var lastUngappedPos = selectionEndRes;
106 while (lastUngappedPos >= selectionStartRes && jalview.util.Comparison.isGap (selected.getCharAt (lastUngappedPos))) {
107 lastUngappedPos--;
108 }
109 var startResiduePos = selected.findPosition (firstUngappedPos);
110 var endResiduePos = selected.findPosition (lastUngappedPos);
111 for (var acf, $acf = codonFrames.iterator (); $acf.hasNext () && ((acf = $acf.next ()) || true);) {
112 var mappedSequence = targetIsNucleotide ? acf.getDnaForAaSeq (selected) : acf.getAaForDnaSeq (selected);
113 if (mappedSequence != null) {
114 for (var seq, $seq = mapTo.getAlignment ().getSequences ().iterator (); $seq.hasNext () && ((seq = $seq.next ()) || true);) {
115 var mappedStartResidue = 0;
116 var mappedEndResidue = 0;
117 if (seq.getDatasetSequence () === mappedSequence) {
118 var sr = jalview.util.MappingUtils.buildSearchResults (selected, startResiduePos, java.util.Collections.singleton (acf));
119 for (var m, $m = sr.getResults ().iterator (); $m.hasNext () && ((m = $m.next ()) || true);) {
120 mappedStartResidue = m.getStart ();
121 mappedEndResidue = m.getEnd ();
122 }
123 sr = jalview.util.MappingUtils.buildSearchResults (selected, endResiduePos, java.util.Collections.singleton (acf));
124 for (var m, $m = sr.getResults ().iterator (); $m.hasNext () && ((m = $m.next ()) || true);) {
125 mappedStartResidue = Math.min (mappedStartResidue, m.getStart ());
126 mappedEndResidue = Math.max (mappedEndResidue, m.getEnd ());
127 }
128 var mappedStartCol = seq.findIndex (mappedStartResidue) - 1;
129 minStartCol = minStartCol == -1 ? mappedStartCol : Math.min (minStartCol, mappedStartCol);
130 var mappedEndCol = seq.findIndex (mappedEndResidue) - 1;
131 maxEndCol = maxEndCol == -1 ? mappedEndCol : Math.max (maxEndCol, mappedEndCol);
132 mappedGroup.addSequence (seq, false);
133 break;
134 }}
135 }}
136 }
137 mappedGroup.setStartRes (minStartCol < 0 ? 0 : minStartCol);
138 mappedGroup.setEndRes (maxEndCol < 0 ? 0 : maxEndCol);
139 return mappedGroup;
140 }, "jalview.datamodel.SequenceGroup,jalview.api.AlignViewportI,jalview.api.AlignViewportI");
141 c$.mapOrderCommand = Clazz.defineMethod (c$, "mapOrderCommand", 
142 function (command, undo, mapTo, mappings) {
143 var sortOrder = command.getSequenceOrder (undo);
144 var mappedOrder =  new java.util.ArrayList ();
145 var j = 0;
146 var mappingToNucleotide = mapTo.isNucleotide ();
147 for (var seq, $seq = 0, $$seq = sortOrder; $seq < $$seq.length && ((seq = $$seq[$seq]) || true); $seq++) {
148 for (var acf, $acf = mappings.iterator (); $acf.hasNext () && ((acf = $acf.next ()) || true);) {
149 var mappedSeq = mappingToNucleotide ? acf.getDnaForAaSeq (seq) : acf.getAaForDnaSeq (seq);
150 if (mappedSeq != null) {
151 for (var seq2, $seq2 = mapTo.getSequences ().iterator (); $seq2.hasNext () && ((seq2 = $seq2.next ()) || true);) {
152 if (seq2.getDatasetSequence () === mappedSeq) {
153 mappedOrder.add (seq2);
154 j++;
155 break;
156 }}
157 }}
158 }
159 if (j == 0) {
160 return null;
161 }if (j < mapTo.getHeight ()) {
162 for (var seq, $seq = mapTo.getSequences ().iterator (); $seq.hasNext () && ((seq = $seq.next ()) || true);) {
163 if (!mappedOrder.contains (seq)) {
164 mappedOrder.add (seq);
165 }}
166 }var mappedOrderArray = mappedOrder.toArray ( new Array (mappedOrder.size ()));
167 var oldOrder = mapTo.getSequencesArray ();
168 jalview.analysis.AlignmentSorter.sortBy (mapTo,  new jalview.datamodel.AlignmentOrder (mappedOrderArray));
169 var result =  new jalview.commands.OrderCommand (command.getDescription (), oldOrder, mapTo);
170 return result;
171 }, "jalview.commands.OrderCommand,~B,jalview.datamodel.AlignmentI,java.util.Set");
172 c$.mapColumnSelection = Clazz.defineMethod (c$, "mapColumnSelection", 
173 function (colsel, mapFrom, mapTo) {
174 var targetIsNucleotide = mapTo.isNucleotide ();
175 var protein = targetIsNucleotide ? mapFrom : mapTo;
176 var codonFrames = protein.getAlignment ().getCodonFrames ();
177 var mappedColumns =  new jalview.datamodel.ColumnSelection ();
178 if (colsel == null) {
179 return mappedColumns;
180 }var fromGapChar = mapFrom.getAlignment ().getGapCharacter ();
181 for (var obj, $obj = colsel.getSelected ().iterator (); $obj.hasNext () && ((obj = $obj.next ()) || true);) {
182 var col = (obj).intValue ();
183 var mappedToMin = 2147483647;
184 var mappedToMax = -2147483648;
185 for (var fromSeq, $fromSeq = mapFrom.getAlignment ().getSequences ().iterator (); $fromSeq.hasNext () && ((fromSeq = $fromSeq.next ()) || true);) {
186 if (fromSeq.getCharAt (col) == fromGapChar) {
187 continue;
188 }var residuePos = fromSeq.findPosition (col);
189 var sr = jalview.util.MappingUtils.buildSearchResults (fromSeq, residuePos, codonFrames);
190 for (var m, $m = sr.getResults ().iterator (); $m.hasNext () && ((m = $m.next ()) || true);) {
191 var mappedStartResidue = m.getStart ();
192 var mappedEndResidue = m.getEnd ();
193 var mappedSeq = m.getSequence ();
194 for (var toSeq, $toSeq = mapTo.getAlignment ().getSequences ().iterator (); $toSeq.hasNext () && ((toSeq = $toSeq.next ()) || true);) {
195 if (toSeq.getDatasetSequence () === mappedSeq) {
196 var mappedStartCol = toSeq.findIndex (mappedStartResidue);
197 var mappedEndCol = toSeq.findIndex (mappedEndResidue);
198 mappedToMin = Math.min (mappedToMin, mappedStartCol);
199 mappedToMax = Math.max (mappedToMax, mappedEndCol);
200 break;
201 }}
202 }
203 }
204 for (var i = mappedToMin; i <= mappedToMax; i++) {
205 mappedColumns.addElement (i - 1);
206 }
207 }
208 return mappedColumns;
209 }, "jalview.datamodel.ColumnSelection,jalview.api.AlignViewportI,jalview.api.AlignViewportI");
210 c$.findCodonFor = Clazz.defineMethod (c$, "findCodonFor", 
211 function (seq, col, mappings) {
212 var dsPos = seq.findPosition (col);
213 for (var mapping, $mapping = mappings.iterator (); $mapping.hasNext () && ((mapping = $mapping.next ()) || true);) {
214 if (mapping.involvesSequence (seq)) {
215 return mapping.getMappedCodon (seq.getDatasetSequence (), dsPos);
216 }}
217 return null;
218 }, "jalview.datamodel.SequenceI,~N,java.util.Set");
219 c$.flattenRanges = Clazz.defineMethod (c$, "flattenRanges", 
220 function (ranges) {
221 var count = 0;
222 for (var i = 0; i < ranges.length - 1; i += 2) {
223 count += ranges[i + 1] - ranges[i] + 1;
224 }
225 var result =  Clazz.newIntArray (count, 0);
226 var k = 0;
227 for (var i = 0; i < ranges.length - 1; i += 2) {
228 for (var j = ranges[i]; j <= ranges[i + 1]; j++) {
229 result[k++] = j;
230 }
231 }
232 return result;
233 }, "~A");
234 c$.findMappingsForSequence = Clazz.defineMethod (c$, "findMappingsForSequence", 
235 function (sequence, mappings) {
236 var result =  new java.util.ArrayList ();
237 if (sequence == null || mappings == null) {
238 return result;
239 }for (var mapping, $mapping = mappings.iterator (); $mapping.hasNext () && ((mapping = $mapping.next ()) || true);) {
240 if (mapping.involvesSequence (sequence)) {
241 result.add (mapping);
242 }}
243 return result;
244 }, "jalview.datamodel.SequenceI,java.util.Set");
245 });