Skip to content

Commit 99a792b

Browse files
committed
fixed black
1 parent 2258c65 commit 99a792b

File tree

2 files changed

+74
-31
lines changed

2 files changed

+74
-31
lines changed

relecov_tools/assets/pipeline_utils/viralrecon.py

+22-10
Original file line numberDiff line numberDiff line change
@@ -158,33 +158,45 @@ def save_to_file(self, j_list, batch_date):
158158
file_name = "long_table_" + batch_date + ".json"
159159
file_path = os.path.join(self.output_directory, file_name)
160160
if os.path.exists(file_path):
161-
stderr.print(f"[blue]Long table {file_path} file already exists. Merging new data if possible.")
162-
log.info("Long table %s file already exists. Merging new data if possible." % file_path)
161+
stderr.print(
162+
f"[blue]Long table {file_path} file already exists. Merging new data if possible."
163+
)
164+
log.info(
165+
"Long table %s file already exists. Merging new data if possible."
166+
% file_path
167+
)
163168
original_table = relecov_tools.utils.read_json_file(file_path)
164169
samples_indict = {item["sample_name"]: item for item in original_table}
165170
for item in j_list:
166171
sample_name = item["sample_name"]
167172
if sample_name in samples_indict:
168173
if samples_indict[sample_name] != item:
169-
stderr.print(f"[red]Same sample has different data in both long tables.")
174+
stderr.print(
175+
f"[red]Same sample has different data in both long tables."
176+
)
170177
log.error(
171-
"Sample %s has different data in %s and new long table. Can't merge." % (sample_name, file_path)
172-
)
178+
"Sample %s has different data in %s and new long table. Can't merge."
179+
% (sample_name, file_path)
180+
)
173181
return None
174182
else:
175183
original_table.append(item)
176184
try:
177185
with open(file_path, "w") as fh:
178186
fh.write(json.dumps(original_table, indent=4))
179-
stderr.print("[green]\tParsed data successfully saved to file:", file_path)
187+
stderr.print(
188+
"[green]\tParsed data successfully saved to file:", file_path
189+
)
180190
except Exception as e:
181191
stderr.print("[red]\tError saving parsed data to file:", str(e))
182192

183193
else:
184194
try:
185195
with open(file_path, "w") as fh:
186196
fh.write(json.dumps(j_list, indent=4))
187-
stderr.print("[green]\tParsed data successfully saved to file:", file_path)
197+
stderr.print(
198+
"[green]\tParsed data successfully saved to file:", file_path
199+
)
188200
except Exception as e:
189201
stderr.print("[red]\tError saving parsed data to file:", str(e))
190202

