from typing import Union
from pathlib import Path
from typing import Optional


def get_name_from_reads(path: Union[str, Path]) -> Optional[str]:
	""" Gets the name of a sample from the filename of one of the fastq files."""
	if isinstance(path, str):
		path = Path(path)
	name = path.stem
	# If the path refers to a read file, only the first part is useful.
	if 'R1' in name or 'R2' in name:
		name = name.split('_')[:-3]
		sample_name = "_".join(name)

	elif 'forward' in name or 'reverse' in name:
		# Probably trimmed
		sample_name, *_ = name.partition('.')
	else:
		sample_name = None
	return sample_name