Next version of JABA
[jabaws.git] / binaries / linuxAMD64 / fasta34 / nr_to_sql.pl
1 #!/usr/bin/perl -w
2
3 use DBI;
4
5 $SIG{__WARN__} = sub { die @_ };
6
7 my $mysql = DBI->connect("DBI:mysql:database=seq_demo;user=seq_demo;password=demo_pass");
8
9 $mysql->do(q{LOCK TABLES prot WRITE,
10              annot WRITE,
11              sp WRITE });
12
13 my $EL = 125;
14 my $NA = 123;
15
16 my @aatrans = ($EL,$NA,$NA,$NA,$NA,$NA,$NA,$NA,$NA,$NA,$EL,$NA,$NA,$EL,$NA,$NA,
17                $NA,$NA,$NA,$NA,$NA,$NA,$NA,$NA,$NA,$NA,$NA,$NA,$NA,$NA,$NA,$NA,
18                $NA,$NA,$NA,$NA,$NA,$NA,$NA,$NA,$NA,$NA, 24,$NA,$NA,$NA,$NA,$NA,
19                $NA,$NA,$NA,$NA,$NA,$NA,$NA,$NA,$NA,$NA,$NA,$NA,$NA,$NA,$NA,$NA,
20                $NA,  1, 21,  5,  4,  7, 14,  8,  9, 10,$NA, 12, 11, 13,  3,$NA,
21                 15,  6,  2, 16, 17,$NA, 20, 18, 23, 19, 22,$NA,$NA,$NA,$NA,$NA,
22                $NA,  1, 21,  5,  4,  7, 14,  8,  9, 10,$NA, 12, 11, 13,  3,$NA,
23                 15,  6,  2, 16, 17,$NA, 20, 18, 23, 19, 22,$NA,$NA,$NA,$NA,$NA
24               );
25
26 my $ins_prot = $mysql->prepare(q{
27     INSERT INTO prot (seq,bin,len) VALUES (?, ?, ?)
28     });
29
30 my $ins_annot = $mysql->prepare(q{
31     INSERT INTO annot (gi, prot_id, db, descr) VALUES (?, ?, ?, ?)
32     });
33
34 my $ins_sp = $mysql->prepare(q{
35     INSERT INTO sp (gi, acc, name) VALUES (?, ?, ?)
36     });
37
38 use vars qw( $seq $bin $tot_seq $tot_annot $tot_sp );
39 use vars qw( $gi $prot_id $db $desc $sp_acc $sp_name );
40 use vars qw( $header $seq @entries );
41 use vars qw( $gi $db $db_acc $db_name $desc);
42
43 $tot_seq = $tot_annot = $tot_sp = 0;
44
45 for my $db_file ( @ARGV ) {
46     open(DATA, "<$db_file") or die $!;
47     local $/ = "\n>";
48     while (<DATA>) {
49         chomp; # remove trailing "\n>" record header
50         ($header, $seq) = $_ =~ m/^>?  # record separator (first entry)
51             ( [^\n]* ) \n  # header line
52                 (     .* )     # the sequence
53                     /osx; # optimize, multiline, commented
54         
55         $seq =~ s/\W|\d//sg;
56         $bin = pack('C*', map { $aatrans[unpack('C', $_)] } split(//, $seq));
57         $ins_prot->execute($seq,$bin,length($seq));
58         $prot_id = $ins_prot->{mysql_insertid};
59
60         $tot_seq++;
61
62 #       print STDERR "Inserted $prot_id: ". length($seq)."\n";
63
64         @entries = split(/\001/, $header);
65
66         for ( @entries ) {
67             ($gi,$db,$db_acc,$db_name,$desc)= 
68                 $_ =~ /^gi\|(\d+)\|([a-z]+)\|(\S*)\|(\S*) (.*)$/o;
69 #           print "$prot_id: $gi\t$db\t$db_acc\t$desc\n";
70             $ins_annot->execute($gi,$prot_id,$db,$desc);
71
72             $tot_annot++;
73
74             if ($db eq "sp") {
75                 $ins_sp->execute($gi,$db_acc,$db_name);
76                 $tot_sp++;
77             }
78         }
79     }
80     close(DATA);
81 }
82
83 print "Inserted $tot_seq sequences; $tot_annot annotations; $tot_sp swissprot\n";
84
85
86