From 1a542972c4098ac24ae13740e2fd645400152bac Mon Sep 17 00:00:00 2001 From: Daniel Schuette Date: Sat, 15 Sep 2018 19:39:01 -0400 Subject: [PATCH] bug fix - last enzyme in *.re file did not get parsed --- parse.go | 13 +++++++++++-- parse_test.go | 30 ++++++++++++++++++------------ tests/parse2.re | 2 +- tests/parse3.re | 1 + 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/parse.go b/parse.go index ed9bb38..960b756 100644 --- a/parse.go +++ b/parse.go @@ -57,7 +57,16 @@ func ParseEnzymesFromFile(file string) (map[string]RestrictEnzyme, error) { Loop: for i, n := 0, len(b); i < n; i++ { - // decide what to do next + // current char is the last char in the document => add parsed results to `enzymesMap' + if (i + 1) == n { + if itemContainer != nil { + if _, ok := enzymesMap[itemContainer.Name]; !ok { + enzymesMap[itemContainer.Name] = *itemContainer + } + } + } + + // otherwise, the document is not yet fully parsed => decide what to do next if i < len(b)-2 { // if current char is a new line delimiter, decide how to proceed if b[i] == '\n' { @@ -126,7 +135,7 @@ Loop: } } - fmt.Printf("parsed %d enzymes from '%s'\n", line, file) + fmt.Printf("parsed %d of %d enzyme(s) from '%s'\n", len(enzymesMap), line, file) return enzymesMap, nil } diff --git a/parse_test.go b/parse_test.go index 52a3748..84d0de1 100644 --- a/parse_test.go +++ b/parse_test.go @@ -2,6 +2,7 @@ package cloningprimer import ( "errors" + "log" "testing" ) @@ -37,10 +38,10 @@ func TestParseEnzymesFromFile(t *testing.T) { want: map[string]RestrictEnzyme{ "AclI": { Name: "AclI", - RecognitionSite: "aslkfhsdf", - NoPalinCleav: "sdlfkj", - ID: "sldkfj", - Isoschizomeres: []string{"sdfklj", "sdlfkj"}, + RecognitionSite: "invalid", + NoPalinCleav: "invalid", + ID: "invalid", + Isoschizomeres: []string{"invalid", "invalid"}, }, }, err: nil, @@ -51,10 +52,10 @@ func TestParseEnzymesFromFile(t *testing.T) { want: map[string]RestrictEnzyme{ "AclI": { Name: "AclI", - RecognitionSite: "aslkfhsdf", - NoPalinCleav: "sdlfkj", - ID: "sdfk", - Isoschizomeres: []string{"sdlkfj", "sdflkj"}, + RecognitionSite: "invalid", + NoPalinCleav: "invalid", + ID: "invalid", + Isoschizomeres: []string{"invalid", "invalid"}, }, }, err: nil, @@ -65,10 +66,10 @@ func TestParseEnzymesFromFile(t *testing.T) { want: map[string]RestrictEnzyme{ "AclI": { Name: "AclI", - RecognitionSite: "slfkj", - NoPalinCleav: "sdlkfj", - ID: "sldkfj", - Isoschizomeres: []string{"sldkjf"}, + RecognitionSite: "invalid", + NoPalinCleav: "invalid", + ID: "invalid", + Isoschizomeres: []string{"invalid", "invalid"}, }, }, err: nil, @@ -78,6 +79,8 @@ func TestParseEnzymesFromFile(t *testing.T) { // loop over test cases for _, c := range cases { got, err := ParseEnzymesFromFile(c.in) + log.Printf("parsed len: %v, expect len: %v\n", len(got), len(c.want)) + log.Printf("parsed: %v, expect: %v\n", got, c.want) // test similarity of expected and received value if !isSimilarMap(got, c.want) { @@ -178,6 +181,9 @@ func isSimilarMap(m1, m2 map[string]RestrictEnzyme) bool { if (m1 == nil) && (m2 == nil) { return true } + if (m1 == nil) || (m2 == nil) { + return false + } for k, v := range m1 { // test fields of type string if val, ok := m2[k]; !ok { diff --git a/tests/parse2.re b/tests/parse2.re index f5f5d58..4f516bb 100644 --- a/tests/parse2.re +++ b/tests/parse2.re @@ -4,4 +4,4 @@ * a parsed without a problem */ enzyme_name recognition_sequence non_palindromic_cleaveage PDB_ID isoschizomers -'AclI' 'AACGTT' 'no' +'AclI' 'AACGTT' 'no' 'A1A1' 'AclI' diff --git a/tests/parse3.re b/tests/parse3.re index e3f5a7f..3889bb2 100644 --- a/tests/parse3.re +++ b/tests/parse3.re @@ -4,3 +4,4 @@ * a parsed without a problem; even without column labels! */ 'AclI' 'AACGTT' 'no' 'A1A1' 'AclI' +'AclII' 'ACCGGT' 'no' 'A2A2' 'AclII'