Skip to content

Commit eae65a3

Browse files
authored
Merge pull request #204 from mwang87/file-name-length-fix
Bug Fix - If output filename for results is too long, force it shorter
2 parents edda6a4 + 24b2f60 commit eae65a3

1 file changed

Lines changed: 24 additions & 8 deletions

File tree

massql/msql_cmd.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import sys
66
import json
7+
import uuid
78
import pandas as pd
89

910
# Making sure the root is in the path, kind of a hack
@@ -18,12 +19,12 @@ def main():
1819
parser = argparse.ArgumentParser(description="MSQL CMD")
1920
parser.add_argument('filename', help='Input filename')
2021
parser.add_argument('query', help='Input Query')
21-
parser.add_argument('--output_file', default=None, help='output results filename')
22+
parser.add_argument('--output_file', default=None, help='output results filename, if filename is too long will truncate filename')
2223
parser.add_argument('--parallel_query', default="NO", help='YES to make it parallel with ray locally, NO is default')
2324
parser.add_argument('--cache', default="YES", help='YES to cache with feather, YES is the default')
2425
parser.add_argument('--original_path', default=None, help='Original absolute path for the filename, useful in proteosafe')
25-
parser.add_argument('--extract_mzML', default=None, help='Extracting spectra found as mzML file')
26-
parser.add_argument('--extract_json', default=None, help='Extracting spectra found as json file, each spectrum is a line')
26+
parser.add_argument('--extract_mzML', default=None, help='Extracting spectra found as mzML file, if filename is too long will truncate filename')
27+
parser.add_argument('--extract_json', default=None, help='Extracting spectra found as json file, each spectrum is a line, if filename is too long, wiil truncate',)
2728
parser.add_argument('--maxfilesize', default=None, help='Maximum file size in MB')
2829

2930
args = parser.parse_args()
@@ -76,8 +77,17 @@ def main():
7677
results_df["mz_upper"] = results_df["comment"].astype(float) + 10
7778
except:
7879
pass
80+
81+
# Handling output filename
82+
output_results_filename = args.output_file
83+
output_file_toolong_unique_prefix = str(uuid.uuid4()).replace("-", "") + "_"
84+
85+
if output_results_filename is not None:
86+
if len(os.path.basename(output_results_filename)) > 80:
87+
output_results_filename = os.path.join(os.path.split(output_results_filename)[0],
88+
output_file_toolong_unique_prefix + os.path.basename(output_results_filename)[-80:])
7989

80-
if args.output_file and len(results_df) > 0:
90+
if output_results_filename and len(results_df) > 0:
8191
results_df["filename"] = os.path.basename(args.filename)
8292

8393
if args.original_path is not None:
@@ -116,16 +126,22 @@ def main():
116126
columns = list(results_df.columns)
117127
columns.sort()
118128

119-
if ".tsv" in args.output_file:
120-
results_df.to_csv(args.output_file, index=False, sep="\t", columns=columns)
129+
if ".tsv" in output_results_filename:
130+
results_df.to_csv(output_results_filename, index=False, sep="\t", columns=columns)
121131
else:
122-
results_df.to_csv(args.output_file, index=False, columns=columns)
132+
results_df.to_csv(output_results_filename, index=False, columns=columns)
123133

124134
# Extracting
125135
if args.extract_json is not None and len(results_df) > 0:
126136
print("Extracting {} spectra".format(len(results_df)))
127137
try:
128-
msql_extract._extract_spectra(results_df, os.path.dirname(args.filename), output_json_filename=args.extract_json)
138+
output_json_filename = args.extract_json
139+
if output_json_filename is not None:
140+
if len(os.path.basename(output_json_filename)) > 80:
141+
output_json_filename = os.path.join(os.path.split(output_json_filename)[0],
142+
output_file_toolong_unique_prefix + os.path.basename(output_json_filename)[-80:])
143+
144+
msql_extract._extract_spectra(results_df, os.path.dirname(args.filename), output_json_filename=output_json_filename)
129145
except:
130146
print("Extraction Failed")
131147

0 commit comments

Comments
 (0)