Skip to content

Commit

Permalink
update http handling and add pfam warning
Browse files Browse the repository at this point in the history
  • Loading branch information
pbnjay committed Feb 17, 2023
1 parent 86abe45 commit fd2c062
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 24 deletions.
2 changes: 1 addition & 1 deletion data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

// MotifNames has human-readable names
// - mostly from http://pfam-legacy.xfam.org/help#tabview=tab9
// - mostly from http://pfam-legacy.xfam.org/help#tabview=tab9
var MotifNames = map[string]string{
"disorder": "Disordered region",
"low_complexity": "Low complexity region",
Expand Down
19 changes: 14 additions & 5 deletions data/http.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
//go:build !wasm
// +build !wasm

package data

import (
"crypto/tls"
"fmt"
"net/http"
"net/url"
"crypto/tls"
"os"
)

func httpGet(url string) (*http.Response, error) {
return http.Get(url)
}

func httpGetInsecure(url string) (*http.Response, error) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
return client.Get(url)
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
fmt.Fprintln(os.Stderr, "WARNING: making insecure request to ", url)
fmt.Fprintln(os.Stderr, " eventually this will no longer work correctly!")
return client.Get(url)
}

func httpPostForm(url string, vals url.Values) (*http.Response, error) {
Expand Down
6 changes: 6 additions & 0 deletions data/http_wasm.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build wasm
// +build wasm

package data
Expand All @@ -24,6 +25,11 @@ func httpGet(url string) (*http.Response, error) {
return resp, err
}

func httpGetInsecure(url string) (*http.Response, error) {
// I am not willing to test if this can be configured in WASM.
return httpGet(url)
}

// implements http.PostForm but makes wasm's fetch work with CORS
func httpPostForm(wurl string, vals url.Values) (*http.Response, error) {
body := strings.NewReader(vals.Encode())
Expand Down
8 changes: 4 additions & 4 deletions data/pfam.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const PfamGraphicURL = "https://pfam-legacy.xfam.org/protein/%s/graphic"

func GetPfamGraphicData(accession string) (*GraphicResponse, error) {
queryURL := fmt.Sprintf(PfamGraphicURL, accession)
resp, err := httpGet(queryURL)
resp, err := httpGetInsecure(queryURL)
if err != nil {
if err, ok := err.(net.Error); ok && err.Timeout() {
fmt.Fprintf(os.Stderr, "Unable to connect to Pfam. Check your internet connection or try again later.")
Expand All @@ -47,9 +47,9 @@ func GetPfamGraphicData(accession string) (*GraphicResponse, error) {

data := []GraphicResponse{}
err = json.Unmarshal(respBytes, &data)
//if err != nil {
// return nil, err
//}
if err != nil {
return nil, err
}
if len(data) != 1 {
return nil, fmt.Errorf("pfam returned invalid result")
}
Expand Down
38 changes: 24 additions & 14 deletions data/uniprot.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package data

import (
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -60,13 +61,27 @@ func getValueForKey(line, key string) string {
return ""
}

func uniprotDecompress(respBytes []byte) []byte {
// uniprot's REST implementation doesn't set a valid Content-Encoding header when
// gzipping the response, so Go's automatic gzip decompression doesn't work.
// since they'll probably fix it after put in this workaround, we'll just try
// to un-gzip and replace the content if it doesn't fail.

buf := bytes.NewReader(respBytes)
zrdr, err := gzip.NewReader(buf)
if err != nil {
return respBytes
}
data, err := io.ReadAll(zrdr)
if err == nil {
return data
}
return respBytes
}

func GetUniprotGraphicData(accession string) (*GraphicResponse, error) {
queryURL := fmt.Sprintf(UniprotDataURL, accession)
client := &http.Client{}
req, err := http.NewRequest("GET", queryURL, nil)
req.Header.Add("Accept-Encoding", "UTF-8")
resp, err := client.Do(req)

resp, err := httpGet(queryURL)
if err != nil {
if err, ok := err.(net.Error); ok && err.Timeout() {
fmt.Fprintf(os.Stderr, "Unable to connect to Uniprot. Check your internet connection or try again later.")
Expand All @@ -79,6 +94,7 @@ func GetUniprotGraphicData(accession string) (*GraphicResponse, error) {
if err != nil {
return nil, err
}
respBytes = uniprotDecompress(respBytes)
if resp.StatusCode != 200 {
return nil, fmt.Errorf("pfam error: %s", resp.Status)
}
Expand Down Expand Up @@ -172,26 +188,20 @@ const UNIPROTRESTURL = "https://rest.uniprot.org/uniprotkb/search?query=%s+AND+r

func GetProtID(symbol string) (string, error) {
apiURL := fmt.Sprintf(UNIPROTRESTURL, symbol)
client := &http.Client{}
req, err := http.NewRequest("GET", apiURL, nil)
req.Header.Add("Accept-Encoding", "UTF-8")
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
resp, err := http.Get(apiURL)
if err != nil {
if err, ok := err.(net.Error); ok && err.Timeout() {
fmt.Fprintf(os.Stderr, "Unable to connect to Uniprot. Check your internet connection or try again later.")
os.Exit(1)
}
return "", err
}

defer resp.Body.Close()
respBytes, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
respBytes = uniprotDecompress(respBytes)
if resp.StatusCode != 200 {
return "", fmt.Errorf("uniprot error: %s", resp.Status)
}
Expand Down

0 comments on commit fd2c062

Please sign in to comment.