Repository: nf-core/tools

Issue:
SAMPLESHEET_CHECK fails on sample sheet with many sample lines
### Description of the bug

`check_samplesheet.py` is throwing an error when the sample sheet has many sample lines. Attached is a sample sheet that failed.
This bug has been first reported by a user (m3hdad) while running `rnavar` pipeline and I have replicated the issue with a test sheet - attached here

[samplesheet_large.csv](https://github.com/nf-core/tools/files/8669457/samplesheet_large.csv)

### Command used and terminal output

```console
$nextflow run nf-core/rnavar -r dev -profile test,docker --input samplesheet_large.csv
......
<output truncated>
......
Error executing process > 'NFCORE_RNAVAR:RNAVAR:INPUT_CHECK:SAMPLESHEET_CHECK (samplesheet_large.csv)'

Caused by:
  Process `NFCORE_RNAVAR:RNAVAR:INPUT_CHECK:SAMPLESHEET_CHECK (samplesheet_large.csv)` terminated with an error exit status (1)

Command executed:

  check_samplesheet.py \
      samplesheet_large.csv \
      samplesheet.valid.csv

  cat <<-END_VERSIONS > versions.yml
  "NFCORE_RNAVAR:RNAVAR:INPUT_CHECK:SAMPLESHEET_CHECK":
      python: $(python --version | sed 's/Python //g')
  END_VERSIONS

Command exit status:
  1

Command output:
  (empty)

Command error:
  Traceback (most recent call last):
    File "/data2/praveen/rnavar_eval/test_data/rnavar/bin/check_samplesheet.py", line 250, in <module>
      sys.exit(main())
    File "/data2/praveen/rnavar_eval/test_data/rnavar/bin/check_samplesheet.py", line 246, in main
      check_samplesheet(args.file_in, args.file_out)
    File "/data2/praveen/rnavar_eval/test_data/rnavar/bin/check_samplesheet.py", line 186, in check_samplesheet
      reader = csv.DictReader(in_handle, dialect=sniff_format(in_handle))
    File "/data2/praveen/rnavar_eval/test_data/rnavar/bin/check_samplesheet.py", line 149, in sniff_format
      if not sniffer.has_header(peek):
    File "/usr/local/lib/python3.9/csv.py", line 393, in has_header
      rdr = reader(StringIO(sample), self.sniff(sample))
    File "/usr/local/lib/python3.9/csv.py", line 187, in sniff
      raise Error("Could not determine delimiter")
  _csv.Error: Could not determine delimiter
```


### System information

Python 3.8.8
nextflow version: 21.10.6
SAMPLESHEET_CHECK fails on sample sheet with many sample lines
### Description of the bug

`check_samplesheet.py` is throwing an error when the sample sheet has many sample lines. Attached is a sample sheet that failed.
This bug has been first reported by a user (m3hdad) while running `rnavar` pipeline and I have replicated the issue with a test sheet - attached here

[samplesheet_large.csv](https://github.com/nf-core/tools/files/8669457/samplesheet_large.csv)

### Command used and terminal output

```console
$nextflow run nf-core/rnavar -r dev -profile test,docker --input samplesheet_large.csv
......
<output truncated>
......
Error executing process > 'NFCORE_RNAVAR:RNAVAR:INPUT_CHECK:SAMPLESHEET_CHECK (samplesheet_large.csv)'

Caused by:
  Process `NFCORE_RNAVAR:RNAVAR:INPUT_CHECK:SAMPLESHEET_CHECK (samplesheet_large.csv)` terminated with an error exit status (1)

Command executed:

  check_samplesheet.py \
      samplesheet_large.csv \
      samplesheet.valid.csv

  cat <<-END_VERSIONS > versions.yml
  "NFCORE_RNAVAR:RNAVAR:INPUT_CHECK:SAMPLESHEET_CHECK":
      python: $(python --version | sed 's/Python //g')
  END_VERSIONS

Command exit status:
  1

Command output:
  (empty)

Command error:
  Traceback (most recent call last):
    File "/data2/praveen/rnavar_eval/test_data/rnavar/bin/check_samplesheet.py", line 250, in <module>
      sys.exit(main())
    File "/data2/praveen/rnavar_eval/test_data/rnavar/bin/check_samplesheet.py", line 246, in main
      check_samplesheet(args.file_in, args.file_out)
    File "/data2/praveen/rnavar_eval/test_data/rnavar/bin/check_samplesheet.py", line 186, in check_samplesheet
      reader = csv.DictReader(in_handle, dialect=sniff_format(in_handle))
    File "/data2/praveen/rnavar_eval/test_data/rnavar/bin/check_samplesheet.py", line 149, in sniff_format
      if not sniffer.has_header(peek):
    File "/usr/local/lib/python3.9/csv.py", line 393, in has_header
      rdr = reader(StringIO(sample), self.sniff(sample))
    File "/usr/local/lib/python3.9/csv.py", line 187, in sniff
      raise Error("Could not determine delimiter")
  _csv.Error: Could not determine delimiter
```


### System information

Python 3.8.8
nextflow version: 21.10.6


Discussion:

Resolving patch:
diff --git a/nf_core/pipeline-template/bin/check_samplesheet.py b/nf_core/pipeline-template/bin/check_samplesheet.py
--- a/nf_core/pipeline-template/bin/check_samplesheet.py
+++ b/nf_core/pipeline-template/bin/check_samplesheet.py
@@ -129,6 +129,16 @@ def validate_unique_samples(self):
                     row[self._sample_col] = f"{sample}_T{seen[sample]}"
 
 
+def read_head(handle, num_lines=10):
+    """Read the specified number of lines from the current position in the file."""
+    lines = []
+    for idx, line in enumerate(handle):
+        if idx == num_lines:
+            break
+        lines.append(line)
+    return "".join(lines)
+
+
 def sniff_format(handle):
     """
     Detect the tabular format.
@@ -144,13 +154,13 @@ def sniff_format(handle):
         https://docs.python.org/3/glossary.html#term-text-file
 
     """
-    peek = handle.read(2048)
+    peek = read_head(handle)
+    handle.seek(0)
     sniffer = csv.Sniffer()
     if not sniffer.has_header(peek):
         logger.critical(f"The given sample sheet does not appear to contain a header.")
         sys.exit(1)
     dialect = sniffer.sniff(peek)
-    handle.seek(0)
     return dialect
 
 


Test patch: