| 1 |
// =========================================================================== |
| 2 |
// |
| 3 |
// PUBLIC DOMAIN NOTICE |
| 4 |
// National Center for Biotechnology Information (NCBI) |
| 5 |
// |
| 6 |
// This software/database is a "United States Government Work" under the |
| 7 |
// terms of the United States Copyright Act. It was written as part of |
| 8 |
// the author's official duties as a United States Government employee and |
| 9 |
// thus cannot be copyrighted. This software/database is freely available |
| 10 |
// to the public for use. The National Library of Medicine and the U.S. |
| 11 |
// Government do not place any restriction on its use or reproduction. |
| 12 |
// We would, however, appreciate having the NCBI and the author cited in |
| 13 |
// any work or product based on this material. |
| 14 |
// |
| 15 |
// Although all reasonable efforts have been taken to ensure the accuracy |
| 16 |
// and reliability of the software and data, the NLM and the U.S. |
| 17 |
// Government do not and cannot warrant the performance or results that |
| 18 |
// may be obtained by using this software or data. The NLM and the U.S. |
| 19 |
// Government disclaim all warranties, express or implied, including |
| 20 |
// warranties of performance, merchantability or fitness for any particular |
| 21 |
// purpose. |
| 22 |
// |
| 23 |
// =========================================================================== |
| 24 |
// |
| 25 |
// File Name: spdi.go |
| 26 |
// |
| 27 |
// Author: Jonathan Kans |
| 28 |
// |
| 29 |
// ========================================================================== |
| 30 |
|
| 31 |
package eutils |
| 32 |
|
| 33 |
import ( |
| 34 |
"os" |
| 35 |
"strconv" |
| 36 |
"strings" |
| 37 |
) |
| 38 |
|
| 39 |
// SequenceRemove removes characters from the beginning, the end, or both |
| 40 |
func SequenceRemove(seq, first, last string) string { |
| 41 |
|
| 42 |
if seq == "" { |
| 43 |
return "" |
| 44 |
} |
| 45 |
|
| 46 |
ln := len(seq) |
| 47 |
|
| 48 |
if IsAllDigits(first) { |
| 49 |
val, err := strconv.Atoi(first) |
| 50 |
if err == nil && val > 0 { |
| 51 |
if val <= ln { |
| 52 |
seq = seq[val:] |
| 53 |
ln = len(seq) |
| 54 |
} else { |
| 55 |
DisplayError("-first argument %d is greater than sequence length %d", val, ln) |
| 56 |
seq = "" |
| 57 |
} |
| 58 |
} |
| 59 |
} else { |
| 60 |
val := len(first) |
| 61 |
if val > 0 { |
| 62 |
if val <= ln { |
| 63 |
// warn if existing sequence does not match deletion argument |
| 64 |
ext := seq[:val] |
| 65 |
if strings.ToUpper(first) != strings.ToUpper(ext) { |
| 66 |
DisplayWarning("-first argument %s does not match existing sequence %s", strings.ToUpper(first), strings.ToUpper(ext)) |
| 67 |
} |
| 68 |
// delete characters |
| 69 |
seq = seq[val:] |
| 70 |
ln = len(seq) |
| 71 |
} else { |
| 72 |
DisplayError("-first argument %d is greater than sequence length %d", val, ln) |
| 73 |
seq = "" |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
if IsAllDigits(last) { |
| 79 |
val, err := strconv.Atoi(last) |
| 80 |
if err == nil && val > 0 { |
| 81 |
if val <= ln { |
| 82 |
seq = seq[:ln-val] |
| 83 |
} else { |
| 84 |
DisplayError("-last argument %d is greater than remaining sequence length %d", val, ln) |
| 85 |
seq = "" |
| 86 |
} |
| 87 |
} |
| 88 |
} else { |
| 89 |
val := len(last) |
| 90 |
if val > 0 { |
| 91 |
if val <= ln { |
| 92 |
// warn if existing sequence does not match deletion argument |
| 93 |
ext := seq[ln-val:] |
| 94 |
if strings.ToUpper(last) != strings.ToUpper(ext) { |
| 95 |
DisplayWarning("-last argument %s does not match existing sequence %s", strings.ToUpper(last), strings.ToUpper(ext)) |
| 96 |
} |
| 97 |
// delete characters |
| 98 |
seq = seq[:ln-val] |
| 99 |
ln = len(seq) |
| 100 |
} else { |
| 101 |
DisplayError("-last argument %d is greater than sequence length %d", val, ln) |
| 102 |
seq = "" |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
return seq |
| 108 |
} |
| 109 |
|
| 110 |
// SequenceRetain keeps leading or trailing sequence characters |
| 111 |
func SequenceRetain(seq string, lead, trail int) string { |
| 112 |
|
| 113 |
if seq == "" { |
| 114 |
return "" |
| 115 |
} |
| 116 |
|
| 117 |
ln := len(seq) |
| 118 |
|
| 119 |
if lead > 0 && trail > 0 { |
| 120 |
DisplayError("Cannot have both -leading and -trailing arguments") |
| 121 |
seq = "" |
| 122 |
} else if lead > 0 { |
| 123 |
if lead <= ln { |
| 124 |
seq = seq[:lead] |
| 125 |
} else { |
| 126 |
DisplayError("-leading argument %d is greater than sequence length %d", lead, ln) |
| 127 |
seq = "" |
| 128 |
} |
| 129 |
} else if trail > 0 { |
| 130 |
if trail <= ln { |
| 131 |
seq = seq[ln-trail:] |
| 132 |
} else { |
| 133 |
DisplayError("-trailing argument %d is greater than sequence length %d", trail, ln) |
| 134 |
seq = "" |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
return seq |
| 139 |
} |
| 140 |
|
| 141 |
// SequenceReplace applies SPDI instructions |
| 142 |
func SequenceReplace(seq string, pos int, del, ins string) string { |
| 143 |
|
| 144 |
if seq == "" { |
| 145 |
return "" |
| 146 |
} |
| 147 |
|
| 148 |
ln := len(seq) |
| 149 |
|
| 150 |
if del == "" && ins == "" { |
| 151 |
DisplayError("-replace command requires either -delete or -insert") |
| 152 |
return "" |
| 153 |
} |
| 154 |
|
| 155 |
if pos > ln { |
| 156 |
|
| 157 |
if pos == ln+1 && del == "" && ins != "" { |
| 158 |
|
| 159 |
// append to end of sequence |
| 160 |
seq = seq[:] + ins |
| 161 |
|
| 162 |
} else { |
| 163 |
DisplayError("-replace position %d is greater than sequence length %d", pos, ln) |
| 164 |
return "" |
| 165 |
} |
| 166 |
|
| 167 |
} else { |
| 168 |
|
| 169 |
if IsAllDigits(del) { |
| 170 |
val, err := strconv.Atoi(del) |
| 171 |
if err == nil && val > 0 { |
| 172 |
if val <= ln-pos { |
| 173 |
seq = seq[:pos] + seq[pos+val:] |
| 174 |
} else { |
| 175 |
DisplayError("-replace deletion %d is greater than remaining sequence length %d", val, ln-pos) |
| 176 |
return "" |
| 177 |
} |
| 178 |
} |
| 179 |
} else { |
| 180 |
val := len(del) |
| 181 |
if val > 0 { |
| 182 |
if val <= ln-pos { |
| 183 |
// warn if existing sequence does not match deletion argument |
| 184 |
ext := seq[pos : pos+val] |
| 185 |
if strings.ToUpper(del) != strings.ToUpper(ext) { |
| 186 |
DisplayWarning("-replace deletion %s does not match existing sequence %s", strings.ToUpper(del), strings.ToUpper(ext)) |
| 187 |
} |
| 188 |
// delete characters |
| 189 |
seq = seq[:pos] + seq[pos+val:] |
| 190 |
} else { |
| 191 |
DisplayError("-replace deletion %d is greater than remaining sequence length %d", val, ln-pos) |
| 192 |
return "" |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
ln = len(seq) |
| 198 |
if ins != "" { |
| 199 |
if pos <= ln { |
| 200 |
seq = seq[:pos] + ins + seq[pos:] |
| 201 |
} else { |
| 202 |
DisplayError("-replace position %d is greater than remaining sequence length %d", pos, ln-pos) |
| 203 |
return "" |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
return seq |
| 209 |
} |
| 210 |
|
| 211 |
// SequenceExtract returns the sequence under the intervals of a feature location |
| 212 |
func SequenceExtract(seq, featLoc string, isOneBased bool) string { |
| 213 |
|
| 214 |
if seq == "" { |
| 215 |
return "" |
| 216 |
} |
| 217 |
|
| 218 |
ln := len(seq) |
| 219 |
|
| 220 |
// split intervals, e.g., "201..224,1550..1920,1986..2085,2317..2404,2466..2629" |
| 221 |
comma := strings.Split(featLoc, ",") |
| 222 |
|
| 223 |
var buffer strings.Builder |
| 224 |
|
| 225 |
for _, item := range comma { |
| 226 |
|
| 227 |
// also allow dash separator, e.g., "201-224,1550-1920" |
| 228 |
item = strings.Replace(item, "-", "..", -1) |
| 229 |
// and colon separator, e.g., "201:224,1550:1920" |
| 230 |
item = strings.Replace(item, ":", "..", -1) |
| 231 |
|
| 232 |
fr, to := SplitInTwoLeft(item, "..") |
| 233 |
|
| 234 |
fr = strings.TrimSpace(fr) |
| 235 |
to = strings.TrimSpace(to) |
| 236 |
|
| 237 |
min, err := strconv.Atoi(fr) |
| 238 |
if err != nil { |
| 239 |
DisplayError("Unrecognized number '%s'", fr) |
| 240 |
os.Exit(1) |
| 241 |
} |
| 242 |
if min < 1 || min > ln { |
| 243 |
DisplayError("Starting point '%s' out of range", fr) |
| 244 |
os.Exit(1) |
| 245 |
} |
| 246 |
|
| 247 |
max, err := strconv.Atoi(to) |
| 248 |
if err != nil { |
| 249 |
DisplayError("Unrecognized number '%s'", to) |
| 250 |
os.Exit(1) |
| 251 |
} |
| 252 |
if max < 1 || max > ln { |
| 253 |
DisplayError("Ending point '%s' out of range", to) |
| 254 |
os.Exit(1) |
| 255 |
} |
| 256 |
|
| 257 |
if !isOneBased { |
| 258 |
min++ |
| 259 |
max++ |
| 260 |
} |
| 261 |
|
| 262 |
if min < max { |
| 263 |
min-- |
| 264 |
sub := seq[min:max] |
| 265 |
buffer.WriteString(sub) |
| 266 |
} else if min > max { |
| 267 |
max-- |
| 268 |
sub := seq[max:min] |
| 269 |
sub = ReverseComplement(sub) |
| 270 |
buffer.WriteString(sub) |
| 271 |
} else { |
| 272 |
// need more information to know strand if single point |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
return buffer.String() |
| 277 |
} |
| 278 |
|
| 279 |
// ReverseComplement returns the reverse complement of a sequence |
| 280 |
func ReverseComplement(seq string) string { |
| 281 |
|
| 282 |
runes := []rune(seq) |
| 283 |
// reverse sequence letters - middle base in odd-length sequence is not touched |
| 284 |
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 { |
| 285 |
runes[i], runes[j] = runes[j], runes[i] |
| 286 |
} |
| 287 |
found := false |
| 288 |
// now complement every base, also handling uracil, leaving case intact |
| 289 |
for i, ch := range runes { |
| 290 |
runes[i], found = revComp[ch] |
| 291 |
if !found { |
| 292 |
runes[i] = 'X' |
| 293 |
} |
| 294 |
} |
| 295 |
seq = string(runes) |
| 296 |
|
| 297 |
return seq |
| 298 |
} |
| 299 |
|
| 300 |
// SequenceReverse reverses a sequence, but does not complement the bases |
| 301 |
func SequenceReverse(seq string) string { |
| 302 |
|
| 303 |
runes := []rune(seq) |
| 304 |
// reverse sequence letters - middle base in odd-length sequence is not touched |
| 305 |
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 { |
| 306 |
runes[i], runes[j] = runes[j], runes[i] |
| 307 |
} |
| 308 |
seq = string(runes) |
| 309 |
|
| 310 |
return seq |
| 311 |
} |