-
Notifications
You must be signed in to change notification settings - Fork 0
/
pubmed.pl
executable file
·158 lines (154 loc) · 6.03 KB
/
pubmed.pl
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/perl
use strict;
use utf8;
use Text::Unidecode;
sub uniq {
my %seen;
grep !$seen{$_}++, @_;
}
#
# Extract abstracts from Medline XML files.
#
while (<STDIN>) {
if (/<PubmedArticle>/) {
my %medline = (
"pmid" => "",
"ids" => "",
"authors" => "",
"journal" => "",
"volume" => "",
"issue" => "",
"pages" => "",
"year" => "",
"title" => "",
"abstract" => "",
"exclude" => 0,
"mesh" => "",
"refids" => ""
);
my @refids = ();
while (<STDIN>) {
last if /<\/PubmedArticle>/;
if (/<MedlineCitation .*?>/) {
while (<STDIN>) {
last if /<\/MedlineCitation>/;
$medline{"pmid"} = $2 if /<(MedlineID|PMID).*?>(.+?)<\/(MedlineID|PMID)>/ and $medline{"pmid"} eq "";
$medline{"ids"} .= "|PMCID:".$1 if /<OtherID Source="NLM">PMC([0-9]+).*?<\/OtherID>/;
$medline{"ids"} .= "|".uc($1).":".$2 if /<ELocationID EIdType="(doi|pii)" ValidYN="[YN]">(.+?)<\/ELocationID>/;
$medline{"journal"} = $1 if /<MedlineTA>(.*?)<\/MedlineTA>/ and $medline{"journal"} eq "";
$medline{"volume"} = $1 if /<Volume>(.+?)<\/Volume>/ and $medline{"volume"} eq "";
$medline{"issue"} = $1 if /<Issue>(.+?)<\/Issue>/ and $medline{"issue"} eq "";
$medline{"pages"} = $1 if /<MedlinePgn>(.+?)<\/MedlinePgn>/ and $medline{"pages"} eq "";
$medline{"title"} = $1 if /<ArticleTitle>(.+?)<\/ArticleTitle>/ and $medline{"title"} eq "";
if (/<Abstract( .*?)?>/) {
while (<STDIN>) {
last if /<\/Abstract>/;
if (/<AbstractText.*? Label="(.+?)".*?>(.+?)<\/AbstractText>/ and $1 ne "UNLABELLED") {
if ($medline{"abstract"} eq "") {
$medline{"abstract"} = $1.": ".$2;
}
else {
$medline{"abstract"} .= " ".$1.": ".$2;
}
}
elsif (/<AbstractText.*?>(.+?)<\/AbstractText>/) {
if ($medline{"abstract"} eq "") {
$medline{"abstract"} = $1;
}
else {
$medline{"abstract"} .= " ".$1;
}
}
}
}
$medline{"exclude"} = 1 if /<PublicationType UI=\"D016441\">/;
$medline{"mesh"} .= "$1 " if /<DescriptorName MajorTopicYN=\"[YN]\">(.*)<\/DescriptorName>/;
if (/<Author ValidYN="[YN]">/) {
my $initials = "";
my $lastname = "";
while (<STDIN>) {
last if /<\/Author>/;
$initials = $1 if /<Initials>(.+?)<\/Initials>/;
$lastname = $1 if /<LastName>(.+?)<\/LastName>/;
}
if ($initials ne "" and $lastname ne "") {
$medline{"authors"} .= ", " unless $medline{"authors"} eq "";
$medline{"authors"} .= $lastname." ".$initials;
}
}
if (/<PubDate>/) {
while (<STDIN>) {
last if /<\/PubDate>/;
$medline{"year"} = $1 if /<Year>(.*?)<\/Year>/ and $medline{"year"} eq "";
$medline{"year"} = $1 if /<MedlineDate>([0-9][0-9][0-9][0-9]) .*?<\/MedlineDate>/ and $medline{"year"} eq "";
}
}
}
next if $medline{"pmid"} eq "";
if ($medline{"exclude"}) {
$medline{"title"} = "";
$medline{"abstract"} = "";
}
foreach my $section ("title", "abstract") {
next unless exists $medline{$section};
$medline{$section} =~ s/^[ \t\r\n]+//;
$medline{$section} =~ s/[ \t\r\n]*$/ /;
$medline{$section} =~ s/[\t\r\n]+/\t/g;
}
my $where = $medline{"journal"};
if ($where ne "") {
$where .= ".";
if ($medline{"volume"} ne "") {
$where .= " ".$medline{"volume"};
$where .= "(".$medline{"issue"}.")" if $medline{"issue"} ne "";
$where .= ":".$medline{"pages"} if $medline{"pages"} ne "";
}
else {
$where .= " ".$medline{"pages"} if $medline{"pages"} ne "";
}
}
#wrap all superscripts in parentheses
while ($medline{"title"} =~ s/<(sup|mml:msup)>([^<>]*)<\/\1>/\($2\)/g) {}
while ($medline{"abstract"} =~ s/<(sup|mml:msup)>([^<>]*)<\/\1>/\($2\)/g) {}
#remove all other XML tags
while ($medline{"title"} =~ s/<[^<>]*>//g) {}
while ($medline{"abstract"} =~ s/<[^<>]*>//g) {}
$medline{"where"} =~ s/"/"/g;
$medline{"where"} =~ s/'/'/g;
$medline{"where"} =~ s/>/>/g;
$medline{"where"} =~ s/</</g;
$medline{"where"} =~ s/&/&/g;
$medline{"title"} =~ s/"/"/g;
$medline{"title"} =~ s/'/'/g;
$medline{"title"} =~ s/>/>/g;
$medline{"title"} =~ s/</</g;
$medline{"title"} =~ s/&/&/g;
$medline{"abstract"} =~ s/"/"/g;
$medline{"abstract"} =~ s/'/'/g;
$medline{"abstract"} =~ s/>/>/g;
$medline{"abstract"} =~ s/</</g;
$medline{"abstract"} =~ s/&/&/g;
#printf "%s\t%s\t", $medline{"pmid"}.$medline{"ids"}, $medline{"year"};
#printf "PMID:%s\t%s\t%s\t%s\t%s\t%s\t%s\n", $medline{"pmid"}.$medline{"ids"}, $medline{"authors"}, $where, $medline{"year"}, $medline{"title"}, $medline{"abstract"};
}
elsif (/<DeleteCitation>/) {
while (<STDIN>) {
last if /<\/DeleteCitation>/;
printf "%s\t", unidecode($2) if /<(MedlineID|PMID).*?>(.+?)<\/(MedlineID|PMID)>/;
#printf "PMID:%s\n", unidecode($2) if /<(MedlineID|PMID).*?>(.+?)<\/(MedlineID|PMID)>/;
}
}
if (/<ReferenceList>/) {
while (<STDIN>) {
last if /<\/ReferenceList>/;
#$medline{"refids"} .= $2." " if /<ArticleId IdType="(.+?)">(.+?)<\/ArticleId>/ and $1 eq "pubmed";
push @refids, $2 if ((/<ArticleId IdType="(.+?)">(?:PMID:)?([0-9]+)<\/ArticleId>/) && (int($2) < 2147483647) && ($1 eq ("pubmed" || "pmcid"))); # match all
#$medline{"refids"} .= $2."($1) " if /<ArticleId IdType="(.+?)">(.+?)<\/ArticleId>/ and $1 ne "pubmed";
}
}
}
my @uniqrefids = uniq(@refids);
$medline{"refids"} = join(' ', @uniqrefids);
printf "%s\t%s\t%s\n", $medline{"pmid"}, $medline{"year"}, $medline{"refids"};
}
}