src/hg/encode/encodeValidate/doEncodeValidate.pl 1.183

1.183 2009/06/18 00:18:14 tdreszer
Trying to get old metadata for new submissions
Index: src/hg/encode/encodeValidate/doEncodeValidate.pl
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/encode/encodeValidate/doEncodeValidate.pl,v
retrieving revision 1.182
retrieving revision 1.183
diff -b -B -U 4 -r1.182 -r1.183
--- src/hg/encode/encodeValidate/doEncodeValidate.pl	8 Jun 2009 18:23:21 -0000	1.182
+++ src/hg/encode/encodeValidate/doEncodeValidate.pl	18 Jun 2009 00:18:14 -0000	1.183
@@ -1575,11 +1575,8 @@
     $metadata .= ' dataVersion="' . $Encode::dataVersion .'"';
     if($submitDir =~ /(\d+)$/) {
         $metadata .= " subId=$1";
     }
-    my (undef, undef, undef, $rMDay, $rMon, $rYear) = Encode::restrictionDate($ddfFileTime); # Use DDF time
-    $metadata .= sprintf(" dateSubmitted=%04d-%02d-%02d", 1900 + $year, $mon + 1, $mday);
-    $metadata .= sprintf(" dateUnrestricted=%04d-%02d-%02d", 1900 + $rYear, $rMon + 1, $rMDay);
 
     HgAutomate::verbose(2, "  View: $view\n");
     my $replicate;
     if($hasReplicates && $daf->{TRACKS}{$view}{hasReplicates}) {
@@ -1590,14 +1587,14 @@
         }
     }
     # Construct table name from track name and variables
     my $tableName = "$compositeTrack";
-    if($Encode::dafVersion le "1.0") {
+    #if($Encode::dafVersion le "1.0") {
         $tableName .= $view;
         if(defined($replicate)) {
             $tableName .= "Rep$replicate";
         }
-    }
+    #}
     if(!defined($daf->{TRACKS}{$view}{shortLabelPrefix})) {
         $daf->{TRACKS}{$view}{shortLabelPrefix} = "";
     }
     my $shortLabel = defined($daf->{TRACKS}{$view}{shortLabelPrefix}) ? $daf->{TRACKS}{$view}{shortLabelPrefix} : "";
@@ -1677,21 +1674,68 @@
             $subGroups .= " $groupVar=$hash{$var}";
             $additional = "    $var $hash{$var}\n" . $additional;
         }
     }
-    if($Encode::dafVersion gt "1.0") {
-        $tableName .= "$view";
-        if(defined($replicate)) {
-            $tableName .= "Rep$replicate";
-        }
-    }
+    #if($Encode::dafVersion gt "1.0") {
+    #    $tableName .= "$view";
+    #    if(defined($replicate)) {
+    #        $tableName .= "Rep$replicate";
+    #    }
+    #}
 
     # mysql doesn't allow hyphens in table names and our naming convention doesn't allow underbars; to be
     # safe, we strip non-alphanumerics.
     $tableName =~ s/[^A-Za-z0-9]//g;
 
+    my (undef, undef, undef, $rMDay, $rMon, $rYear) = Encode::restrictionDate($ddfFileTime); # Use DDF time
+    my $dateSubmitted    = sprintf("%04d-%02d-%02d", 1900 + $year, $mon + 1, $mday);
+    my $dateUnrestricted = sprintf("%04d-%02d-%02d", 1900 + $rYear, $rMon + 1, $rMDay);
+
+
+    # dataVersion means the tableName must be different (append Vn), and the old metaddata should be used for dateSubmitted and dateUnrestricted
+    if(!defined($daf->{dataVersion}) || $daf->{dataVersion} > 1) {
+        my $prevTableName = "$tableName";
+        # Find old metadata to lookup dateSubmitted and dateUnrestricted
+        my $prevTableFound = 0;
+        for (my $preVer=$daf->{dataVersion} - 1; $preVer > 1; $preVer--) {
+            $prevTableFound = $db->quickQuery("select count(*) from trackDb where tableName = ?", $prevTableName . "V$preVer");
+            if($prevTableFound) {
+                $prevTableName .= "V$preVer";
+                last;
+            }
+        }
+        if($prevTableFound == 0) {
+            $prevTableFound = $db->quickQuery("select count(*) from trackDb where tableName = '$prevTableName'");
+        }
+        if($prevTableFound) {
+            my $oldSettings = $db->quickQuery("select settings from trackDb where tableName = '$prevTableName'");
+            if( $oldSettings ) {
+                $oldSettings =~ m/metadata (.*?)\n/;    # Is this throwing away all but the contents of the metadata line?
+                my ( $tagRef, $valRef ) = Encode::metadataLineToArrays($1);
+                my @tags = @{$tagRef};
+                my @vals = @{$valRef};
+                my $tix = 0;
+                while($tags[$tix]) {
+                    if($tags[$tix] eq "dateUnrestricted") {
+                        $dateUnrestricted = $vals[$tix];
+                    } elsif($tags[$tix] eq "dateSubmitted") {
+                        $metadata .= " dateResubmitted=$dateSubmitted";
+                        $dateSubmitted = $vals[$tix];
+                    }
+                    $tix++;
+                }
+            }
+        }
+        # Now finally complete the real tableName
+        $tableName = $tableName . "V" . $daf->{dataVersion};
+    }
+    # Delayed adding these terms to metadata so that resubmissions could have the looked up term
+    $metadata .= " dateSubmitted=$dateSubmitted";
+    $metadata .= " dateUnrestricted=$dateUnrestricted";
+
     die "Table name [$tableName] too long, must be <= 64 chars, got [".length($tableName)."]\n" if length($tableName) > 64;
 
+
     if($tableNamesUsed{$tableName}++) {
         dieTellWrangler("System Error: identical tableName '$tableName' was generated by multiple data sets\n");
     }