diff --git a/parsers/uniprot/data/uniprot_sprot_mini.xml.gz b/parsers/uniprot/data/uniprot_sprot_mini.xml.gz index 6bad9a41..fc6c8f18 100644 Binary files a/parsers/uniprot/data/uniprot_sprot_mini.xml.gz and b/parsers/uniprot/data/uniprot_sprot_mini.xml.gz differ diff --git a/parsers/uniprot/uniprot.go b/parsers/uniprot/uniprot.go index 7e3fdd95..bb888d99 100644 --- a/parsers/uniprot/uniprot.go +++ b/parsers/uniprot/uniprot.go @@ -60,21 +60,21 @@ func ParseUniprot(r io.Reader, entries chan<- Entry, errors chan<- error) { decoder := xml.NewDecoder(r) for { decoderToken, err := decoder.Token() + if err != nil { + if err.Error() == "EOF" { + break + } errors <- err } - if decoderToken == nil { - break - } - // type assertion startElement, ok := decoderToken.(xml.StartElement) if ok && startElement.Name.Local == "entry" { - var e Entry - err = decoder.DecodeElement(&e, &startElement) - if err != nil { - errors <- err - } - entries <- e + var e Entry + err = decoder.DecodeElement(&e, &startElement) + if err != nil { + errors <- err + } + entries <- e } } close(entries) diff --git a/parsers/uniprot/uniprot_test.go b/parsers/uniprot/uniprot_test.go index fca220a3..30593bfd 100644 --- a/parsers/uniprot/uniprot_test.go +++ b/parsers/uniprot/uniprot_test.go @@ -2,8 +2,8 @@ package uniprot import ( "compress/gzip" - "os" "fmt" + "os" "testing" ) @@ -20,18 +20,18 @@ func ExampleReadUniprot() { func ExampleParseUniprot() { xmlFile, _ := os.Open("data/uniprot_sprot_mini.xml.gz") - unzippedBytes, _ := gzip.NewReader(xmlFile) + 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) + decoderErrors := make(chan error, 100) go ParseUniprot(unzippedBytes, entries, decoderErrors) var entry Entry - for singleEntry := range entries { - entry = singleEntry - } - fmt.Println(entry.Accession[0]) - // Output: O55723 + for singleEntry := range entries { + entry = singleEntry + } + fmt.Println(entry.Accession[0]) + // Output: O55723 } func TestReadUniprot(t *testing.T) { @@ -44,4 +44,15 @@ func TestReadUniprot(t *testing.T) { if err == nil { t.Errorf("Failed to fail on empty file") } + + _, errors, err := ReadUniprot("data/uniprot_sprot_mini.xml.gz") + if err != nil { + t.Errorf("Failed on real file with error: %v", err) + } + + for err := range errors { + if err != nil { + t.Errorf("Failed during parsing with error: %v", err) + } + } }