使用exists运算符避免DBI重复主键

时间:2016-11-28 23:50:26

标签: perl hash exists perl-data-structures

我想将FASTA文件中的信息插入MySQL数据库中的表中。我使用Ensembl_id列作为主键。

我的一些Ensembl_id并不是唯一的,因此我尝试使用exists运算符来克服此问题。但是,表中只插入了5行,其中只有一行具有重复的Ensembl_id值。

#!/usr/bin/perl -w

#usage script.pl <username> <password> <database_name> <mouse_genes> <mouse_transcripts>

use DBI;
use Data::Dumper;

my $user              = shift @ARGV or die $!;
my $password          = shift @ARGV or die $!;
my $database          = shift @ARGV or die $!;
my $mouse_genes       = shift @ARGV or die $!;
my $mouse_transcripts = shift @ARGV or die $!;

my $dbh = DBI->connect( "dbi:mysql:$database:localhost", "$user", "$password",
    { RaiseError => 1 } );
my %gene;

$/ = "\n>";

open( FILE, "gzip -d -c /data.dash/class2016/student/Mus_musculus.GRCm38.cdna.all.fa.gz |" )
        or die $!;

LOOP:
while ( <FILE> ) {

    my $line = $_;
    chomp $line;

    if ( $line =~ /[a-z]/ ) {

        my @array = split( "\t", $line );

        if ( m/gene:(\w+\d+\.\w+)/ ) {

            my $Ensembl_id = $1;

            if ( !exists $gene{$Ensembl_id} ) {
                $gene{$Ensembl_id} = 1;
            }
            else {
                next;
            }

            if ( m/gene_biotype:(\w+)/ ) {

                my $gene_biotype = $1;

                if ( m/gene_symbol:(\w+\D\d+)/ ) {

                    my $gene_symbol = $1;

                    if ( m/description:(\w+\s+\w+\s+\w+\s+)/ ) {

                        my $gene_description = $1;

                        if ( m/MGI:(\d+)/ ) {

                            my $MGI_accession = $1;
                            my $sth           = $dbh->prepare(
                                qq{insert into $mouse_genes (Ensembl_id,gene_biotype,gene_symbol,gene_description,MGI_accession) values ("$Ensembl_id","$gene_biotype","$gene_symbol","$gene_description","$MGI_accession")}
                            );
                            $sth->execute();
                            $sth->finish();

                            next LOOP;
                        }
                    }
                }
            }
        }
    }
}

close FILE;

$dbh->disconnect();

如果主键exists重复,我如何使用$Ensembl_id运算符移动到文件的下一行?

1 个答案:

答案 0 :(得分:1)

我以为我看到了一个与此非常相似的问题,但我找不到它

解决方案是忘记哈希并使用IGNORE关键字来避免引发错误。 MySQL documentation说明了这一点:

  

如果使用IGNORE关键字,则会忽略执行INSERT语句时发生的错误。例如,如果没有IGNORE,则复制表中现有UNIQUE索引或PRIMARY KEY值的行会导致重复键错误,并且语句将中止。使用IGNORE,该行将被丢弃并且不会发生错误。虽然重复键错误没有,但忽略错误可能会生成警告。

您还应该在SQL语句中使用占位符,因此它应该如下所示

请注意,END_SQL必须在其前后不显示空格。您可能希望在程序顶部定义SQL语句,以避免损坏缩进

my $sth = $dbh->prepare(<<END_SQL);
INSERT IGNORE INTO $mouse_genes (
    Ensembl_id,
    gene_biotype,
    gene_symbol,
    gene_description,
    MGI_accession
)
VALUES ( ?, ?, ?, ?, ? )
END_SQL

$sth->execute($Ensembl_id, $gene_biotype, $gene_symbol, $gene_description, $MGI_accession);


更新

您的程序可以整理很多,以便于阅读。这就是我写它的方式

#!/usr/bin/perl

use strict;
use warnings 'all';

# usage script.pl <username> <password> <database_name> <mouse_genes> <mouse_transcripts>

use DBI;

my $user              = shift @ARGV or die $!;
my $password          = shift @ARGV or die $!;
my $database          = shift @ARGV or die $!;
my $mouse_genes       = shift @ARGV or die $!;
my $mouse_transcripts = shift @ARGV or die $!; # Not used at present

my $dbh = DBI->connect( "dbi:mysql:$database:localhost", $user, $password,
        { RaiseError => 1, PrintError => 0 } );

my $sth = $dbh->prepare( <<END_SQL );
INSERT IGNORE INTO $mouse_genes (
    Ensembl_id,
    gene_biotype,
    gene_symbol,
    gene_description,
    MGI_accession
)
VALUES ( ?, ?, ?, ?, ? )
END_SQL

my $cmd = 'gzip -d -c /data.dash/class2016/student/Mus_musculus.GRCm38.cdna.all.fa.gz';

open my $cmd_fh, '-|', $cmd or die $!;

$/ = "\n>";

while ( <$cmd_fh> ) {

    next unless my ( $ensembl_id )       = /gene:(\w+\d+\.\w+)/;
    next unless my ( $gene_biotype )     = /gene_biotype:(\w+)/;
    next unless my ( $gene_symbol )      = /gene_symbol:(\w+\D\d+)/;
    next unless my ( $gene_description ) = /description:(\w+\s+\w+\s+\w+)\s/;
    next unless my ( $mgi_accession )    = /MGI:(\d+)/;

    $sth->execute( $ensembl_id, $gene_biotype, $gene_symbol, $gene_description, $mgi_accession );
}

$dbh->disconnect;