@@ -24,6 +24,7 @@ import (
2424 "bytes"
2525 "fmt"
2626 "io"
27+ "path/filepath"
2728 "regexp"
2829 "runtime"
2930 "strings"
@@ -61,6 +62,9 @@ more on: http://bioinf.shenwei.me/seqkit/usage/#replace
6162Special replacement symbols (only for replacing name not sequence):
6263
6364 {nr} Record number, starting from 1
65+ {fn} File name
66+ {fbn} File base name
67+ {fbne} File base name without any extension
6468 {kv} Corresponding value of the key (captured variable $n) by key-value file,
6569 n can be specified by flag -I (--key-capt-idx) (default: 1)
6670
@@ -137,6 +141,16 @@ Filtering records to edit:
137141 replaceWithFN = true
138142 }
139143
144+ var replaceWithFBN bool
145+ if reFBN .Match (replacement ) {
146+ replaceWithFBN = true
147+ }
148+
149+ var replaceWithFBNE bool
150+ if reFN .Match (replacement ) {
151+ replaceWithFBNE = true
152+ }
153+
140154 var replaceWithKV bool
141155 var kvs map [string ]string
142156 if reKV .Match (replacement ) {
@@ -333,10 +347,23 @@ Filtering records to edit:
333347 var re * regexp.Regexp
334348 var h uint64
335349
350+ var fileBase string
351+
336352 for _ , file := range files {
337353 fastxReader , err := fastx .NewReader (alphabet , file , idRegexp )
338354 checkError (err )
355+
339356 nr := 0
357+ bFile := []byte (file )
358+ fileBase = filepath .Base (file )
359+ bFileBase := []byte (fileBase )
360+ var bFileBaseWithoutExtension []byte
361+ if i := strings .Index (fileBase , "." ); i >= 0 {
362+ bFileBaseWithoutExtension = []byte (fileBase [:i ])
363+ } else {
364+ bFileBaseWithoutExtension = []byte (fileBase )
365+ }
366+
340367 for {
341368 record , err = fastxReader .Read ()
342369 if err != nil {
@@ -435,7 +462,13 @@ Filtering records to edit:
435462 }
436463
437464 if replaceWithFN {
438- r = reFN .ReplaceAll (r , []byte (file ))
465+ r = reFN .ReplaceAll (r , bFile )
466+ }
467+ if replaceWithFBN {
468+ r = reFBN .ReplaceAll (r , bFileBase )
469+ }
470+ if replaceWithFBNE {
471+ r = reFBNE .ReplaceAll (r , bFileBaseWithoutExtension )
439472 }
440473
441474 if replaceWithKV {
@@ -518,3 +551,5 @@ func init() {
518551var reNR = regexp .MustCompile (`\{(NR|nr)\}` )
519552var reKV = regexp .MustCompile (`\{(KV|kv)\}` )
520553var reFN = regexp .MustCompile (`\{(FN|fn)\}` )
554+ var reFBN = regexp .MustCompile (`\{(FBN|fbn)\}` )
555+ var reFBNE = regexp .MustCompile (`\{(FBNE|fbne)\}` )
0 commit comments