#!/opt/anaconda1anaconda2anaconda3/bin/perl 

use strict;
use warnings;
use FindBin;

use Fasta_reader;
use DBI;
use Sqlite_connect;
use Getopt::Long qw(:config no_ignore_case bundling pass_through);


my $usage = "usage: $0 sqlite.db transcripts.fasta\n\n";

my $sqlite_db = $ARGV[0] or die $usage;
my $transcripts_fasta = $ARGV[1] or die $usage;
 

main: {

    my $dbproc = &connect_to_db($sqlite_db);

    &AutoCommit($dbproc, 0);
    
    my $fasta_reader = new Fasta_reader($transcripts_fasta);

    my $counter = 0;
    while (my $seq_obj = $fasta_reader->next()) {
        $counter++;
        
        my $acc = $seq_obj->get_accession();
        my $sequence = $seq_obj->get_sequence();

        my $query = "update Transcript set annotation = ?, sequence = ? where transcript_id = ?";
        &RunMod($dbproc, $query, $acc, $sequence, $acc);

        if ($counter % 1000 == 0) {
            $dbproc->commit;
        }
        print STDERR "\r[$counter]    ";
    }

    $dbproc->commit;

    $dbproc->disconnect;

    print STDERR "\n\nDone.\n\n";
    
    exit(0);
    
}
