Skip to content

Commit

Permalink
Add method to stats.Map to output JSON - close #16
Browse files Browse the repository at this point in the history
  • Loading branch information
emi80 committed Jun 19, 2018
1 parent cfdb2d2 commit 1939832
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
5 changes: 4 additions & 1 deletion cmd/bamstats/bamstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

log "github.com/Sirupsen/logrus"
"github.com/guigolab/bamstats"
"github.com/guigolab/bamstats/utils"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -33,7 +34,9 @@ func run(cmd *cobra.Command, args []string) (err error) {
if err != nil {
return
}
bamstats.WriteOutput(output, allStats)

w := utils.NewWriter(output)
allStats.OutputJSON(w)

return
}
Expand Down
6 changes: 0 additions & 6 deletions process.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/guigolab/bamstats/config"
"github.com/guigolab/bamstats/sam"
"github.com/guigolab/bamstats/stats"
"github.com/guigolab/bamstats/utils"
)

func init() {
Expand Down Expand Up @@ -134,11 +133,6 @@ func Process(bamFile string, anno string, cpu int, maxBuf int, reads int, uniq b
return allStats, nil
}

func WriteOutput(output string, st stats.Map) {
out := utils.NewOutput(output)
utils.OutputJSON(out, st)
}

func makeStatsMap(index *annotation.RtreeMap, cfg *config.Config) stats.Map {
m := make(stats.Map)
m.Add("general", stats.NewGeneralStats())
Expand Down
15 changes: 15 additions & 0 deletions stats/stats.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package stats

import (
"bufio"
"encoding/json"
"fmt"
"io"
"strconv"

"github.com/guigolab/bamstats/sam"
Expand Down Expand Up @@ -49,6 +51,19 @@ func (sm Map) Add(key string, s Stats) {
sm[key] = s
}

// OutputJSON writes sm to the wrtier as JSON
func (sm Map) OutputJSON(writer io.Writer) error {
b, err := json.MarshalIndent(sm, "", "\t")
if err != nil {
return err
}
writer.Write(b)
if w, ok := writer.(*bufio.Writer); ok {
w.Flush()
}
return nil
}

// NewMap creates and instance of a stats.Map
// func NewMap(general, coverage, uniq bool) Map {
// m := make(Map)
Expand Down
18 changes: 5 additions & 13 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,16 @@ func Min(a, b int) int {
return b
}

// OutputJSON writes the json representation of stats to an io.Writer
func OutputJSON(writer io.Writer, stats interface{}) {
b, err := json.MarshalIndent(stats, "", "\t")
Check(err)
writer.Write(b)
if w, ok := writer.(*bufio.Writer); ok {
w.Flush()
}
}

// NewOutput return a new io.Writer given an output file name. If the file name is '-' os.Stdout is returned.
func NewOutput(output string) io.Writer {
// NewWriter return a new io.Writer given an output file name. If the file name is '-' os.Stdout is returned.
func NewWriter(output string) io.Writer {
switch output {
case "-":
return os.Stdout
default:
f, err := os.Create(output)
Check(err)
if err != nil {
return nil
}
return bufio.NewWriter(f)
}
}

0 comments on commit 1939832

Please sign in to comment.