Skip to content

Commit

Permalink
Simplified names in parsers (#168)
Browse files Browse the repository at this point in the history
* Simplified names

* Fixed up tests

Co-authored-by: Tim <[email protected]>
  • Loading branch information
Koeng101 and TimothyStiles authored Jun 5, 2021
1 parent f8cbd8f commit 7825244
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 30 deletions.
14 changes: 7 additions & 7 deletions parsers/rebase/rebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
14 changes: 7 additions & 7 deletions parsers/rebase/rebase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
16 changes: 8 additions & 8 deletions parsers/uniprot/uniprot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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()
Expand Down
17 changes: 9 additions & 8 deletions parsers/uniprot/uniprot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
}
Expand Down

0 comments on commit 7825244

Please sign in to comment.