@@ -311,9 +323,9 @@ def get_pango_data_version(files_list):
311323
)
312324
# Add custom content in pangolin
313325
pango_data_key = next(iter(pango_data))
314-
pango_data[pango_data_key]["lineage_analysis_date"] = (
315-
relecov_tools.utils.get_file_date(pango_file)
316-
)
326+
pango_data[pango_data_key][
327+
"lineage_analysis_date"
328+
] = relecov_tools.utils.get_file_date(pango_file)
317329
pango_data[pango_data_key]["pangolin_database_version"] = pango_data_v
318330
# Rename key in f_data
319331
pango_data_updated = {

relecov_tools/read_bioinfo_metadata.py

+52-21
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,9 @@ def validate_software_mandatory_files(self, files_dict):
233233
self.log_report.print_log_report(method_name, ["valid", "warning"])
234234
return
235235

236-
def add_bioinfo_results_metadata(self, files_dict, j_data, batch_id, output_folder=None):
236+
def add_bioinfo_results_metadata(
237+
self, files_dict, j_data, batch_id, output_folder=None
238+
):
237239
"""Adds metadata from bioinformatics results to j_data.
238240
It first calls file_handlers and then maps the handled
239241
data into j_data.
@@ -370,7 +372,12 @@ def handling_files(self, file_list, output_folder, batch_id):
370372
import_statement = f"import {utils_name}"
371373
exec(import_statement)
372374
# Get method name and execute it.
373-
data = eval(utils_name + "." + func_name + "(file_list, batch_id, output_folder)")
375+
data = eval(
376+
utils_name
377+
+ "."
378+
+ func_name
379+
+ "(file_list, batch_id, output_folder)"
380+
)
374381
except Exception as e:
375382
self.log_report.update_log_report(
376383
self.add_bioinfo_results_metadata.__name__,
@@ -751,12 +758,10 @@ def extract_batch_rows_to_file(file, sufix):
751758
sample_col = file_df.columns[sample_colpos]
752759
file_df[sample_col] = file_df[sample_col].astype(str)
753760
file_df = file_df[file_df[sample_col].isin(batch_samples)]
754-
761+
755762
base, ext = os.path.splitext(os.path.basename(file))
756763
new_filename = f"{base}_{sufix}{ext}"
757-
output_path = os.path.join(
758-
output_dir, "analysis_results", new_filename
759-
)
764+
output_path = os.path.join(output_dir, "analysis_results", new_filename)
760765
file_df.to_csv(output_path, index=False, sep=extdict.get(file_extension))
761766
return
762767

@@ -771,7 +776,7 @@ def extract_batch_rows_to_file(file, sufix):
771776
sample_colpos = self.get_sample_idx_colpos(key)
772777
for file in files:
773778
try:
774-
extract_batch_rows_to_file(file,sufix)
779+
extract_batch_rows_to_file(file, sufix)
775780
except Exception as e:
776781
if self.software_config[key].get("required"):
777782
log_type = "error"
@@ -794,17 +799,22 @@ def merge_metadata(self, batch_filepath, batch_data):
794799
batch_data (dict): A dictionary containing metadata of the samples.
795800
Returns:
796801
None
797-
"""
802+
"""
798803
merged_metadata = relecov_tools.utils.read_json_file(batch_filepath)
799-
prev_metadata_dict = {item["sequencing_sample_id"]: item for item in merged_metadata}
804+
prev_metadata_dict = {
805+
item["sequencing_sample_id"]: item for item in merged_metadata
806+
}
800807
for item in batch_data:
801808
sample_id = item["sequencing_sample_id"]
802809
if sample_id in prev_metadata_dict:
803810
# When sample already in metadata, checking whether dictionary is the same
804811
if prev_metadata_dict[sample_id] != item:
805-
stderr.print(f"[red] Sample {sample_id} has different data in {batch_filepath} and new metadata. Can't merge.")
812+
stderr.print(
813+
f"[red] Sample {sample_id} has different data in {batch_filepath} and new metadata. Can't merge."
814+
)
806815
log.error(
807-
"Sample %s has different data in %s and new metadata. Can't merge." % (sample_id, batch_filepath)
816+
"Sample %s has different data in %s and new metadata. Can't merge."
817+
% (sample_id, batch_filepath)
808818
)
809819
sys.exit(1)
810820
else:
@@ -842,11 +852,18 @@ def save_splitted_files(self, files_dict, batch_date, output_folder=None):
842852
continue
843853
try:
844854
# Dynamically import the function from the specified module
845-
utils_name = f"relecov_tools.assets.pipeline_utils.{self.software_name}"
855+
utils_name = (
856+
f"relecov_tools.assets.pipeline_utils.{self.software_name}"
857+
)
846858
import_statement = f"import {utils_name}"
847859
exec(import_statement)
848860
# Get method name and execute it.
849-
data = eval(utils_name + "." + func_name + "(file_path, batch_date, output_folder)")
861+
data = eval(
862+
utils_name
863+
+ "."
864+
+ func_name
865+
+ "(file_path, batch_date, output_folder)"
866+
)
850867
except Exception as e:
851868
self.log_report.update_log_report(
852869
self.save_splitted_files.__name__,
@@ -855,7 +872,7 @@ def save_splitted_files(self, files_dict, batch_date, output_folder=None):
855872
)
856873
sys.exit(self.log_report.print_log_report(method_name, ["error"]))
857874
return
858-
875+
859876
def get_multiple_sample_files(self):
860877
method_name = f"{self.add_bioinfo_files_path.__name__}:{self.get_multiple_sample_files.__name__}"
861878
multiple_sample_files = []
@@ -880,16 +897,20 @@ def create_bioinfo_file(self):
880897
# Split files found based on each batch of samples
881898
data_by_batch = self.split_data_by_batch(self.j_data)
882899
batch_dates = []
883-
#Get batch date for all the samples
900+
# Get batch date for all the samples
884901
for batch_dir, batch_dict in data_by_batch.items():
885902
if batch_dir.split("/")[-1] not in batch_dates:
886903
batch_dates.append(batch_dir.split("/")[-1])
887904

888905
if len(batch_dates) == 1:
889906
batch_dates = str(batch_dates[0])
890907
else:
891-
stderr.print(f"[orange]More than one batch date in the same json data. Using current date as batch date.")
892-
log.info("]More than one batch date in the same json data. Using current date as batch date.")
908+
stderr.print(
909+
f"[orange]More than one batch date in the same json data. Using current date as batch date."
910+
)
911+
log.info(
912+
"]More than one batch date in the same json data. Using current date as batch date."
913+
)
893914
batch_dates = datetime.now().strftime("%Y%m%d%H%M%S")
894915

895916
# Add bioinfo metadata to j_data
@@ -917,8 +938,13 @@ def create_bioinfo_file(self):
917938
batch_filename = tag + lab_code + "_" + batch_date + ".json"
918939
batch_filepath = os.path.join(batch_dir, batch_filename)
919940
if os.path.exists(batch_filepath):
920-
stderr.print(f"[blue]Bioinfo metadata {batch_filepath} file already exists. Merging new data if possible.")
921-
log.info("Bioinfo metadata %s file already exists. Merging new data if possible." % batch_filepath)
941+
stderr.print(
942+
f"[blue]Bioinfo metadata {batch_filepath} file already exists. Merging new data if possible."
943+
)
944+
log.info(
945+
"Bioinfo metadata %s file already exists. Merging new data if possible."
946+
% batch_filepath
947+
)
922948
batch_data = self.merge_metadata(batch_filepath, batch_data)
923949
else:
924950
relecov_tools.utils.write_json_fo_file(batch_data, batch_filepath)
@@ -941,8 +967,13 @@ def create_bioinfo_file(self):
941967
stderr.print("[blue]Writting output json file")
942968
file_path = os.path.join(out_path, batch_filename)
943969
if os.path.exists(file_path):
944-
stderr.print(f"[blue]Bioinfo metadata {file_path} file already exists. Merging new data if possible.")
945-
log.info("Bioinfo metadata %s file already exists. Merging new data if possible." % file_path)
970+
stderr.print(
971+
f"[blue]Bioinfo metadata {file_path} file already exists. Merging new data if possible."
972+
)
973+
log.info(
974+
"Bioinfo metadata %s file already exists. Merging new data if possible."
975+
% file_path
976+
)
946977
batch_data = self.merge_metadata(file_path, self.j_data)
947978
else:
948979
relecov_tools.utils.write_json_fo_file(self.j_data, file_path)

0 commit comments

Comments
 (0)