Skip to content

Instantly share code, notes, and snippets.

@cuongld2

cuongld2/main.go

Created Dec 12, 2020
Embed
What would you like to do?
convert chromosome format
package main
import (
"encoding/csv"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
)
func BytesToString(data []byte) string {
return string(data[:])
}
func main() {
buildNumber := "hg38"
file, err := os.Create("chrConversion.csv")
if err != nil {
log.Fatal(err)
}
defer func() {
err = file.Close()
if err != nil {
log.Println(err)
}
}()
writer := csv.NewWriter(file)
defer writer.Flush()
for chr := 1; chr <= 22; chr++ {
var data []string
client := &http.Client{}
req, err := http.NewRequest("GET", "https://mutalyzer.nl/json/chromAccession?"+"build="+buildNumber+"&name="+"chr"+strconv.FormatInt(int64(chr), 10), nil)
if err != nil {
log.Fatal(err)
}
resp, err := client.Do(req)
if resp == nil {
log.Fatal(err)
}
if resp.StatusCode != 200 {
log.Fatal(err)
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
chrConverted := BytesToString(bodyBytes)
removeDouble := strings.Replace(chrConverted, "\"", "", 2)
data = append(data, "chr"+strconv.FormatInt(int64(chr), 10), removeDouble)
err = writer.Write(data)
if err != nil {
log.Fatal(err)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment