|
4 | 4 | import pytest |
5 | 5 |
|
6 | 6 |
|
| 7 | +import pytest |
| 8 | +from pairtools.lib.headerops import ( |
| 9 | + canonicalize_columns, |
| 10 | + get_column_index, |
| 11 | + extract_column_names, |
| 12 | +) |
| 13 | + |
| 14 | +def test_canonicalize_columns(): |
| 15 | + # Test basic canonicalization |
| 16 | + assert canonicalize_columns(['chr1', 'chr2']) == ['chrom1', 'chrom2'] |
| 17 | + assert canonicalize_columns(['chrom1', 'chrom2']) == ['chrom1', 'chrom2'] |
| 18 | + assert canonicalize_columns(['pt', 'other']) == ['pair_type', 'other'] |
| 19 | + |
| 20 | + # Test mixed case |
| 21 | + assert canonicalize_columns(['Chr1', 'CHR2']) == ['chrom1', 'chrom2'] |
| 22 | + assert canonicalize_columns(['CHR1', 'chr2']) == ['chrom1', 'chrom2'] |
| 23 | + |
| 24 | + # Test no changes needed |
| 25 | + assert canonicalize_columns(['readID', 'pos1']) == ['readID', 'pos1'] |
| 26 | + |
| 27 | + # Test empty input |
| 28 | + assert canonicalize_columns([]) == [] |
| 29 | + |
| 30 | + # Test all known aliases |
| 31 | + assert canonicalize_columns(['chr1', 'Chr2', 'PT']) == ['chrom1', 'chrom2', 'pair_type'] |
| 32 | + |
| 33 | +def test_get_column_index(): |
| 34 | + # Setup test columns |
| 35 | + columns = ['readID', 'chr1', 'pos1', 'chr2', 'pos2', 'strand1', 'strand2', 'pair_type'] |
| 36 | + |
| 37 | + # Test string lookup - direct matches |
| 38 | + assert get_column_index(columns, 'chr1') == 1 |
| 39 | + assert get_column_index(columns, 'pos2') == 4 |
| 40 | + assert get_column_index(columns, 'pair_type') == 7 |
| 41 | + |
| 42 | + # Test string lookup - canonicalized matches |
| 43 | + assert get_column_index(columns, 'chrom1') == 1 |
| 44 | + assert get_column_index(columns, 'CHROM2') == 3 |
| 45 | + assert get_column_index(columns, 'PT') == 7 |
| 46 | + |
| 47 | + # Test case insensitive matches |
| 48 | + assert get_column_index(columns, 'CHR1') == 1 |
| 49 | + assert get_column_index(columns, 'ChR2') == 3 |
| 50 | + |
| 51 | + # Test integer lookup |
| 52 | + assert get_column_index(columns, 0) == 0 |
| 53 | + assert get_column_index(columns, 3) == 3 |
| 54 | + assert get_column_index(columns, 7) == 7 |
| 55 | + |
| 56 | + # Test error cases |
| 57 | + with pytest.raises(ValueError, match="Column 'nonexistent' not found"): |
| 58 | + get_column_index(columns, 'nonexistent') |
| 59 | + |
| 60 | + with pytest.raises(ValueError, match="Column index 100 out of range"): |
| 61 | + get_column_index(columns, 100) |
| 62 | + |
| 63 | + with pytest.raises(AttributeError, match="Column spec must be string or integer"): |
| 64 | + get_column_index(columns, 3.14) |
| 65 | + |
| 66 | +def test_integration_with_extract_column_names(): |
| 67 | + # Test with actual header format |
| 68 | + header = [ |
| 69 | + "## pairs format v1.0", |
| 70 | + "#columns: readID chr1 pos1 chr2 pos2 strand1 strand2 pair_type", |
| 71 | + "#chromsize: chr1 1000", |
| 72 | + "#chromsize: chr2 800" |
| 73 | + ] |
| 74 | + |
| 75 | + columns = extract_column_names(header) |
| 76 | + assert columns == ['readID', 'chr1', 'pos1', 'chr2', 'pos2', 'strand1', 'strand2', 'pair_type'] |
| 77 | + |
| 78 | + # Test canonicalized column access |
| 79 | + assert get_column_index(columns, 'chrom1') == 1 |
| 80 | + assert get_column_index(columns, 'chrom2') == 3 |
| 81 | + assert get_column_index(columns, 'pt') == 7 |
| 82 | + |
| 83 | + # Test with alternative header format |
| 84 | + header2 = [ |
| 85 | + "## pairs format v1.0", |
| 86 | + "#columns: readID chrom1 pos1 chrom2 pos2 strand1 strand2 pair_type", |
| 87 | + ] |
| 88 | + columns2 = extract_column_names(header2) |
| 89 | + assert get_column_index(columns2, 'chr1') == 1 |
| 90 | + assert get_column_index(columns2, 'chr2') == 3 |
| 91 | + |
| 92 | +def test_edge_cases(): |
| 93 | + # Test empty columns |
| 94 | + with pytest.raises(ValueError): |
| 95 | + get_column_index([], 'chrom1') |
| 96 | + |
| 97 | + # Test invalid column spec type |
| 98 | + with pytest.raises(AttributeError): |
| 99 | + get_column_index(['a', 'b', 'c'], 3.14) # float not supported |
| 100 | + |
| 101 | + # Test negative indices |
| 102 | + assert get_column_index(['a', 'b', 'c'], -1) == 2 # Python-style negative indexing |
| 103 | + |
| 104 | + |
7 | 105 | def test_make_standard_header(): |
8 | 106 | header = headerops.make_standard_pairsheader() |
9 | 107 |
|
|
0 commit comments