Add anno2vcf command.
[lightning.git] / hgvs / diff.go
index 5a6b625e6b7b6d9e640feb3ef00e2c0ca83d888d..835fcc2b97c704b0ff7d4d1ad205148c77cd28bd 100644 (file)
@@ -1,3 +1,7 @@
+// Copyright (C) The Lightning Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
 package hgvs
 
 import (
@@ -125,7 +129,31 @@ func cleanup(in []diffmatchpatch.Diff) (out []diffmatchpatch.Diff) {
                        in[i+1] = ins
                        in[i+2] = eq
                }
+               // diffmatchpatch solves diff("AXX","XXX") with
+               // [delA,=XX,insX] but we prefer to spell it
+               // [delA,insX,=XX].
+               //
+               // So, when we see a [del,=,ins] sequence that has the
+               // same effect after swapping the "=" and "ins" parts,
+               // we swap them.
+               if i < len(in)-2 &&
+                       d.Type == diffmatchpatch.DiffDelete &&
+                       in[i+1].Type == diffmatchpatch.DiffEqual &&
+                       in[i+2].Type == diffmatchpatch.DiffInsert &&
+                       in[i+1].Text+in[i+2].Text == in[i+2].Text+in[i+1].Text {
+                       in[i+2], in[i+1] = in[i+1], in[i+2]
+               }
                out = append(out, d)
        }
        return
 }
+
+func Less(a, b Variant) bool {
+       if a.Position != b.Position {
+               return a.Position < b.Position
+       } else if a.New != b.New {
+               return a.New < b.New
+       } else {
+               return a.Ref < b.Ref
+       }
+}