Exemple #1
0
func remoteName2TaxID(host string, port int, useRegexp bool, nameClass string, names []string) {
	msg := taxon.RemoteQueryName2TaxID(host, port, useRegexp, nameClass, names)
	if msg.Status != "OK" {
		log.Error(msg.Message)
	}

	for name, items := range msg.TaxIDs {
		idnames := make([]string, len(items))
		for i, item := range items {
			idnames[i] = fmt.Sprintf("%d(%s)", item.TaxID, item.ScientificName)
		}
		fmt.Printf("%s\t%s\n", name, strings.Join(idnames, ","))
	}
}
Exemple #2
0
func remoteName2TaxIDByFile(host string, port int, useRegexp bool, nameClass string, dataFile string, chunkSize int, threads int) {
	if chunkSize <= 0 {
		chunkSize = 1000
	}
	fn := func(line string) (interface{}, bool, error) {
		line = strings.TrimRight(line, "\n")
		if line == "" {
			return "", false, nil
		}
		return line, true, nil
	}
	reader, err := breader.NewBufferedReader(dataFile, threads, chunkSize, fn)
	checkError(err)

	chResults := make(chan taxon.MssageName2TaxIDMap, threads)

	// receive result and print
	chDone := make(chan int)
	go func() {
		for msg := range chResults {
			if msg.Status != "OK" {
				log.Error(msg.Message)
			}
			for name, items := range msg.TaxIDs {
				idnames := make([]string, len(items))
				for i, item := range items {
					idnames[i] = fmt.Sprintf("%d(%s)", item.TaxID, item.ScientificName)
				}
				fmt.Printf("%s\t%s\n", name, strings.Join(idnames, ","))
			}
		}
		chDone <- 1
	}()

	// querying
	var wg sync.WaitGroup
	tokens := make(chan int, threads)
	for chunk := range reader.Ch {
		tokens <- 1
		wg.Add(1)

		queries := make([]string, len(chunk.Data))
		for i, data := range chunk.Data {
			queries[i] = data.(string)
		}

		go func(queries []string) {
			defer func() {
				wg.Done()
				<-tokens
			}()

			msg := taxon.RemoteQueryName2TaxID(host, port, useRegexp, nameClass, queries)
			checkError(err)
			chResults <- msg
		}(queries)
	}
	wg.Wait()
	close(chResults)
	<-chDone
}