Skip to content

Commit 38f71b3

Browse files
committed
refactor data package
1 parent d463e5e commit 38f71b3

File tree

4 files changed

+260
-220
lines changed

4 files changed

+260
-220
lines changed

data/data.go

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//
2+
// Lollipops diagram generation framework for genetic variations.
3+
// Copyright (C) 2015 Jeremy Jay <[email protected]>
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
18+
package data
19+
20+
import (
21+
"encoding/json"
22+
"os"
23+
"strings"
24+
)
25+
26+
// MotifNames has human-readable names
27+
// - mostly from http://pfam.xfam.org/help#tabview=tab9
28+
var MotifNames = map[string]string{
29+
"disorder": "Disordered region",
30+
"low_complexity": "Low complexity region",
31+
"sig_p": "Signal peptide region",
32+
"coiled_coil": "Coiled-coil motif",
33+
"transmembrane": "Transmembrane region",
34+
}
35+
36+
// GraphicFeature is a generic representation of various feature responses
37+
type GraphicFeature struct {
38+
Color string `json:"colour"`
39+
Text string `json:"text"`
40+
Type string `json:"type"`
41+
Start json.Number `json:"start"`
42+
End json.Number `json:"end"`
43+
Link string `json:"href"`
44+
Metadata GraphicMetadata `json:"metadata"`
45+
}
46+
47+
type GraphicMetadata struct {
48+
Description string `json:"description"`
49+
Identifier string `json:"identifier"`
50+
}
51+
52+
type GraphicResponse struct {
53+
Length json.Number `json:"length"`
54+
Metadata GraphicMetadata `json:"metadata"`
55+
Motifs []GraphicFeature `json:"motifs"`
56+
Regions []GraphicFeature `json:"regions"`
57+
}
58+
59+
func GetLocalGraphicData(filename string) (*GraphicResponse, error) {
60+
f, err := os.Open(filename)
61+
if err != nil {
62+
return nil, err
63+
}
64+
pf := &GraphicResponse{}
65+
err = json.NewDecoder(f).Decode(pf)
66+
f.Close()
67+
for i, x := range pf.Motifs {
68+
if x.Link != "" && !strings.Contains(x.Link, "://") {
69+
x.Link = "http://pfam.xfam.org" + x.Link
70+
pf.Motifs[i] = x
71+
}
72+
}
73+
for i, x := range pf.Regions {
74+
if x.Link != "" && !strings.Contains(x.Link, "://") {
75+
x.Link = "http://pfam.xfam.org" + x.Link
76+
pf.Regions[i] = x
77+
}
78+
}
79+
return pf, err
80+
}

data/fetch_uniprot.go

-149
This file was deleted.

data/pfam.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//
2+
// Lollipops diagram generation framework for genetic variations.
3+
// Copyright (C) 2015 Jeremy Jay <[email protected]>
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
18+
package data
19+
20+
import (
21+
"encoding/json"
22+
"fmt"
23+
"io/ioutil"
24+
"net"
25+
"net/http"
26+
"os"
27+
)
28+
29+
const PfamGraphicURL = "http://pfam.xfam.org/protein/%s/graphic"
30+
31+
func GetPfamGraphicData(accession string) (*GraphicResponse, error) {
32+
queryURL := fmt.Sprintf(PfamGraphicURL, accession)
33+
resp, err := http.Get(queryURL)
34+
if err != nil {
35+
if err, ok := err.(net.Error); ok && err.Timeout() {
36+
fmt.Fprintf(os.Stderr, "Unable to connect to Pfam. Check your internet connection or try again later.")
37+
os.Exit(1)
38+
}
39+
return nil, err
40+
}
41+
respBytes, err := ioutil.ReadAll(resp.Body)
42+
if err != nil {
43+
return nil, err
44+
}
45+
if resp.StatusCode != 200 {
46+
return nil, fmt.Errorf("pfam error: %s", resp.Status)
47+
}
48+
49+
data := []GraphicResponse{}
50+
err = json.Unmarshal(respBytes, &data)
51+
//if err != nil {
52+
// return nil, err
53+
//}
54+
if len(data) != 1 {
55+
return nil, fmt.Errorf("pfam returned invalid result")
56+
}
57+
r := data[0]
58+
for i, x := range r.Motifs {
59+
if x.Link != "" {
60+
x.Link = "http://pfam.xfam.org" + x.Link
61+
r.Motifs[i] = x
62+
}
63+
}
64+
for i, x := range r.Regions {
65+
if x.Link != "" {
66+
x.Link = "http://pfam.xfam.org" + x.Link
67+
r.Regions[i] = x
68+
}
69+
}
70+
return &r, nil
71+
}

0 commit comments

Comments
 (0)