def test_no_tags_returns_default(self): record = PafRecord() tag = "NM" actual = record.get_tag(tag) assert actual is None
def test_unmapped_record_returns_zero(self): record = PafRecord() actual = record.blast_identity() expected = 0.0 assert actual == expected
def test_alignment_block_same_length_as_matches(self): record = PafRecord(mlen=5, blen=5) actual = record.blast_identity() expected = 1.0 assert actual == expected
def test_line_with_no_tags(self): fields = [ "query_name", "1239", "65", "1239", "+", "target_name", "4378340", "2555250", "2556472", "1139", "1228", "60", ] line = "\t".join(fields) actual = PafRecord.from_str(line) expected = PafRecord( "query_name", 1239, 65, 1239, Strand.Forward, "target_name", 4378340, 2555250, 2556472, 1139, 1228, 60, ) assert actual == expected
def test_alignment_block_longer_than_matches(self): record = PafRecord(mlen=4, blen=5) actual = record.blast_identity() expected = 0.8 assert actual == expected
def test_no_matches_returns_zero(self): record = PafRecord(mlen=0, blen=5) actual = record.blast_identity() expected = 0.0 assert actual == expected
def test_tag_present(self): expected = Tag.from_str("de:f:0.1") tags = {"de": expected} record = PafRecord(tags=tags) tag = "de" actual = record.get_tag(tag) assert actual == expected
def test_tag_not_present_returns_default(self): tags = {"de": Tag.from_str("de:f:0.1")} record = PafRecord(tags=tags) tag = "NM" default = Tag.from_str("NM:i:0") actual = record.get_tag(tag, default=default) expected = default assert actual == expected
def test_for_loop_returns_pafrecords(self): with tempfile.TemporaryDirectory() as tmpdirname: record1 = PafRecord() record2 = PafRecord(qname="record2") path = Path(f"{tmpdirname}/test.paf") path.write_text(f"{str(record1)}\n{str(record2)}") with PafFile(path) as paf: actual = [record for record in paf] expected = [record1, record2] assert actual == expected
def test_unmapped_record_returns_zero(self): record = PafRecord(tlen=0) actual = record.target_coverage expected = 0.0 assert actual == expected
def test_tend_less_than_tstart(self): record = PafRecord(tstart=5, tend=2) actual = record.target_aligned_length expected = 3 assert actual == expected
def test_tend_greater_than_tstart(self): record = PafRecord(tstart=2, tend=5) actual = record.target_aligned_length expected = 3 assert actual == expected
def test_unmapped_record_returns_zero(self): record = PafRecord() actual = record.target_aligned_length expected = 0 assert actual == expected
def test_qend_less_than_qstart(self): record = PafRecord(qstart=5, qend=2) actual = record.query_aligned_length expected = 3 assert actual == expected
def test_qend_greater_than_qstart(self): record = PafRecord(qstart=2, qend=5) actual = record.query_aligned_length expected = 3 assert actual == expected
def test_unmapped_record_returns_zero(self): record = PafRecord() actual = record.relative_length expected = 0.0 assert actual == expected
def test_next_after_end_raises_error(self): with tempfile.TemporaryDirectory() as tmpdirname: record = PafRecord() path = Path(f"{tmpdirname}/test.paf") path.write_text(str(record)) with PafFile(path) as paf: next(paf) with pytest.raises(StopIteration): next(paf)
def test_mapped_record(self): record = PafRecord( qname="05f868dc-6760-47ec-b7e7-ab4054b0e4fe", qlen=4641, qstart=5, qend=4640, strand=Strand.Reverse, tname="NODE_1_length_4378477_cov_60.093643", tlen=4378340, tstart=1069649, tend=1074329, mlen=4499, blen=4740, mapq=60, tags={"tp": Tag.from_str("tp:A:P")}, ) assert not record.is_unmapped()
def test_no_tags(self): record = PafRecord() actual = str(record) expected = DELIM.join( str(x) for x in PafRecord._field_defaults.values() if x is not None) assert actual == expected
def test_next_returns_pafrecord(self): with tempfile.TemporaryDirectory() as tmpdirname: record = PafRecord() path = Path(f"{tmpdirname}/test.paf") path.write_text(str(record)) with PafFile(path) as paf: actual = next(paf) expected = record assert actual == expected
def test_with_tags(self): tags = {"NM": Tag.from_str("NM:i:1"), "ms": Tag.from_str("ms:i:1906")} record = PafRecord(tags=tags) actual = str(record) expected = (DELIM.join( str(x) for x in PafRecord._field_defaults.values() if x is not None) + DELIM + DELIM.join(tags)) assert actual == expected
def test_line_with_invalid_tag_raises_error(self): fields = [ "query_name", "1239", "65", "1239", "-", "target_name", "4378340", "2555250", "2556472", "1139", "1228", "60", "NMX:i:89", ] line = "\t".join(fields) with pytest.raises(InvalidTagFormat): PafRecord.from_str(line)
def test_part_of_query_aligned(self): qlen = 10 qalen = 4 patch_target = "pafpy.pafrecord.PafRecord.query_aligned_length" with patch(patch_target, new_callable=PropertyMock) as mocked_qalen: mocked_qalen.return_value = qalen record = PafRecord(qlen=qlen) actual = record.query_coverage mocked_qalen.assert_called_once() expected = 0.4 assert actual == expected
def test_all_of_target_aligned(self): tlen = 10 talen = 10 patch_target = "pafpy.pafrecord.PafRecord.target_aligned_length" with patch(patch_target, new_callable=PropertyMock) as mocked_talen: mocked_talen.return_value = talen record = PafRecord(tlen=tlen) actual = record.target_coverage mocked_talen.assert_called_once() expected = 1.0 assert actual == expected
def test_line_with_dupliacte_tag_returns_last_one(self): fields = [ "query_name", "1239", "65", "1239", "-", "target_name", "4378340", "2555250", "2556472", "1139", "1228", "60", "NM:i:89", "NM:i:2", ] line = "\t".join(fields) actual = PafRecord.from_str(line) expected_tags = {"NM": Tag.from_str("NM:i:2")} expected = PafRecord( "query_name", 1239, 65, 1239, Strand.Reverse, "target_name", 4378340, 2555250, 2556472, 1139, 1228, 60, expected_tags, ) assert actual == expected
def test_line_with_tags(self): fields = [ "query_name", "1239", "65", "1239", "-", "target_name", "4378340", "2555250", "2556472", "1139", "1228", "60", "NM:i:89", "ms:i:1906", ] line = "\t".join(fields) actual = PafRecord.from_str(line) tags = {"NM": Tag.from_str("NM:i:89"), "ms": Tag.from_str("ms:i:1906")} expected = PafRecord( "query_name", 1239, 65, 1239, Strand.Reverse, "target_name", 4378340, 2555250, 2556472, 1139, 1228, 60, tags, ) assert actual == expected
def test_query_and_target_same_length(self): talen = 10 qalen = 10 patch_talen = "pafpy.pafrecord.PafRecord.target_aligned_length" patch_qalen = "pafpy.pafrecord.PafRecord.query_aligned_length" with ExitStack() as stack: mocked_talen = stack.enter_context( patch(patch_talen, new_callable=PropertyMock)) mocked_qalen = stack.enter_context( patch(patch_qalen, new_callable=PropertyMock)) mocked_talen.return_value = talen mocked_qalen.return_value = qalen record = PafRecord() actual = record.relative_length mocked_talen.assert_called_once() mocked_qalen.assert_called_once() expected = 1.0 assert actual == expected
def __next__(self) -> PafRecord: if self.closed: raise IOError("PAF file is closed - cannot get next element.") return PafRecord.from_str(next(self._stream))
def test_unknown_char_raises_error(self): tag = Tag.from_str("tp:A:?") with pytest.raises(ValueError): PafRecord(strand=Strand.Forward, tags={ tag.tag: tag }).is_inversion()
def test_tp_tag_not_present_in_mapped_record_raises_error(self): with pytest.raises(ValueError): PafRecord(strand=Strand.Reverse).is_inversion()