-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcitation_graph.R
58 lines (45 loc) · 1.58 KB
/
citation_graph.R
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
require(plyr)
require(XML)
require(RCurl)
require(readxl)
require(visNetwork)
# We start with a list PubMed ids, for which we would like to have the citation graph
pmids <- c(10097079)
pmids_ch <- paste(pmids, collapse=",")
citegraph <- NULL
for(pmid in pmids){
# Get more info with the pmid id
tryCatch({
# We build the URL to retrieve the citing pmids
path <- paste("https://www.ncbi.nlm.nih.gov/pubmed?linkname=pubmed_pubmed_citedin&from_uid=",
pmid,
"&report=uilist&format=text&dispmax=200", sep="")
f <- file(path)
data <- readLines(f, warn = F)
citing <-strsplit(xmlToList(xmlParse(data, asText = T))[1], "\n")[[1]]
close(f)
# Then we create a table containing ll these pmids
if(length(citing) < 200){
for(pm in citing){
citegraph <- rbind(citegraph, data.frame(pmid=as.numeric(pmid), citing=as.numeric(pm)))
}
}
message(paste0(pmid," done: ",length(citing)," citations"))
}, warning = function(w) {
message(w)
}, error = function(e) {
message(e)
})
}
# We use visNetwork for the vizualisation of the network
# For that we need a nodes and edges tables.
nodes <- data.frame(id=unique(c(citegraph$pmid, citegraph$citing)))
for(i in 1:nrow(nodes)){
nodes$color[i] <- "lightblue"
if(grepl(nodes$id[i], pmids_ch)){
nodes$color[i] <- "red"
}
}
edges <- data.frame(from = citegraph[,2], to = citegraph[,1])
nodes$label <- nodes$id
visNetwork(nodes, edges, width = "100%", main = "Graph network of Citation")