File: gmod_bulk_load_pubmed.pl

package info (click to toggle)
libchado-perl 1.31-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 44,716 kB
  • sloc: sql: 282,721; xml: 192,553; perl: 25,524; sh: 102; python: 73; makefile: 57
file content (486 lines) | stat: -rw-r--r-- 11,439 bytes parent folder | download | duplicates (4)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#!/usr/bin/perl
=head1 NAME

gmod_bulk_load_pubmed.pl - stores pubmed entries in the database

=head1 DESCRIPTION

 Usage: perl gmod_bulk_load_pubmed.pl -H [dbhost] -D [dbname]  [-vt] -i file


parameters

=over 6

=item -H

hostname for database 

=item -D

database name 

=item -i 

input file [required]

=item -v

verbose output
 
=item -t

trial mode. Do not perform any store operations at all.

=item -g

GMOD database profile name (can provide host and DB name) Default: 'default'

=back

=head2 If not using a GMOD database profile (option -g) then you must provide the following parameters

=over 3

=item -u

user name 

=item -d 

database driver name (i.e. 'Pg' for postgres)

=item -p 

password for youe user to connect to the database


=back

The script stores pubmed entries in the database.
Existing ones are ignored. 
Input file should contain a list of pubmed ids. Then a new Publication object (Bio::Chado::Schema::Pub::Pub) with accession= PMID,
the publication specs are fetched from Entrez (using eUtils) which sets the different fields in the Publication object. When the publication is stored, a new dbxref is stored first (see Chado General module)   

=head2 This script works with Chado schema and accesse the following tables:

=over 5

=item pub

=item pubauthor

=item pubprop

=item dbxref

=item pub_dbxref


=back


=head1 AUTHOR

Naama Menda <nm249@cornell.edu>

=head1 VERSION AND DATE

Version 1.1, April 2010.

=cut


#! /usr/bin/env perl
use strict;
use warnings;


use Bio::GMOD::Config;
use Bio::GMOD::DB::Config;

use Bio::Chado::Schema;
use XML::Twig;
use LWP::Simple qw/ get /;

use Getopt::Std;

our ($opt_H, $opt_D, $opt_v, $opt_t, $opt_i,  $opt_g, $opt_p, $opt_d, $opt_u);

getopts('H:D:i:p:g:p:d:u:tv');

our $publication;


my $dbhost = $opt_H;
my $dbname = $opt_D;
my $infile = $opt_i;
my $pass = $opt_p;
my $driver = $opt_d;
my $user = $opt_u;

my $DBPROFILE = $opt_g ;

print "H= $opt_H, D= $opt_D, u=$opt_u, d=$opt_d, v=$opt_v, t=$opt_t, i=$opt_i  \n";

my $port;
my ($dbh, $schema);

if ($opt_g) {
    my $DBPROFILE = $opt_g;
    $DBPROFILE ||= 'default';
    my $gmod_conf = Bio::GMOD::Config->new() ;
    my $db_conf = Bio::GMOD::DB::Config->new( $gmod_conf, $DBPROFILE ) ;
    
    $dbhost ||= $db_conf->host();
    $dbname ||= $db_conf->name();
    $driver = $db_conf->driver();
    

    $port= $db_conf->port();
    
    $user= $db_conf->user();
    $pass= $db_conf->password();
}

if (!$dbhost && !$dbname) { die "Need -D dbname and -H hostname arguments.\n"; }
if (!$driver) { die "Need -d (dsn) driver, or provide one in -g gmod_conf\n"; }
if (!$user) { die "Need -u user_name, or provide one in -g gmod_conf\n"; }
#if (!$pass) { die "Need -p password, or provide one in -g gmod_conf\n"; }

my $dsn = "dbi:$driver:dbname=$dbname";
$dsn .= ";host=$dbhost";
$dsn .= ";port=$port";

$schema= Bio::Chado::Schema->connect($dsn, $user, $pass||'', { AutoCommit=>0 });

$dbh=$schema->storage->dbh();

if (!$schema || !$dbh) { die "No schema or dbh is avaiable! \n"; }

print STDOUT "Connected to database $dbname on host $dbhost.\n";
#####################################################################################################

my $sth;

my %seq  = (
    db         => 'db_db_id_seq',
    dbxref     => 'dbxref_dbxref_id_seq',
    pub        => 'pub_pub_id_seq',
    pub_dbxref => 'pub_dbxref_pub_dbxref_id_seq',
    pubauthor  => 'pubauthor_pubauthor_id_seq',
    pubprop    => 'pubprop_pubprop_id_seq',
    cv         => 'cv_cv_id_seq',
    cvterm     => 'cvterm_cvterm_id_seq',
    );

