Skip to content

Commit

Permalink
More helpful error message on unrecognized input file format
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelm committed Jun 20, 2024
1 parent 9276a89 commit d9cf273
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
7 changes: 5 additions & 2 deletions src/cutadapt/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def has_qualities(self) -> bool:


# TODO copied and adjusted from dnaio; upstream this
def detect_file_format(file: BinaryIO) -> Optional[FileFormat]:
def detect_file_format(file: BinaryIO) -> FileFormat:
if file.seekable():
original_position = file.tell()
magic = file.read(4)
Expand All @@ -313,4 +313,7 @@ def detect_file_format(file: BinaryIO) -> Optional[FileFormat]:
return FileFormat.FASTA
elif magic == b"BAM\1":
return FileFormat.BAM
return None
raise dnaio.exceptions.UnknownFileFormat(
f"Input file format not recognized. The file starts with {magic!r}, "
"but files in supported formats start with '>' (FASTA), '@' (FASTQ) or 'BAM'"
)
19 changes: 4 additions & 15 deletions src/cutadapt/runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,8 @@ def run(self):
self._file_format_connection.send((e, traceback.format_exc()))
raise
self._file_format_connection.send(file_format)
if file_format is not None:
for index, chunks in enumerate(self._read_chunks(*files)):
self.send_to_worker(index, *chunks)
for index, chunks in enumerate(self._read_chunks(*files)):
self.send_to_worker(index, *chunks)
self.shutdown()
except Exception as e:
# TODO better send this to a common "something went wrong" Queue
Expand Down Expand Up @@ -321,12 +320,7 @@ def __init__(
)
self._reader_process.daemon = True
self._reader_process.start()
file_format: Optional[FileFormat] = self._try_receive(file_format_connection_r)
if file_format is None:
raise dnaio.exceptions.UnknownFileFormat(
f"Format of input file '{self._inpaths.paths[0]}' not recognized."
)
self._input_file_format = file_format
self._input_file_format = self._try_receive(file_format_connection_r)

def _start_workers(
self, pipeline, proxy_files
Expand Down Expand Up @@ -434,12 +428,7 @@ def close(self):
self._infiles.close()

def input_file_format(self) -> FileFormat:
detected = detect_file_format(self._infiles._files[0])
if detected is None:
raise dnaio.exceptions.UnknownFileFormat(
f"Format of input file '{self._infiles._files[0].name}' not recognized."
)
return detected
return detect_file_format(self._infiles._files[0])


def make_runner(
Expand Down

0 comments on commit d9cf273

Please sign in to comment.