From 78252441551e0f445ced8af8f6ec87020f87d05b Mon Sep 17 00:00:00 2001 From: Koeng101 Date: Sat, 5 Jun 2021 09:25:25 -0700 Subject: [PATCH] Simplified names in parsers (#168) * Simplified names * Fixed up tests Co-authored-by: Tim --- parsers/rebase/rebase.go | 14 +++++++------- parsers/rebase/rebase_test.go | 14 +++++++------- parsers/uniprot/uniprot.go | 16 ++++++++-------- parsers/uniprot/uniprot_test.go | 17 +++++++++-------- 4 files changed, 31 insertions(+), 30 deletions(-) diff --git a/parsers/rebase/rebase.go b/parsers/rebase/rebase.go index 8f07e047..56622f67 100644 --- a/parsers/rebase/rebase.go +++ b/parsers/rebase/rebase.go @@ -166,8 +166,8 @@ type Enzyme struct { References string `json:"references"` } -// ParseRebase parses the Rebase database into a map of enzymes -func ParseRebase(file []byte) map[string]Enzyme { +// Parse parses the Rebase database into a map of enzymes +func Parse(file []byte) map[string]Enzyme { // Setup some variables var enzyme Enzyme enzymeMap := make(map[string]Enzyme) @@ -244,18 +244,18 @@ func ParseRebase(file []byte) map[string]Enzyme { return enzymeMap } -// ReadRebase returns an enzymeMap from a Rebase data dump -func ReadRebase(path string) (map[string]Enzyme, error) { +// Read returns an enzymeMap from a Rebase data dump +func Read(path string) (map[string]Enzyme, error) { file, err := ioutil.ReadFile(path) if err != nil { return map[string]Enzyme{}, err } - enzymeMap := ParseRebase(file) + enzymeMap := Parse(file) return enzymeMap, nil } -// ExportRebase returns a json file of the Rebase database -func ExportRebase(enzymeMap map[string]Enzyme) []byte { +// Export returns a json file of the Rebase database +func Export(enzymeMap map[string]Enzyme) []byte { jsonRebase, _ := json.Marshal(enzymeMap) return jsonRebase } diff --git a/parsers/rebase/rebase_test.go b/parsers/rebase/rebase_test.go index 6f9c87ce..3072996f 100644 --- a/parsers/rebase/rebase_test.go +++ b/parsers/rebase/rebase_test.go @@ -5,21 +5,21 @@ import ( "testing" ) -func ExampleReadRebase() { - enzymeMap, _ := ReadRebase("data/rebase_test.txt") +func ExampleRead() { + enzymeMap, _ := Read("data/rebase_test.txt") fmt.Println(enzymeMap["AarI"].MicroOrganism) // Output: Arthrobacter aurescens SS2-322 } -func ExampleExportRebase() { - enzymeMap, _ := ReadRebase("data/rebase_test.txt") - enzymeJson := ExportRebase(enzymeMap) +func ExampleExport() { + enzymeMap, _ := Read("data/rebase_test.txt") + enzymeJson := Export(enzymeMap) fmt.Println(string(enzymeJson)[:100]) // Output: {"AaaI":{"name":"AaaI","isoschizomers":["XmaIII","BseX3I","BsoDI","BstZI","EagI","EclXI","Eco52I","S } -func TestReadRebase(t *testing.T) { - _, err := ReadRebase("data/FAKE.txt") +func TestRead(t *testing.T) { + _, err := Read("data/FAKE.txt") if err == nil { t.Errorf("Failed to error on fake file") } diff --git a/parsers/uniprot/uniprot.go b/parsers/uniprot/uniprot.go index bb888d99..b9f8714b 100644 --- a/parsers/uniprot/uniprot.go +++ b/parsers/uniprot/uniprot.go @@ -23,10 +23,10 @@ xml.go from uniprot.xsd Each protein in Uniprot is known as an "Entry" (as defined in xml.go). -The function ParseUniprot stream-reads Uniprot into an Entry channel, from which -you can use the entries however you want. ReadUniprot simplifies reading gzipped +The function Parse stream-reads Uniprot into an Entry channel, from which +you can use the entries however you want. Read simplifies reading gzipped files from a disk into an Entry channel, essentially just preparing the reader for -ParseUniprot. +Parse. Cheers, Keoni @@ -37,10 +37,10 @@ Keoni ******************************************************************************/ -// ReadUniprot reads a gzipped Uniprot XML dump. Failing to open the XML dump +// Read reads a gzipped Uniprot XML dump. Failing to open the XML dump // gives a single error, while errors encountered while decoding the XML dump // are added to the errors channel. -func ReadUniprot(path string) (chan Entry, chan error, error) { +func Read(path string) (chan Entry, chan error, error) { entries := make(chan Entry, 100) // if you don't have a buffered channel, nothing will be read in loops on the channel. decoderErrors := make(chan error, 100) xmlFile, err := os.Open(path) @@ -51,12 +51,12 @@ func ReadUniprot(path string) (chan Entry, chan error, error) { if err != nil { return entries, decoderErrors, err } - go ParseUniprot(unzippedBytes, entries, decoderErrors) + go Parse(unzippedBytes, entries, decoderErrors) return entries, decoderErrors, nil } -// ParseUniprot parses Uniprot entries into a channel. -func ParseUniprot(r io.Reader, entries chan<- Entry, errors chan<- error) { +// Parse parses Uniprot entries into a channel. +func Parse(r io.Reader, entries chan<- Entry, errors chan<- error) { decoder := xml.NewDecoder(r) for { decoderToken, err := decoder.Token() diff --git a/parsers/uniprot/uniprot_test.go b/parsers/uniprot/uniprot_test.go index 30593bfd..8e22845e 100644 --- a/parsers/uniprot/uniprot_test.go +++ b/parsers/uniprot/uniprot_test.go @@ -7,8 +7,8 @@ import ( "testing" ) -func ExampleReadUniprot() { - entries, _, _ := ReadUniprot("data/uniprot_sprot_mini.xml.gz") +func ExampleRead() { + entries, _, _ := Read("data/uniprot_sprot_mini.xml.gz") var entry Entry for singleEntry := range entries { @@ -18,13 +18,14 @@ func ExampleReadUniprot() { // Output: O55723 } -func ExampleParseUniprot() { +func ExampleParse() { xmlFile, _ := os.Open("data/uniprot_sprot_mini.xml.gz") unzippedBytes, _ := gzip.NewReader(xmlFile) entries := make(chan Entry, 100) // if you don't have a buffered channel, nothing will be read in loops on the channel. decoderErrors := make(chan error, 100) - go ParseUniprot(unzippedBytes, entries, decoderErrors) + + go Parse(unzippedBytes, entries, decoderErrors) var entry Entry for singleEntry := range entries { @@ -34,18 +35,18 @@ func ExampleParseUniprot() { // Output: O55723 } -func TestReadUniprot(t *testing.T) { - _, _, err := ReadUniprot("data/test") +func TestRead(t *testing.T) { + _, _, err := Read("data/test") if err == nil { t.Errorf("Failed to fail on non-gzipped file") } - _, _, err = ReadUniprot("data/FAKE") + _, _, err = Read("data/FAKE") if err == nil { t.Errorf("Failed to fail on empty file") } - _, errors, err := ReadUniprot("data/uniprot_sprot_mini.xml.gz") + _, errors, err := Read("data/uniprot_sprot_mini.xml.gz") if err != nil { t.Errorf("Failed on real file with error: %v", err) }