open (INFILE, "<$infile") || die "can't open file $infile";   #
open (ERR, ">$infile.err") || die "Can't open the error ($infile.err) file for writing.\n";
my $exists_count=0;
my $pubmed_count=0;

my %maxval=();

eval {
    
    #Fetch last database ids of relevant tables for resetting in case of rollback
  
    foreach my $key( keys %seq) {
	my $id_column= $key . "_id";
	my $table =  $key;
	my $query = "SELECT max($id_column) FROM $table";
	$sth=$dbh->prepare($query);
	$sth->execute();
	my ($next) = $sth->fetchrow_array();
	$maxval{$key}= $next;
    }

    #db name for pubmed ids
    my $db= $schema->resultset("General::Db")->find_or_create(
	{ name => 'PMID' } ); 
    my $db_id = $db->get_column('db_id');

    #cvterm_name for 'journal' . Currently this software does not support other types. All PubMed publications are stored with this default 'journal' type_id
    my $journal_cvterm = $schema->resultset('Cv::Cvterm')->create_with(
	{name=>'journal', cv=>'publication'});
    
    while (my $line = <INFILE>) {
	
	$publication = undef;
	
	chomp $line;
	my $pmid;
	if ( $line=~ m/(\[PMID: )(\d+)(.*)/ ) { 
	    $pmid= $2; 
	}else { $pmid=$line;}
	if (!$pmid) { next(); }
	
	#add a dbxref
	my $dbxref = $schema->resultset("General::Dbxref")->find_or_create(
	    { accession => $pmid,
	      db_id => $db_id,
	    });
	
	##
	# new Bio::Chado::Schema::Pub::Pub object
	my $pub_dbxref = $dbxref->find_related ('pub_dbxrefs', {}, { key=> 'pub_dbxref_c1' }, );
	$publication= $pub_dbxref->find_related('pub', {}, { key=> 'pub_c1'}, ) if $pub_dbxref;
	
	if(!($publication)) { #publication does not exist in our database
	    $pubmed_count++;
	    $publication = $schema->resultset('Pub::Pub')->new( {} ) ; 
	    
	    my $message= fetch_pubmed($pmid);
	    
	    if ($message) { message($message,1); }
	    print STDOUT "storing new publication. pubmed id = $pmid\n";
	    ####
	    #extract the abstract and uniquename assigned in Pubmed.pm
	    my $abstract = $publication->uniquename();
	    print STDOUT "The abstract is $abstract \n\n\n";
	    my (@authors) = split /-----/ , $publication->title();
	    my $title = shift(@authors);
	    print STDOUT "The title is $title\n\n";
	    $publication->title($title) ;
	    
	    
            #remove the abstract from the uniquename field
	    $publication->set_column(uniquename => $pmid . ":" . $title ) ;
	    $publication->type_id( $journal_cvterm->cvterm_id() );
	    
	    #store the publication in the pub table
	    $publication->insert();
	    
	    #store a pubprop for the abstract
	    #
	    my $pubprop = create_pubprops($publication, { 'abstract'=>$abstract }, { autocreate => 1 } );
	    
	    ## Add pub_dbxref
	    $publication->find_or_create_related('pub_dbxrefs' ,
						 { dbxref_id   => $dbxref->dbxref_id } );
	    ##
	    
	    ##Add the authors
	    my $rank =1;
	    foreach (@authors) {
		my ($surname, $givennames)= split  /\|/, $_;
		print STDOUT "Author: Surname=$surname, givennames = $givennames \n";
		
		$surname =~ s/^\s+|\s+$//g;
		$givennames =~ s/^\s+|\s+$//g;
		$publication->find_or_create_related( 'pubauthors' ,
						      {
							  surname => $surname,
							  givennames => $givennames,
							  rank => $rank++,
						      }
		    );
	    }
	    
	    #publication exists, do nothing
	}else  {  
	    $exists_count++;
	    print STDOUT "Publication $pmid is already stored in the database. Skipping..\n";
	}
    }
};


if($@) {
    print $@;
    print"Failed; rolling back.\n";
   
    foreach my $key ( keys %seq ) { 
	my $value= $seq{$key};
	my $maxvalue= $maxval{$key} || 0;
	if ($maxvalue) { $dbh->do("SELECT setval ('$value', $maxvalue, true)") ;  }
	else {  $dbh->do("SELECT setval ('$value', 1, false)");  }
    }
    $dbh->rollback();
}else{ 
    print"Succeeded.\n";
    print "Inserted $pubmed_count new publications!\n";
    print "$exists_count publication already exist in the database\n";
    
    if($opt_t) {
        print STDOUT "Rolling back!\n";
	foreach my $key ( keys %seq ) { 
	    my $value= $seq{$key};
	    my $maxvalue= $maxval{$key} || 0;
	    
	    if ($maxvalue) { $dbh->do("SELECT setval ('$value', $maxvalue, true)") ;  }
	    else {  $dbh->do("SELECT setval ('$value', 1, false)");  }
	}
	$dbh->rollback();
    }else {
        print STDOUT "Committing...\n";
        $dbh->commit();
    }
}

close ERR;
close INFILE;


sub message {
    my $message=shift;
    my $err=shift;
    if ($opt_v) {
	print STDOUT $message. "\n";
    }
    print ERR "$message \n" if $err;
}



sub sanitize {
    my $string = shift;
    $string =~ s/^\s+//; #remove leading spaces
    $string =~ s/\s+$//; #remove trailing spaces
    return $string;
}


sub create_pubprops {
    my ($self, $props, $opts) = @_;
    
    # process opts
    $opts ||= {};
    $opts->{cv_name} = 'publication'
	unless defined $opts->{cv_name};
    
    return Bio::Chado::Schema::Util->create_properties
	( properties => $props,
	  options    => $opts,
	  row        => $self,
	  prop_relation_name => 'pubprops',
	);
}

sub reset_sequences {
    my %seq=@_;
    my %maxval=@_;
    #reset sequences
    foreach my $key ( keys %seq ) { 
	my $value= $seq{$key};
	my $maxvalue= $maxval{$key} || 0;
	#print STDERR "$key: $value, $maxvalue \n";
	if ($maxvalue) { $dbh->do("SELECT setval ('$value', $maxvalue, true)") ;  }
	else {  $dbh->do("SELECT setval ('$value', 1, false)");  }
    }
}


sub fetch_pubmed {
    
    my $accession=shift;
    my $pub_xml = get("http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&id=$accession&rettype=xml&retmode=text");
    
    eval {
	my $twig=XML::Twig->new(
	    twig_roots   => 
	    {
		'Article/ArticleTitle'    => \&title,
		'JournalIssue/Volume'     => \&volume,
		'JournalIssue/Issue'      => \&issue,
		'DateCompleted/Year'      => \&pyear,
		'PubDate/Year'            => \&pyear,
		'Pagination/MedlinePgn'   => \&pages,
		'Journal/Title'           => \&journal_name,
		#'PublicationTypeList/PublicationType'  => \&pub_type,
		'Abstract/AbstractText'   => \&abstract,
		Author       => \&author, 
	    },
	    twig_handlers =>
	    { },
	    pretty_print => 'indented',  # output will be nicely formatted
	    ); 
	
	$twig->parse($pub_xml ); # build it
	
    };
    if($@) {
	my $message= "Error in transaction or NCBI server seems to be down. Please check your input for accession $accession or try again later.\n $@";
	return $message;
    }else { return undef ; }
    
}

##########################################
#Functions for parsing the XML
##########################################

sub title {
    
    my ($twig, $elt)= @_;
    $publication->title($elt->text) ;
    $twig->purge;
}



sub volume {
    my ($twig, $elt)= @_;
    $publication->volume($elt->text) ;
    $twig->purge;
}


sub issue {
    my ($twig, $elt)= @_;
    $publication->issue($elt->text) ;

    $twig->purge;
}


sub pyear {
    my ($twig, $elt)= @_;
    my $pyear = $elt->text;
    $publication->pyear($pyear);

    $twig->purge;
}


sub pages {
    my ($twig, $elt)= @_;
    $publication->pages($elt->text) ;

    $twig->purge;
}



sub journal_name {
    my ($twig, $elt)= @_;
    $publication->series_name($elt->text) ;

    $twig->purge;
}

sub abstract {
    my ($twig, $elt)= @_;
    $publication->uniquename($elt->text) ;

    $twig->purge
}

sub author {
    my ($twig, $elt)= @_;
    
    my $lastname=$elt->children_text('LastName');
    my $initials=$elt->children_text('Initials');  
    #sometimes the firstname has no initials but full first name 'ForName'..
  
    if (!$initials) {  $initials=$elt->children_text('ForeName') || $elt->children_text('FirstName') ; }
    
    
    my $author_data=  $lastname ."|" . $initials ; 
    
    #append the authors to the 'title' field. 
    #Then extract the list and store in pubprop
    $publication->title($publication->title() . "-----" . $author_data) ;
    
    $twig->purge
}