Skip to content

Commit

Permalink
add more complete unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdoret committed Oct 7, 2020
1 parent ecc8308 commit 77f09db
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
3 changes: 3 additions & 0 deletions dnaglider/pkg/kmers.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ func (p *KmerProfile) CountsToFreqs() {
func (p *KmerProfile) KmerDist(ref KmerProfile) float64 {
var dist float64
for kmer, freq := range ref.Profile {
// This works because etching missing k-mer
// returns zero value for float

dist += math.Pow(freq-p.Profile[kmer], 2.0)
}
dist = math.Sqrt(dist)
Expand Down
2 changes: 1 addition & 1 deletion dnaglider/pkg/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func SeqEntropy(seq *seq.Seq) float64 {
return -entro
}

// SeqKmer will compute the Kmer profile of the input profile
// SeqKmerDiv will compute the Kmer profile of the input profile
// and compute its distance to a reference k-mer profile.
func SeqKmerDiv(seq *seq.Seq, ref KmerProfile) float64 {
// Get the k-mer length from field name, compute the
Expand Down
20 changes: 20 additions & 0 deletions dnaglider/pkg/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,23 @@ func TestSeqEntropy(t *testing.T) {
}

}

func TestSeqKmerDiv(t *testing.T) {
refSeq, _ := seq.NewSeq(seq.DNA, []byte("AAATAA"))
querySeq, _ := seq.NewSeq(seq.DNA, []byte("AAA"))

refProf := NewKmerProfile(3)
refProf.GetSeqKmers(refSeq)
refProf.CountsToFreqs()
// ref: AAA, AAT, ATA, TAA
// prof: AAA
expDist := math.Sqrt(0.75*0.75 + 3*(0.25*0.25))
obsDist := SeqKmerDiv(querySeq, refProf)
if expDist != obsDist {
t.Errorf(
"Wrong kmer distance with reference: got %f instead of %f",
obsDist,
expDist,
)
}
}
3 changes: 0 additions & 3 deletions dnaglider/pkg/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,6 @@ func ChunkGenome(records <-chan fastx.Record, winSize int, winStride int, chunkS
Seq: rec.Seq.SubSeq(bpStart, bpEnd),
}

if len(chunk.Starts) == 0 {
fmt.Println("empty chunk")
}
chunks <- chunk
bpStart = bpEnd + 1
}
Expand Down
2 changes: 1 addition & 1 deletion dnaglider/pkg/windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestConsumeChunks(t *testing.T) {
chunks := ChunkGenome(records, WINSIZE, WINSTRIDE, CHUNKSIZE)
expHeader := []string{"chrom", "start", "end", "GC", "GCSKEW", "ENTRO", "3MER"}

for res := range ConsumeChunks(chunks, []string{"GC", "GCSKEW", "ENTRO"}, ref) {
for res := range ConsumeChunks(chunks, []string{"GC", "GCSKEW", "ENTRO", "3MER"}, ref) {
for i := range res.Header {
if res.Header[i] != expHeader[i] {
t.Errorf("Result header is incorrect.: %s", res.Header)
Expand Down

0 comments on commit 77f09db

Please sign in to comment.