-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcdseq.go
57 lines (49 loc) · 1.17 KB
/
cdseq.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"time"
)
// Takes an ID and returns the fasta sequence of the CDS from Ensembl
func GetSequence(id string) string {
client := &http.Client{}
baseurl := "http://rest.ensembl.org"
ext := "/sequence/id/" + id + "?type=cds;multiple_sequences=1"
req, err := http.NewRequest("GET", baseurl+ext, nil)
req.Header.Set("content-type", "text/x-fasta")
if err != nil {
log.Fatal(err)
}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
seq, err := ioutil.ReadAll(resp.Body)
return string(seq)
}
func main() {
log.SetOutput(os.Stderr)
// Make a scanner to receive a stream of IDs from Stdin
// pass the IDs to the Ensembl REST API and write the cds sequence
// as fasta
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
id := scanner.Text()
seq := GetSequence(id)
// Be polite, don't hammer the API
time.Sleep(100 * time.Millisecond)
d := []byte(seq)
err := ioutil.WriteFile(id+".cds.all.fa", d, 0644)
if err != nil {
panic(err)
}
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading standard input:", err)
}
}