12fa7c77c4a821999af3de6421f1a9d1c491d8ef
[lightning.git] / hgvs / diff.go
1 // Copyright (C) The Lightning Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package hgvs
6
7 import (
8         "fmt"
9         "strings"
10         "time"
11
12         "github.com/sergi/go-diff/diffmatchpatch"
13 )
14
15 type Variant struct {
16         Position int
17         Ref      string
18         New      string
19         Left     string // base preceding an indel, if Ref or New is empty
20 }
21
22 func (v *Variant) String() string {
23         switch {
24         case len(v.New) == 0 && len(v.Ref) == 0:
25                 return fmt.Sprintf("%d=", v.Position)
26         case len(v.New) == 1 && v.New == v.Ref:
27                 return fmt.Sprintf("%d=", v.Position)
28         case v.New == v.Ref:
29                 return fmt.Sprintf("%d_%d=", v.Position, v.Position+len(v.Ref)-1)
30         case len(v.New) == 0 && len(v.Ref) == 1:
31                 return fmt.Sprintf("%ddel", v.Position)
32         case len(v.New) == 0:
33                 return fmt.Sprintf("%d_%ddel", v.Position, v.Position+len(v.Ref)-1)
34         case len(v.Ref) == 1 && len(v.New) == 1:
35                 return fmt.Sprintf("%d%s>%s", v.Position, v.Ref, v.New)
36         case len(v.Ref) == 0:
37                 return fmt.Sprintf("%d_%dins%s", v.Position-1, v.Position, v.New)
38         case len(v.Ref) == 1 && len(v.New) > 0:
39                 return fmt.Sprintf("%ddelins%s", v.Position, v.New)
40         default:
41                 return fmt.Sprintf("%d_%ddelins%s", v.Position, v.Position+len(v.Ref)-1, v.New)
42         }
43 }
44
45 // PadLeft returns a Variant that is equivalent to v but (if possible)
46 // uses the stashed preceding base (the Left field) to avoid having a
47 // non-empty Ref or New part, even for an insertion or deletion.
48 //
49 // For example, if v is {Position: 45, Ref: "", New: "A"}, PadLeft
50 // might return {Position: 44, Ref: "T", New: "TA"}.
51 func (v *Variant) PadLeft() Variant {
52         if len(v.Ref) == 0 || len(v.New) == 0 {
53                 return Variant{
54                         Position: v.Position - len(v.Left),
55                         Ref:      v.Left + v.Ref,
56                         New:      v.Left + v.New,
57                 }
58         } else {
59                 return *v
60         }
61 }
62
63 func Diff(a, b string, timeout time.Duration) ([]Variant, bool) {
64         dmp := diffmatchpatch.New()
65         var deadline time.Time
66         if timeout > 0 {
67                 deadline = time.Now().Add(timeout)
68         }
69         diffs := dmp.DiffBisect(a, b, deadline)
70         timedOut := false
71         if timeout > 0 && time.Now().After(deadline) {
72                 timedOut = true
73         }
74         diffs = cleanup(dmp.DiffCleanupEfficiency(diffs))
75         pos := 1
76         var variants []Variant
77         for i := 0; i < len(diffs); {
78                 left := "" // last char before an insertion or deletion
79                 for ; i < len(diffs) && diffs[i].Type == diffmatchpatch.DiffEqual; i++ {
80                         pos += len(diffs[i].Text)
81                         if tlen := len(diffs[i].Text); tlen > 0 {
82                                 left = diffs[i].Text[tlen-1:]
83                         }
84                 }
85                 if i >= len(diffs) {
86                         break
87                 }
88                 v := Variant{Position: pos, Left: left}
89                 for ; i < len(diffs) && diffs[i].Type != diffmatchpatch.DiffEqual; i++ {
90                         if diffs[i].Type == diffmatchpatch.DiffDelete {
91                                 v.Ref += diffs[i].Text
92                         } else {
93                                 v.New += diffs[i].Text
94                         }
95                 }
96                 pos += len(v.Ref)
97                 variants = append(variants, v)
98                 left = ""
99         }
100         return variants, timedOut
101 }
102
103 func cleanup(in []diffmatchpatch.Diff) (out []diffmatchpatch.Diff) {
104         out = make([]diffmatchpatch.Diff, 0, len(in))
105         for i := 0; i < len(in); i++ {
106                 d := in[i]
107                 // Merge consecutive entries of same type (e.g.,
108                 // "insert A; insert B")
109                 for i < len(in)-1 && in[i].Type == in[i+1].Type {
110                         d.Text += in[i+1].Text
111                         i++
112                 }
113                 out = append(out, d)
114         }
115         in, out = out, make([]diffmatchpatch.Diff, 0, len(in))
116         for i := 0; i < len(in); i++ {
117                 d := in[i]
118                 // diffmatchpatch solves diff("AAX","XTX") with
119                 // [delAA,=X,insTX] but we prefer to spell it
120                 // [delAA,insXT,=X].
121                 //
122                 // So, when we see a [del,=,ins] sequence where the
123                 // "=" part is a suffix of the "ins" part -- e.g.,
124                 // [delAAA,=CGG,insTTTCGG] -- we rearrange it to the
125                 // equivalent spelling [delAAA,insCGGTTT,=CGG].
126                 if i < len(in)-2 &&
127                         d.Type == diffmatchpatch.DiffDelete &&
128                         in[i+1].Type == diffmatchpatch.DiffEqual &&
129                         in[i+2].Type == diffmatchpatch.DiffInsert &&
130                         strings.HasSuffix(in[i+2].Text, in[i+1].Text) {
131                         eq, ins := in[i+1], in[i+2]
132                         ins.Text = eq.Text + ins.Text[:len(ins.Text)-len(eq.Text)]
133                         in[i+1] = ins
134                         in[i+2] = eq
135                 }
136                 // diffmatchpatch solves diff("AXX","XXX") with
137                 // [delA,=XX,insX] but we prefer to spell it
138                 // [delA,insX,=XX].
139                 //
140                 // So, when we see a [del,=,ins] sequence that has the
141                 // same effect after swapping the "=" and "ins" parts,
142                 // we swap them.
143                 if i < len(in)-2 &&
144                         d.Type == diffmatchpatch.DiffDelete &&
145                         in[i+1].Type == diffmatchpatch.DiffEqual &&
146                         in[i+2].Type == diffmatchpatch.DiffInsert &&
147                         in[i+1].Text+in[i+2].Text == in[i+2].Text+in[i+1].Text {
148                         in[i+2], in[i+1] = in[i+1], in[i+2]
149                 }
150                 // when diffmatchpatch says [delAAA, insXAY] and
151                 // len(X)==1, we prefer to treat the A>X as a snp.
152                 if i < len(in)-1 &&
153                         d.Type == diffmatchpatch.DiffDelete &&
154                         in[i+1].Type == diffmatchpatch.DiffInsert &&
155                         len(d.Text) >= 2 &&
156                         len(in[i+1].Text) >= 2 &&
157                         d.Text[1] == in[i+1].Text[1] {
158                         eqend := 2
159                         for ; eqend < len(d.Text) && eqend < len(in[i+1].Text) && d.Text[eqend] == in[i+1].Text[eqend]; eqend++ {
160                         }
161                         out = append(out,
162                                 diffmatchpatch.Diff{diffmatchpatch.DiffDelete, d.Text[:1]},
163                                 diffmatchpatch.Diff{diffmatchpatch.DiffInsert, in[i+1].Text[:1]},
164                                 diffmatchpatch.Diff{diffmatchpatch.DiffEqual, d.Text[1:eqend]})
165                         in[i].Text, in[i+1].Text = in[i].Text[eqend:], in[i+1].Text[eqend:]
166                         i--
167                         continue
168                 }
169                 // when diffmatchpatch says [delAAA, insXaY] and
170                 // len(Y)==1, we prefer to treat the A>Y as a snp.
171                 if i < len(in)-1 &&
172                         d.Type == diffmatchpatch.DiffDelete &&
173                         in[i+1].Type == diffmatchpatch.DiffInsert &&
174                         len(d.Text) >= 2 &&
175                         len(in[i+1].Text) >= 2 &&
176                         d.Text[len(d.Text)-2] == in[i+1].Text[len(in[i+1].Text)-2] {
177                         // eqstart will be the number of equal chars
178                         // before the terminal snp, plus 1 for the snp
179                         // itself. Example, for [delAAAA, insTTAAG],
180                         // eqstart will be 3.
181                         eqstart := 2
182                         for ; eqstart < len(d.Text) && eqstart < len(in[i+1].Text) && d.Text[len(d.Text)-eqstart] == in[i+1].Text[len(in[i+1].Text)-eqstart]; eqstart++ {
183                         }
184                         eqstart--
185                         out = append(out,
186                                 diffmatchpatch.Diff{diffmatchpatch.DiffDelete, d.Text[:len(d.Text)-eqstart]},
187                                 diffmatchpatch.Diff{diffmatchpatch.DiffInsert, in[i+1].Text[:len(in[i+1].Text)-eqstart]},
188                                 diffmatchpatch.Diff{diffmatchpatch.DiffEqual, d.Text[len(d.Text)-eqstart : len(d.Text)-1]},
189                                 diffmatchpatch.Diff{diffmatchpatch.DiffDelete, d.Text[len(d.Text)-1:]},
190                                 diffmatchpatch.Diff{diffmatchpatch.DiffInsert, in[i+1].Text[len(in[i+1].Text)-1:]})
191                         i++
192                         continue
193                 }
194                 out = append(out, d)
195         }
196         return
197 }
198
199 func Less(a, b Variant) bool {
200         if a.Position != b.Position {
201                 return a.Position < b.Position
202         } else if a.New != b.New {
203                 return a.New < b.New
204         } else {
205                 return a.Ref < b.Ref
206         }
207 }