|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Validate assembly/reference_genomes.tsv structure. |
| 4 | +
|
| 5 | +This file has no header row and expects exactly 4 tab-separated columns: |
| 6 | +1. tax_id (integer) |
| 7 | +2. short_name (string, may be empty) |
| 8 | +3. description (string) |
| 9 | +4. accessions (colon-separated list of accession IDs) |
| 10 | +""" |
| 11 | + |
| 12 | +import sys |
| 13 | +import csv |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | + |
| 17 | +def validate_reference_genomes_tsv(tsv_file: Path) -> bool: |
| 18 | + """ |
| 19 | + Validate that the reference_genomes.tsv file has proper structure: |
| 20 | + - Tab-separated values (not spaces) |
| 21 | + - Exactly 4 columns in every row |
| 22 | + - tax_id is a valid integer |
| 23 | + - accessions field is not empty |
| 24 | + """ |
| 25 | + EXPECTED_COLUMNS = 4 |
| 26 | + errors = [] |
| 27 | + |
| 28 | + print(f"Validating reference genomes TSV: {tsv_file}") |
| 29 | + |
| 30 | + with open(tsv_file, 'r', encoding='utf-8') as f: |
| 31 | + reader = csv.reader(f, delimiter='\t') |
| 32 | + |
| 33 | + for row_num, row in enumerate(reader, start=1): |
| 34 | + # Skip empty rows |
| 35 | + if not row or all(cell.strip() == '' for cell in row): |
| 36 | + continue |
| 37 | + |
| 38 | + # Check column count |
| 39 | + if len(row) != EXPECTED_COLUMNS: |
| 40 | + errors.append( |
| 41 | + f"Line {row_num}: Expected {EXPECTED_COLUMNS} columns, found {len(row)}. " |
| 42 | + f"This may indicate spaces were used instead of tabs." |
| 43 | + ) |
| 44 | + # Show a preview of what was parsed |
| 45 | + if len(row) == 1 and ' ' in row[0]: |
| 46 | + errors.append( |
| 47 | + f" Hint: Line appears to use spaces instead of tabs as delimiters." |
| 48 | + ) |
| 49 | + continue |
| 50 | + |
| 51 | + tax_id, short_name, description, accessions = row |
| 52 | + |
| 53 | + # Validate tax_id is an integer |
| 54 | + tax_id = tax_id.strip() |
| 55 | + if not tax_id: |
| 56 | + errors.append(f"Line {row_num}: tax_id (column 1) is empty") |
| 57 | + else: |
| 58 | + try: |
| 59 | + int(tax_id) |
| 60 | + except ValueError: |
| 61 | + errors.append(f"Line {row_num}: tax_id '{tax_id}' is not a valid integer") |
| 62 | + |
| 63 | + # Validate accessions is not empty |
| 64 | + accessions = accessions.strip() |
| 65 | + if not accessions: |
| 66 | + errors.append(f"Line {row_num}: accessions (column 4) is empty") |
| 67 | + |
| 68 | + if errors: |
| 69 | + print(f"\nERROR: Found {len(errors)} validation issue(s):\n") |
| 70 | + for error in errors: |
| 71 | + print(f" {error}") |
| 72 | + return False |
| 73 | + |
| 74 | + print(f"Validation passed: All rows have {EXPECTED_COLUMNS} tab-separated columns") |
| 75 | + return True |
| 76 | + |
| 77 | + |
| 78 | +def main(): |
| 79 | + if len(sys.argv) != 2: |
| 80 | + print("Usage: python validate_reference_genomes_tsv.py <tsv_file>") |
| 81 | + sys.exit(1) |
| 82 | + |
| 83 | + tsv_file = Path(sys.argv[1]) |
| 84 | + if not tsv_file.exists(): |
| 85 | + print(f"ERROR: File not found: {tsv_file}") |
| 86 | + sys.exit(1) |
| 87 | + |
| 88 | + if not validate_reference_genomes_tsv(tsv_file): |
| 89 | + sys.exit(1) |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + main() |
0 commit comments