Skip to content

Commit 7b2f94a

Browse files
authored
Merge pull request #1397 from mskcc/feature/improve_chronos_output
Check if the bam is already registered in pipeline_output.csv
2 parents 6ea0d1d + d634fd7 commit 7b2f94a

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

runner/operator/chronos_outputs_operator/__init__.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import io
22
import os
3+
import shutil
34
import logging
45
import subprocess
56
from django.conf import settings
@@ -34,6 +35,7 @@ def get_jobs(self):
3435
raise Exception(f"Failed to copy bams for sample {ci_tag}")
3536
self.append_trace(run, destination_directory)
3637
self.append_bam_outputs(run, destination_directory)
38+
self.clean_up_source_directory(run)
3739
return []
3840

3941
def construct_metadata(self, run):
@@ -99,6 +101,10 @@ def append_trace(self, run, destination_directory):
99101
def append_bam_outputs(self, run, destination_directory):
100102
bam_outputs_path = os.path.join(run.output_directory, "pipeline_output.csv")
101103
bam_outputs_file_global = os.path.join(destination_directory, "pipeline_output.csv")
104+
# Read result bam file
105+
with open(bam_outputs_file_global, "r") as f:
106+
results = f.readlines()
107+
results = [line.rstrip() for line in results]
102108
with open(bam_outputs_path, "r") as f:
103109
# Read output bam file content without header
104110
trace_file_content = f.readlines()[1:]
@@ -111,6 +117,16 @@ def append_bam_outputs(self, run, destination_directory):
111117
file_name = elements[3].split("/")[-1]
112118
sample = elements[3].split("/")[-2]
113119
elements[3] = os.path.join(destination_directory, "bams", sample, file_name)
114-
trace_file_result.append("\t".join(elements))
120+
line = "\t".join(elements)
121+
if line not in results:
122+
trace_file_result.append(line)
123+
else:
124+
LOGGER.warning(f"{line} already in {bam_outputs_file_global}")
115125
with io.open(bam_outputs_file_global, "a") as f:
116126
f.writelines(trace_file_result)
127+
128+
def clean_up_source_directory(self, run):
129+
try:
130+
shutil.rmtree(run.output_directory)
131+
except Exception as e:
132+
logging.error(f"Failed to remove directory {run.output_directory}. {e}")

0 commit comments

Comments
 (0)