Index: trunk/ippScripts/scripts/lap_science.pl
===================================================================
--- trunk/ippScripts/scripts/lap_science.pl	(revision 31678)
+++ trunk/ippScripts/scripts/lap_science.pl	(revision 31795)
@@ -85,5 +85,5 @@
     unless (($lapRunInfo{state} eq 'full')||
 	    ($lapRunInfo{state} eq 'drop')) {
-	&my_die("Cannot run cleanup_mode if lapRun != done!", $lap_id);
+	&my_die("Cannot run cleanup_mode if lapRun != full!", $lap_id);
     }
     my $status = cleanup_mode($lap_id);
@@ -456,4 +456,5 @@
     my $can_fstack = 0;
     my $total_exposures = 0;
+    my $lonely_exposures = 0;
     foreach my $exposure (@$exposures) {
 	$total_exposures++;
@@ -474,4 +475,5 @@
 	if ($exposure->{private}) { # I've declared this exposure private to this lapRun.
 	    $needs_qstack = 1;
+#	    $lonely_exposures++;
 	}
 	
@@ -516,4 +518,5 @@
 		    $companion->{pairwise} = 0;
 		    &update_this_exposure($companion);
+#		    $lonely_exposures++;
 		}
 		$exposure->{private} = 1;
@@ -897,7 +900,7 @@
 
     my @clean_modes = (
-	'chiptool -dbname @DBNAME@ -updaterun -set_state goto_cleaned -state full -set_label goto_cleaned -label @LABEL@ -chip_id @CHIP_ID@',
-	'warptool -dbname @DBNAME@ -updaterun -set_state goto_cleaned -state full -set_label goto_cleaned -label @LABEL@ -warp_id @WARP_ID@',
-	'difftool -dbname @DBNAME@ -updaterun -set_state goto_cleaned -state full -set_label goto_cleaned -label @LABEL@ -diff_id @DIFF_ID@',
+	'chiptool -dbname @DBNAME@ -updaterun -set_state goto_cleaned -set_label goto_cleaned -label @LABEL@ -chip_id @CHIP_ID@',
+	'warptool -dbname @DBNAME@ -updaterun -set_state goto_cleaned -set_label goto_cleaned -label @LABEL@ -warp_id @WARP_ID@',
+	'difftool -dbname @DBNAME@ -updaterun -set_state goto_cleaned -set_label goto_cleaned -label @LABEL@ -diff_id @DIFF_ID@',
 	'magicdstool -dbname @DBNAME@ -updaterun -set_state goto_cleaned -state full -set_label goto_cleaned -stage chip -stage_id @CHIP_ID@',
 	'magicdstool -dbname @DBNAME@ -updaterun -set_state goto_cleaned -state full -set_label goto_cleaned -stage warp -stage_id @WARP_ID@',
@@ -910,7 +913,28 @@
  	    my $command = $clean_mode;
 	    $command =~ s/\@DBNAME\@/$dbname/g;
-	    $command =~ s/\@CHIP_ID\@/$exposure->{chip_id}/;
-	    $command =~ s/\@WARP_ID\@/$exposure->{warp_id}/;
-	    $command =~ s/\@DIFF_ID\@/$exposure->{diff_id}/;
+	    if ($command =~ /\@CHIP_ID\@/) {
+		if (S64_IS_NOT_NULL($exposure->{chip_id})) {
+		    $command =~ s/\@CHIP_ID\@/$exposure->{chip_id}/;
+		}
+		else {
+		    next;
+		}
+	    }
+	    if ($command =~ /\@WARP_ID\@/) {
+		if (S64_IS_NOT_NULL($exposure->{warp_id})) {
+		    $command =~ s/\@WARP_ID\@/$exposure->{warp_id}/;
+		}
+		else {
+		    next;
+		}
+	    }
+	    if ($command =~ /\@DIFF_ID\@/) {
+		if (S64_IS_NOT_NULL($exposure->{diff_id})) {
+		    $command =~ s/\@DIFF_ID\@/$exposure->{diff_id}/;
+		}
+		else {
+		    next;
+		}
+	    }
 	    $command =~ s/\@LABEL\@/$exposure->{label}/;
 	    
@@ -925,4 +949,55 @@
  	update_this_exposure($exposure);
     }
+    
+    # Cleanup quickstacks.
+    $command = "$laptool -stacks -lap_id $lap_id";
+    $command .= " -dbname $dbname" if defined $dbname;
+    
+    ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run(command => $command, verbose => $verbose);
+    unless ($success) {
+	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	&my_die("Unable to perform laptool -stacks: $error_code", "none", "none");
+    }
+    if (@$stdout_buf == 0) {
+	# Nothing to do.
+	return(0);
+    }
+    
+    my $stacks = $mdcParser->parse_list(join "", @$stdout_buf) or
+	&my_die("Unable to parse metadata from laptool -inactiveexp", $lap_id);
+    
+
+    @clean_modes = (
+	'stacktool -dbname @DBNAME@ -updaterun -set_state goto_cleaned -set_label goto_cleaned -label @LABEL@ -stack_id @STACK_ID@');
+    foreach my $stack (@$stacks) {
+	if (!S64_IS_NOT_NULL($stack->{quick_stack_id})) {
+	    next;
+	}
+	foreach my $clean_mode (@clean_modes) {
+	    my $command = $clean_mode;
+	    $command =~ s/\@DBNAME\@/$dbname/g;
+	    $command =~ s/\@LABEL\@/$stack->{label}/;
+	    $command =~ s/\@STACK_ID\@/$stack->{quick_stack_id}/;
+
+	    ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+		run(command => $command, verbose => $verbose);
+	    unless ($success) {
+		$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+		&my_die("Unable to perform cleantool: $command : $error_code", "none", "none");
+	    }
+	}
+    }    
+
+    $command = "$laptool -updaterun -lap_id $lap_id";
+    $command .= " -dbname $dbname " if defined $dbname;
+    $command .= " -set_state done ";
+    ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run(command => $command, verbose => $verbose);
+    unless ($success) {
+	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	&my_die("Unable to perform laptool -updaterun: $error_code", $lap_id);
+    }
+
 }
     
