<commit_before>#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Std;
use threads;
use Thread::Queue;
use Cwd;
use DBI;
use FindBin qw($Bin);
use Config::Tiny;
use Time::Local;
use POSIX qw(strftime);

my (%opt, $dir, $pipeline, $threads, $num,  @images, $image_dir, $sqldb, $type, %snapshots, %ids, $zoom_setting, $roi);
my %is_valid = (
  'vis_sv' => 1,
  'vis_tv' => 1,
  'nir_tv' => 1,
  'nir_sv' => 1,
  'flu_tv' => 1
);
getopts('d:p:t:n:i:s:T:z:m:rch', \%opt);
arg_check();

## Job start time
my $start_time = strftime("%Y-%m-%d_%H:%M:%S", localtime());

## Temporary file names
my $snapshot_tmp = $type.'_z'.$zoom_setting.'_snapshots.tab';
my $runinfo_tmp = $type.'_z'.$zoom_setting.'_runinfo.tab';
my $vis_shapes = $type.'_z'.$zoom_setting.'_vis_shapes.tab';
my $vis_colors = $type.'_z'.$zoom_setting.'_vis_colors.tab';
my $nir_shapes = $type.'_z'.$zoom_setting.'_nir_shapes.tab';
my $nir_signal = $type.'_z'.$zoom_setting.'_nir_signal.tab';
my $flu_shapes = $type.'_z'.$zoom_setting.'_flu_shapes.tab';
my $flu_signal = $type.'_z'.$zoom_setting.'_flu_signal.tab';
my $analysis_images = $type.'_z'.$zoom_setting.'_analysis_images.tab';
my $boundary_data = $type.'_z'.$zoom_setting.'_boundary_data.tab';

# Later connect to Bioinfo site to get version
our $outlier_vs = 0;

# Log files
my $skipped_log = 'plantcv_skipped_images_'.$type.'_z'.$zoom_setting.'_'.$start_time.'.log';
my $fail_log = 'plantcv_failed_images_'.$type.'_z'.$zoom_setting.'_'.$start_time.'.log';
open(SKIP, ">$skipped_log") or die "Cannot open file $skipped_log: $!\n\n";
open(FAIL, ">$fail_log") or die "Cannot open file $fail_log: $!\n\n";

# Open temporary database files
open(SNAP, ">$snapshot_tmp") or die "Cannot open file $snapshot_tmp: $!\n\n";
open(RUN, ">$runinfo_tmp") or die "Cannot open file $runinfo_tmp: $!\n\n";
open(IMG, ">$analysis_images") or die "Cannot open file $analysis_images: $!\n\n";
open(BOUND, ">$boundary_data") or die "Cannot open file $boundary_data: $!\n\n";

if ($type =~ /vis/) {
  open(SHAPE, ">$vis_shapes") or die "Cannot open file $vis_shapes: $!\n\n";
  open(SIG, ">$vis_colors") or die "Cannot open file $vis_colors: $!\n\n";
} elsif ($type =~ /nir/) {
  open(SHAPE, ">$nir_shapes") or die "Cannot open file $nir_shapes: $!\n\n";
  open(SIG, ">$nir_signal") or die "Cannot open file $nir_signal: $!\n\n";
} elsif ($type =~ /flu/) {
  open(SHAPE, ">$flu_shapes") or die "Cannot open file $flu_shapes: $!\n\n";
  open(SIG, ">$flu_signal") or die "Cannot open file $flu_signal: $!\n\n";
}

################################################################################
# Begin main
################################################################################

# Database setup
###########################################
if ($opt{'c'}) {
  if (-e $sqldb) {
    print STDERR "WARNING: SQLite database file $sqldb already exists are you sure you want to delete it? (y/n) ";
    my $response = <STDIN>;
    chomp $response;
    if ($response =~ /^y/i) {
      unlink($sqldb);
    } else {
      print STDERR "Okay, stopping\n\n";
      exit;
    }
  }
  my $schema = "$Bin/docs/results.sql";
  unless (-e $schema) {
    print STDERR "Schema file not found: $schema does not exists!\n\n";
    exit 1;
  }
  # Create new database and initialize with template schema
  `sqlite3 $sqldb '.read $Bin/docs/results.sql'`;
}

# Connect to the SQLite database
my $dbh = DBI->connect("dbi:SQLite:dbname=$sqldb","","");
###########################################

# Get last image, snapshot, and run entries
###########################################
foreach my $field ('image_id', 'snapshot_id') {
  my $sth = $dbh->prepare("SELECT MAX(`$field`) as max FROM `snapshots`");
  $sth->execute();
  while (my $result = $sth->fetchrow_hashref) {
    $ids{$field} = $result->{'max'};
  }
  if (!exists($ids{$field})) {
    $ids{$field} = 0;
  }
}

my $sth = $dbh->prepare("SELECT MAX(`run_id`) as max FROM `runinfo`");
$sth->execute();
while (my $result = $sth->fetchrow_hashref) {
  $ids{'run_id'} = $result->{'max'};
}
if (!exists($ids{'run_id'})) {
  $ids{'run_id'} = 0;<commit_msg>Added a check to make sure the output database exists if a create request is not made<commit_after>#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Std;
use threads;
use Thread::Queue;
use Cwd;
use DBI;
use FindBin qw($Bin);
use Config::Tiny;
use Time::Local;
use POSIX qw(strftime);

my (%opt, $dir, $pipeline, $threads, $num,  @images, $image_dir, $sqldb, $type, %snapshots, %ids, $zoom_setting, $roi);
my %is_valid = (
  'vis_sv' => 1,
  'vis_tv' => 1,
  'nir_tv' => 1,
  'nir_sv' => 1,
  'flu_tv' => 1
);
getopts('d:p:t:n:i:s:T:z:m:rch', \%opt);
arg_check();

## Job start time
my $start_time = strftime("%Y-%m-%d_%H:%M:%S", localtime());

## Temporary file names
my $snapshot_tmp = $type.'_z'.$zoom_setting.'_snapshots.tab';
my $runinfo_tmp = $type.'_z'.$zoom_setting.'_runinfo.tab';
my $vis_shapes = $type.'_z'.$zoom_setting.'_vis_shapes.tab';
my $vis_colors = $type.'_z'.$zoom_setting.'_vis_colors.tab';
my $nir_shapes = $type.'_z'.$zoom_setting.'_nir_shapes.tab';
my $nir_signal = $type.'_z'.$zoom_setting.'_nir_signal.tab';
my $flu_shapes = $type.'_z'.$zoom_setting.'_flu_shapes.tab';
my $flu_signal = $type.'_z'.$zoom_setting.'_flu_signal.tab';
my $analysis_images = $type.'_z'.$zoom_setting.'_analysis_images.tab';
my $boundary_data = $type.'_z'.$zoom_setting.'_boundary_data.tab';

# Later connect to Bioinfo site to get version
our $outlier_vs = 0;

# Log files
my $skipped_log = 'plantcv_skipped_images_'.$type.'_z'.$zoom_setting.'_'.$start_time.'.log';
my $fail_log = 'plantcv_failed_images_'.$type.'_z'.$zoom_setting.'_'.$start_time.'.log';
open(SKIP, ">$skipped_log") or die "Cannot open file $skipped_log: $!\n\n";
open(FAIL, ">$fail_log") or die "Cannot open file $fail_log: $!\n\n";

# Open temporary database files
open(SNAP, ">$snapshot_tmp") or die "Cannot open file $snapshot_tmp: $!\n\n";
open(RUN, ">$runinfo_tmp") or die "Cannot open file $runinfo_tmp: $!\n\n";
open(IMG, ">$analysis_images") or die "Cannot open file $analysis_images: $!\n\n";
open(BOUND, ">$boundary_data") or die "Cannot open file $boundary_data: $!\n\n";

if ($type =~ /vis/) {
  open(SHAPE, ">$vis_shapes") or die "Cannot open file $vis_shapes: $!\n\n";
  open(SIG, ">$vis_colors") or die "Cannot open file $vis_colors: $!\n\n";
} elsif ($type =~ /nir/) {
  open(SHAPE, ">$nir_shapes") or die "Cannot open file $nir_shapes: $!\n\n";
  open(SIG, ">$nir_signal") or die "Cannot open file $nir_signal: $!\n\n";
} elsif ($type =~ /flu/) {
  open(SHAPE, ">$flu_shapes") or die "Cannot open file $flu_shapes: $!\n\n";
  open(SIG, ">$flu_signal") or die "Cannot open file $flu_signal: $!\n\n";
}

################################################################################
# Begin main
################################################################################

# Database setup
###########################################
if ($opt{'c'}) {
  if (-e $sqldb) {
    print STDERR "WARNING: SQLite database file $sqldb already exists are you sure you want to delete it? (y/n) ";
    my $response = <STDIN>;
    chomp $response;
    if ($response =~ /^y/i) {
      unlink($sqldb);
    } else {
      print STDERR "Okay, stopping\n\n";
      exit;
    }
  }
  my $schema = "$Bin/docs/results.sql";
  unless (-e $schema) {
    print STDERR "Schema file not found: $schema does not exists!\n\n";
    exit 1;
  }
  # Create new database and initialize with template schema
  `sqlite3 $sqldb '.read $Bin/docs/results.sql'`;
} else {
	unless (-e $sqldb) {
		arg_error("The database $sqldb does not exist and you did not ask to create it [-c].");
		exit 1;
	}
}

# Connect to the SQLite database
my $dbh = DBI->connect("dbi:SQLite:dbname=$sqldb","","");
###########################################

# Get last image, snapshot, and run entries
###########################################
foreach my $field ('image_id', 'snapshot_id') {
  my $sth = $dbh->prepare("SELECT MAX(`$field`) as max FROM `snapshots`");
  $sth->execute();
  while (my $result = $sth->fetchrow_hashref) {
    $ids{$field} = $result->{'max'};
  }
  if (!exists($ids{$field})) {
    $ids{$field} = 0;
  }
}

my $sth = $dbh->prepare("SELECT MAX(`run_id`) as max FROM `runinfo`");
$sth->execute();
while (my $result = $sth->fetchrow_hashref) {
  $ids{'run_id'} = $result->{'max'};
}
if (!exists($ids{'run_id'})) {
  $ids{'run_id'} = 0;
{'fraction_non_alphanumeric': 0.25709177592371873, 'fraction_numerical': 0.002502979737783075, 'mean_word_length': 4.955287437899219, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '718982', 'n_tokens_mistral': 3195, 'n_tokens_neox': 3155, 'n_words': 788}