Skip to content

Commit 644c045

Browse files
Added option --log to goalign mutate gaps
1 parent f21b6cf commit 644c045

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

align/align.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import (
1919
// Alignment represents a set of aligned sequences (multiple Sequence Alignment)
2020
type Alignment interface {
2121
SeqBag
22-
AddGaps(rate, lenprop float64, rand *mathrand.Rand)
22+
// Add random gaps to random sequences, and output the indices of the affected sequences
23+
AddGaps(rate, lenprop float64, rand *mathrand.Rand) []int
2324
AddAmbiguities(rate, lenprop float64, rand *mathrand.Rand)
2425
Append(Alignment) error // Appends alignment sequences to this alignment
2526
AvgAllelesPerSite() float64
@@ -996,7 +997,8 @@ func (a *align) Recombine(prop float64, lenprop float64, swap bool, rand *mathra
996997
// Add prop*100% gaps to lenprop*100% of the sequences
997998
// if prop < 0 || lenprop<0 : does nothing
998999
// if prop > 1 || lenprop>1 : does nothing
999-
func (a *align) AddGaps(lenprop float64, prop float64, rand *mathrand.Rand) {
1000+
// Returns the indices of the affected sequences
1001+
func (a *align) AddGaps(lenprop float64, prop float64, rand *mathrand.Rand) (affected []int) {
10001002
if prop < 0 || prop > 1 {
10011003
return
10021004
}
@@ -1012,10 +1014,12 @@ func (a *align) AddGaps(lenprop float64, prop float64, rand *mathrand.Rand) {
10121014
for i := 0; i < nb; i++ {
10131015
permsites := rand.Perm(a.Length())
10141016
seq := a.seqs[permseqs[i]]
1017+
affected = append(affected, permseqs[i])
10151018
for j := 0; j < nbgaps; j++ {
10161019
seq.sequence[permsites[j]] = GAP
10171020
}
10181021
}
1022+
return
10191023
}
10201024

10211025
// Add prop*100% ambiguities to lenprop*100% of the sequences

cmd/addgaps.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package cmd
22

33
import (
4+
"fmt"
5+
46
"github.com/evolbioinfo/goalign/align"
57
"github.com/evolbioinfo/goalign/io"
68
"github.com/evolbioinfo/goalign/io/utils"
79
"github.com/spf13/cobra"
810
)
911

1012
var gapnbseqs float64
13+
var addgapslogfile string
1114

1215
// rogueCmd represents the rogue command
1316
var addgapsCmd = &cobra.Command{
@@ -21,6 +24,7 @@ goalign mutate gaps -i align.fa -n 0.5 -r 0.5
2124
RunE: func(cmd *cobra.Command, args []string) (err error) {
2225
var aligns *align.AlignChannel
2326
var f utils.StringWriterCloser
27+
var addgapslog utils.StringWriterCloser
2428

2529
if aligns, err = readalign(infile); err != nil {
2630
io.LogError(err)
@@ -32,9 +36,25 @@ goalign mutate gaps -i align.fa -n 0.5 -r 0.5
3236
}
3337
defer utils.CloseWriteFile(f, mutateOutput)
3438

39+
if addgapslog, err = utils.OpenWriteFile(addgapslogfile); err != nil {
40+
io.LogError(err)
41+
return
42+
}
43+
defer utils.CloseWriteFile(addgapslog, addgapslogfile)
44+
3545
for al := range aligns.Achan {
36-
al.AddGaps(mutateRate, gapnbseqs, globalRand)
46+
affected := al.AddGaps(mutateRate, gapnbseqs, globalRand)
3747
writeAlign(al, f)
48+
for _, s := range affected {
49+
if n, ok := al.GetSequenceNameById(s); !ok {
50+
err = fmt.Errorf("affected sequence not found in the alignment: %d", s)
51+
io.LogError(err)
52+
return
53+
} else {
54+
fmt.Fprintf(addgapslog, "%d\t%s\n", s, n)
55+
}
56+
}
57+
3858
}
3959

4060
if aligns.Err != nil {
@@ -48,4 +68,5 @@ goalign mutate gaps -i align.fa -n 0.5 -r 0.5
4868
func init() {
4969
mutateCmd.AddCommand(addgapsCmd)
5070
addgapsCmd.PersistentFlags().Float64VarP(&gapnbseqs, "prop-seq", "n", 0.5, "Proportion of the sequences in which to add gaps")
71+
addgapsCmd.PersistentFlags().StringVar(&addgapslogfile, "log", "none", "Log file with the names of affected sequences")
5172
}

0 commit comments

Comments
 (0)