]> git.donarmstrong.com Git - dbsnp.git/blob - utils/load_gcrma_data.pl
add gcrma data and affymetrix probe annotation loaders
[dbsnp.git] / utils / load_gcrma_data.pl
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 use DBI;
7
8
9 my $dbh = DBI->connect("dbi:Pg:service=snp",'','',{AutoCommit => 1}) or
10     die "Unable to connect to database: ".$DBI::errstr;
11
12 my %sth;
13 $sth{insert_sample} = $dbh->prepare(<<'END') // die "Unable to prepare insert sample statement: ".$dbh->errstr;
14 INSERT INTO gcrma_samples (tissue) VALUES (?);
15 END
16
17 $sth{select_sample} = $dbh->prepare(<<'END') // die "Unable to prepare select sample statement: ".$dbh->errstr;
18 SELECT * FROM gcrma_samples WHERE tissue = ?;
19 END
20
21 $sth{insert_probe} = $dbh->prepare(<<'END') // die "Unable to prepare insert probe statement: ".$dbh->errstr;
22 INSERT INTO affy_probe (probe) VALUES (?);
23 END
24
25 $sth{select_probe} = $dbh->prepare(<<'END') // die "Unable to prepare select probe statement: ".$dbh->errstr;
26 SELECT * FROM affy_probe WHERE probe = ?;
27 END
28
29 $sth{insert_reading} = $dbh->prepare(<<'END') // die "Unable to prepare insert reading statement: ".$dbh->errstr;
30 INSERT INTO gcrma_expression (probe,sample,expression) VALUES (?,?,?);
31 END
32
33
34
35 my @samples;
36
37 use Term::ProgressBar;
38 use Fcntl qw(:seek);
39
40 my @ifh;
41 for my $ifn (@ARGV) {
42     my $ifh = IO::File->new($ifn,'r') or
43         die "Unable to open $ifn for reading: $!";
44     push @ifh,$ifh;
45 }
46
47 # read avg.csv file
48 for my $ifh (@ifh) {
49     my $p;
50     if ($ifh->seek(0,SEEK_END)) {
51         $p = Term::ProgressBar->new({count => $ifh->tell,
52                                      remove => 1,
53                                      ETA=>'linear'});
54         $ifh->seek(0,SEEK_SET);
55     }
56     while (<$ifh>) {
57         chomp;
58         my @line = split /\s*,\s*/;
59         if (not @samples) {
60             shift @line;
61             for my $sample (@line) {
62                 push @samples, insert_sample($dbh,\%sth,$sample);
63             }
64             next;
65         }
66         my $probe = insert_probe($dbh,\%sth,shift @line);
67         $dbh->do('COPY gcrma_expression (probe,sample,expression) FROM STDIN;');
68         for (0..$#line) {
69             $dbh->pg_putcopydata("$probe\t$samples[$_]\t$line[$_]\n");
70         }
71         $dbh->pg_putcopyend();
72         if (defined $p) {
73             $p->update($ifh->tell);
74         }
75     }
76 }
77
78 sub insert_sample{
79     my ($dbh,$sth,$sample) = @_;
80     my $rv = $sth->{insert_sample}->execute($sample) //
81         die "Unable to execute statement properly: ".$dbh->errstr;
82     $sth->{insert_sample}->finish;
83     return select_sample(@_);
84 }
85
86 sub select_sample {
87     my ($dbh,$sth,$sample) = @_;
88     my $rv = $sth->{select_sample}->execute($sample) //
89         die "Unable to execute statement properly: ".$dbh->errstr;
90     my ($sample_id) = map {ref $_ ?@{$_}:()}
91         map {ref $_ ?@{$_}:()} $sth->{select_sample}->fetchall_arrayref([0]);
92     $sth->{select_sample}->finish;
93     return $sample_id;
94 }
95
96 sub insert_probe{
97     my ($dbh,$sth,$probe) = @_;
98     my $rv = $sth->{insert_probe}->execute($probe) //
99         die "Unable to execute statement properly: ".$dbh->errstr;
100     $sth->{insert_probe}->finish;
101     return select_probe(@_);
102 }
103
104 sub select_probe {
105     my ($dbh,$sth,$probe) = @_;
106     my $rv = $sth->{select_probe}->execute($probe) //
107         die "Unable to execute statement properly: ".$dbh->errstr;
108     my ($probe_id) = map {ref $_ ?@{$_}:()}
109         map {ref $_ ?@{$_}:()} $sth->{select_probe}->fetchall_arrayref([0]);
110     $sth->{select_probe}->finish;
111     return $probe_id;
112 }
113
114 sub insert_reading{
115     my ($dbh,$sth,$probe,$sample,$reading) = @_;
116     my $rv = $sth->{insert_reading}->execute($probe,$sample,$reading) //
117         die "Unable to execute statement properly: ".$dbh->errstr;
118     $sth->{insert_reading}->finish;
119 }
120