Skip to content

Commit

Permalink
refactor data package
Browse files Browse the repository at this point in the history
  • Loading branch information
pbnjay committed Mar 19, 2019
1 parent d463e5e commit 38f71b3
Show file tree
Hide file tree
Showing 4 changed files with 260 additions and 220 deletions.
80 changes: 80 additions & 0 deletions data/data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//
// Lollipops diagram generation framework for genetic variations.
// Copyright (C) 2015 Jeremy Jay <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package data

import (
"encoding/json"
"os"
"strings"
)

// MotifNames has human-readable names
// - mostly from http://pfam.xfam.org/help#tabview=tab9
var MotifNames = map[string]string{
"disorder": "Disordered region",
"low_complexity": "Low complexity region",
"sig_p": "Signal peptide region",
"coiled_coil": "Coiled-coil motif",
"transmembrane": "Transmembrane region",
}

// GraphicFeature is a generic representation of various feature responses
type GraphicFeature struct {
Color string `json:"colour"`
Text string `json:"text"`
Type string `json:"type"`
Start json.Number `json:"start"`
End json.Number `json:"end"`
Link string `json:"href"`
Metadata GraphicMetadata `json:"metadata"`
}

type GraphicMetadata struct {
Description string `json:"description"`
Identifier string `json:"identifier"`
}

type GraphicResponse struct {
Length json.Number `json:"length"`
Metadata GraphicMetadata `json:"metadata"`
Motifs []GraphicFeature `json:"motifs"`
Regions []GraphicFeature `json:"regions"`
}

func GetLocalGraphicData(filename string) (*GraphicResponse, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
pf := &GraphicResponse{}
err = json.NewDecoder(f).Decode(pf)
f.Close()
for i, x := range pf.Motifs {
if x.Link != "" && !strings.Contains(x.Link, "://") {
x.Link = "http://pfam.xfam.org" + x.Link
pf.Motifs[i] = x
}
}
for i, x := range pf.Regions {
if x.Link != "" && !strings.Contains(x.Link, "://") {
x.Link = "http://pfam.xfam.org" + x.Link
pf.Regions[i] = x
}
}
return pf, err
}
149 changes: 0 additions & 149 deletions data/fetch_uniprot.go

This file was deleted.

71 changes: 71 additions & 0 deletions data/pfam.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//
// Lollipops diagram generation framework for genetic variations.
// Copyright (C) 2015 Jeremy Jay <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package data

import (
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
)

const PfamGraphicURL = "http://pfam.xfam.org/protein/%s/graphic"

func GetPfamGraphicData(accession string) (*GraphicResponse, error) {
queryURL := fmt.Sprintf(PfamGraphicURL, accession)
resp, err := http.Get(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.")
os.Exit(1)
}
return nil, err
}
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("pfam error: %s", resp.Status)
}

data := []GraphicResponse{}
err = json.Unmarshal(respBytes, &data)
//if err != nil {
// return nil, err
//}
if len(data) != 1 {
return nil, fmt.Errorf("pfam returned invalid result")
}
r := data[0]
for i, x := range r.Motifs {
if x.Link != "" {
x.Link = "http://pfam.xfam.org" + x.Link
r.Motifs[i] = x
}
}
for i, x := range r.Regions {
if x.Link != "" {
x.Link = "http://pfam.xfam.org" + x.Link
r.Regions[i] = x
}
}
return &r, nil
}
Loading

0 comments on commit 38f71b3

Please sign in to comment.