Skip to content

Instantly share code, notes, and snippets.

@cuongld2
Created October 20, 2020 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cuongld2/916da9a3acde4569a9c8a81746ab0b07 to your computer and use it in GitHub Desktop.
Save cuongld2/916da9a3acde4569a9c8a81746ab0b07 to your computer and use it in GitHub Desktop.
Without Concurrency
func TestWithoutConcurrent(t *testing.T) {
listGenes := []string {"DDX11L1","WASH7P", "OR4G4P", "OR4G11P", "WBP1LP6",
"LINC02593","SAMD11","NOC2L", "KLHL17", "PLEKHN1","PERM1"}
start := time.Now()
defer func() {
fmt.Println("Execution Time: ", time.Since(start))
}()
//listGenes := []string {"DDX11L1","WASH7P", "OR4G4P", "OR4G11P", "WBP1LP6",
// "LINC02593","SAMD11","NOC2L", "KLHL17", "PLEKHN1","PERM1"}
var data [][]string
file, err := os.Create("resources/gene_geneID_NCBI.csv")
checkError("Cannot create file", err)
defer file.Close()
for _, gene := range listGenes {
client := &http.Client{}
req, _ := http.NewRequest("GET", "https://www.ncbi.nlm.nih.gov/gene/?term="+gene, nil)
// I wish explicitly setting this Host header would take precedence
req.Header.Set("Host", "www.ncbi.nlm.nih.gov")
resp, err := client.Do(req)
//resp, err := client.Get("https://www.ncbi.nlm.nih.gov/gene/?term=" + gene)
if err != nil {
log.Print(err)
}
if resp.Body != nil {
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
log.Print(err.Error())
log.Println(gene)
}
defer resp.Body.Close()
geneID := doc.Find("span[class='geneid']").Contents().Text()
if geneID != "" {
geneID = strings.Split(strings.Split(geneID, "Gene ID: ")[1], ",")[0]
} else {
geneID = doc.Find("ul[class='ncbi-inline-list '] > li").Text()
if geneID != "" {
geneIDList := strings.Split(geneID, "GeneID: ")
if len(geneIDList) > 1 {
geneID = geneIDList[1]
}
}
}
var geneCSV []string
geneCSV = append(geneCSV, gene, geneID)
data = append(data, geneCSV)
} else {
log.Print(gene + " has body is response body nil")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment