my %opts; # arg options
my $file_type; # tab, csv, gaf
+my $file_del; # data delimeter
my $input_file;
my $output_file;
my $verbose = 0; # flag for verbose output
}
}
- # split data on either commas or tabs, if file type is generic
- if ($file_type = "csv" || $file_type = "tab") {
- my $file_del = ($file_type eq "csv") ? ',' : '\t';
+ # split data on either commas or tabs
+ switch ($file_type) {
+ case "csv" { $file_del = ','; }
+ case "tab" { $file_del = '\t'; }
+ case "gaf" { $file_del = '\t'; }
+ else {
+ die(uc($file_type) . " is not a valid file type. Please supply "
+ . "a tab, csv, or gaf file.\n");
+ }
}
system "clear";
# open file
open(INPUT_FILE,$input_file) or die("Could not open input file.");
+ my $line; # all-purpose line counter
+
# read in the source data
my $count = 0;
while (<INPUT_FILE>)
{
$count++;
- my $line = $_;
+ $line = $_;
chomp $line;
my $data_val = (split('=',$line))[1];
switch ($count) {
case 4 { $source{'SourceVersion'} = $data_val; }
case 5 { $source{'SourceURI'} = $data_val; }
case 6 { $source{'SourceFile'} = $data_val; }
- case 8 { $template_name = $data_val; }
- case 9 { @field_names = split($file_del,$data_val); }
else {;}
}
- if ($count == 10) { last; }
+ if ($count == 6) { last; }
}
+ # read in "[Format] section if filetype is tab or csv"
+ $count++;
+ if ($file_type =~ /(csv)|(tab)/) {
+
+ $count = 0;
+ while (<INPUT_FILE>)
+ {
+ $count++;
+ $line = $_;
+ chomp $line;
+ my $data_val = (split('=',$line))[1];
+ switch ($count) {
+ case 2 { $template_name = $data_val; }
+ case 3 { @field_names = split($file_del,$data_val); }
+ else {;}
+ }
+ if ($count == 3) { last; }
+ }
+ }
+
print "Reading data...\n\n";
+ $line = <INPUT_FILE>; # skip "[Data]"
# loop through data rows and add all data fields to an array of hashes
while (<INPUT_FILE>)
{
- my $line = $_;
+ $line = $_;
chomp $line;
my @tmp_data_ary = split($file_del, $line);
# ---------------------------------------------------------------------------
sub show_input
{
- print "[Source]\n";
+ print "\n[Source]\n";
foreach my $key (keys %source) {
print "$key: $source{$key}\n";
}
print "\n";
- print "[Template]\n$template_name\n\n";
- print "[Fields]\n" . join(', ',@field_names) . "\n\n";
-
+
+ if ($file_type =~ /(csv)|(tab)/) {
+ print "[Template]\n$template_name\n\n";
+ print "[Fields]\n" . join(', ',@field_names) . "\n\n";
+ }
+
print "[Data]\n";
foreach my $row (@field_data) {
foreach my $key (keys %$row) {
print "\n";
}
-
-# loop through the hash and build annotation data and source xml doc
+# xml transformation for generic tab or CSV-templated data
+# (currently uses XML::Smart)
# ---------------------------------------------------------------------------
-sub write_xml
+sub transform_generic
{
- print "Transforming data...\n\n";
my $curr_node; # placeholder for general node cursor
my $curr_prov_node; # placeholder for node cursor in provenance pages
my $curr_annot_node; # placeholder for node cursor in annotation pages
$curr_annot_node = $curr_annot_node->{Template};
# set up next provenance page
- my $next_page = { Title => "Annotation:$next_page_title_id/Provenance" };
+ $next_page = { Title => "Annotation:$next_page_title_id/Provenance" };
push(@{$curr_node->{Page}}, $next_page);
$curr_prov_node = $curr_node->{Page}(
# write out xml doc to a single ImportXML file
print "Writing data to output file...\n\n";
$xml->save($output_file);
+ $output_data = $xml->data;
+}
+
+# xml transformation for GAF data
+# (currently uses XML::DOM)
+# ---------------------------------------------------------------------------
+sub transform_gaf
+{
+ $output_data = "hullo, gaf";
+}
+
+
+# loop through the hash and build annotation data and source xml doc
+# ---------------------------------------------------------------------------
+sub write_xml
+{
+ print "Transforming " . uc($file_type) . " data to SMW/SF XML...\n\n";
+
+ switch ($file_type) {
+ case ('csv' || 'tab') { transform_generic(); }
+ case 'gaf' { transform_gaf(); }
+ }
}
sub show_output
{
print "[XML]\n";
- $output_data = $xml->data;
print $output_data;
print "\n";
}
init;
import_data;
if ($verbose) { show_input; }
-write_xml;
+write_xml();
if ($verbose) { show_output; }
exit;