1
1
import io
2
2
import os
3
+ import shutil
3
4
import logging
4
5
import subprocess
5
6
from django .conf import settings
@@ -34,6 +35,7 @@ def get_jobs(self):
34
35
raise Exception (f"Failed to copy bams for sample { ci_tag } " )
35
36
self .append_trace (run , destination_directory )
36
37
self .append_bam_outputs (run , destination_directory )
38
+ self .clean_up_source_directory (run )
37
39
return []
38
40
39
41
def construct_metadata (self , run ):
@@ -99,6 +101,10 @@ def append_trace(self, run, destination_directory):
99
101
def append_bam_outputs (self , run , destination_directory ):
100
102
bam_outputs_path = os .path .join (run .output_directory , "pipeline_output.csv" )
101
103
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 ]
102
108
with open (bam_outputs_path , "r" ) as f :
103
109
# Read output bam file content without header
104
110
trace_file_content = f .readlines ()[1 :]
@@ -111,6 +117,16 @@ def append_bam_outputs(self, run, destination_directory):
111
117
file_name = elements [3 ].split ("/" )[- 1 ]
112
118
sample = elements [3 ].split ("/" )[- 2 ]
113
119
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 } " )
115
125
with io .open (bam_outputs_file_global , "a" ) as f :
116
126
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