aboutsummaryrefslogtreecommitdiffstats
path: root/extra/mag/magrefs-mem.go
blob: ea7f207b5f0b29a1028e34e275403517976dd9e7 (plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// magrefs turns MAG references into a doi-to-doi version
// PaperReferences.txt is a two column file: PaperId, PaperReferenceId.
//
// Use in memory lookup table.
package main

import (
	"bufio"
	"flag"
	"fmt"
	"io"
	"log"
	"os"
	"strconv"
	"strings"
	"sync/atomic"

	_ "github.com/mattn/go-sqlite3"
	"github.com/miku/parallel"
)

var (
	mappingFile = flag.String("f", "", "two column TSV with key and value")
	cache       = make(map[int]string)

	edgeMissingDOI int64 // number of edges that have one or zero DOI, but not both
)

func populateCache(r io.Reader, cache map[int]string) error {
	var (
		br = bufio.NewReader(r)
		i  int
	)
	for {
		line, err := br.ReadString('\n')
		if err == io.EOF {
			break
		}
		if err != nil {
			return err
		}
		fields := strings.Split(line, "\t")
		if len(fields) < 2 {
			continue
		}
		a, b := strings.TrimSpace(fields[0]), strings.TrimSpace(fields[1])
		id, err := strconv.Atoi(a)
		if err != nil {
			log.Println("skipping invalid id: %s", line)
			continue
		}
		cache[id] = b
		i++
		if i%1000000 == 0 {
			log.Printf("%d", i)
		}
	}
	return nil
}

func main() {
	flag.Parse()
	if *mappingFile == "" {
		log.Fatal("mapping file required")
	}
	f, err := os.Open(*mappingFile)
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()
	if err := populateCache(f, cache); err != nil {
		log.Fatal(err)
	}
	pp := parallel.NewProcessor(os.Stdin, os.Stdout, func(p []byte) ([]byte, error) {
		var (
			s, t, v, w     string
			source, target int
			err            error
			ok             bool
			complete       = true
			fields         = strings.Split(string(p), "\t")
		)
		if len(fields) < 2 {
			return nil, nil
		}
		s, t = strings.TrimSpace(fields[0]), strings.TrimSpace(fields[1])
		if source, err = strconv.Atoi(s); err != nil {
			log.Printf("skipping invalid source id: %s (%v)", s, err)
			return nil, nil
		}
		if target, err = strconv.Atoi(t); err != nil {
			log.Printf("skipping invalid target id: %s (%v)", t, err)
			return nil, nil
		}
		if v, ok = cache[source]; !ok {
			complete = false
		}
		if w, ok = cache[target]; !ok {
			complete = false
		}
		if !complete {
			atomic.AddInt64(&edgeMissingDOI, 1)
			return nil, nil
		}
		line := fmt.Sprintf("%s\t%s\n", v, w)
		return []byte(line), nil
	})
	if err := pp.Run(); err != nil {
		log.Fatal(err)
	}
	log.Printf("edges without DOI: %d", edgeMissingDOI)
}