PAF I/O¶
This module provides classes and helpers for reading, writing, and reordering PAF (Pairwise mApping Format) alignment records.
PafAlignment — Alignment record collection¶
PafAlignment wraps a list of PafRecord
objects and provides filtering, contig reordering, and sequence-length lookup
utilities. It can be passed directly to
DotPlotter — no
SequenceIndex is
required:
from dot_explorer.paf_io import PafAlignment
from dot_explorer.dotplot import DotPlotter
aln = PafAlignment.from_file("alignments.paf")
q_order, t_order = aln.reorder_contigs()
plotter = DotPlotter(aln)
plotter.plot(
query_names=q_order,
target_names=t_order,
output_path="dotplot.png",
scale_sequences=True,
)
PafRecord
dataclass
¶
A single PAF alignment record.
The twelve required PAF columns are represented as typed attributes.
Optional SAM-like tags (e.g. tp:A:P, cg:Z:10M) are stored in
:attr:tags. If a cg:Z: tag is present, CIGAR-derived alignment
statistics are populated automatically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query_name
|
str
|
Query sequence name (column 1). |
required |
query_len
|
int
|
Query sequence length (column 2). |
required |
query_start
|
int
|
Query start position, 0-based (column 3). |
required |
query_end
|
int
|
Query end position, exclusive (column 4). |
required |
strand
|
str
|
Relative strand: |
required |
target_name
|
str
|
Target sequence name (column 6). |
required |
target_len
|
int
|
Target sequence length (column 7). |
required |
target_start
|
int
|
Target start position, 0-based (column 8). |
required |
target_end
|
int
|
Target end position, exclusive (column 9). |
required |
residue_matches
|
int
|
Number of residue matches (column 10). |
required |
alignment_block_len
|
int
|
Number of bases in the alignment block (column 11). |
required |
mapping_quality
|
int
|
Mapping quality (0–255; 255 = missing) (column 12). |
required |
tags
|
dict[str, Any]
|
Optional SAM-like tags decoded as |
dict()
|
cigar
|
str or None
|
CIGAR string from |
None
|
alignment_length
|
int or None
|
Target-span alignment length derived from CIGAR, or |
None
|
n_matches
|
int or None
|
Count of exact-match bases ( |
None
|
n_mismatches
|
int or None
|
Count of mismatch bases ( |
None
|
n_gaps
|
int or None
|
Total number of gap bases ( |
None
|
n_gap_bases
|
int or None
|
Same as |
None
|
tag_types
|
dict[str, str]
|
SAM tag type characters (e.g. |
dict()
|
Source code in dot_explorer/paf_io.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | |
Attributes¶
query_aligned_len
property
¶
Return the aligned length on the query sequence.
Returns:
| Type | Description |
|---|---|
int
|
|
target_aligned_len
property
¶
Return the aligned length on the target sequence.
Returns:
| Type | Description |
|---|---|
int
|
|
identity
property
¶
Return the fraction identity of this alignment in [0, 1].
The most accurate available metric is used, in order of preference:
1 - defrom thede:ftag — minimap2's gap-compressed per-base divergence, emitted with base-level alignment (-cor--cs). A gap run counts as a single difference.- Gap-compressed identity derived from the CIGAR:
residue_matches / (aligned columns + gap openings). Column 10 is used as the match count because minimap2's defaultM-style CIGAR does not distinguish mismatches. - BLAST-style identity from the required PAF columns:
residue_matches / alignment_block_len. For output without base-level alignment this is an approximation.
Returns:
| Type | Description |
|---|---|
float
|
Fraction identity, clamped to |
Methods:¶
from_line(line)
classmethod
¶
Parse a single PAF text line into a :class:PafRecord.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
line
|
str
|
A single PAF record line (tab-separated, trailing newline optional). |
required |
Returns:
| Type | Description |
|---|---|
PafRecord
|
The parsed record. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the line has fewer than 12 tab-separated fields. |
Source code in dot_explorer/paf_io.py
to_line()
¶
Serialise this record back to a PAF-format string (no trailing newline).
Returns:
| Type | Description |
|---|---|
str
|
Tab-separated PAF line with the 12 required columns followed by
any optional tags in insertion order. Tag type characters come
from :attr: |
Source code in dot_explorer/paf_io.py
PafAlignment
¶
A collection of PAF alignment records with contig-ordering utilities.
Can be constructed from a file path or an iterable of :class:PafRecord
objects. Provides :meth:reorder_contigs to sort query and target
sequence names so that a subsequent dotplot shows maximum collinearity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
records
|
list of PafRecord
|
The alignment records. |
required |
Examples:
Load from a file and reorder contigs:
>>> aln = PafAlignment.from_file("alignments.paf")
>>> q_order, t_order = aln.reorder_contigs(aln.query_names, aln.target_names)
Source code in dot_explorer/paf_io.py
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 | |
Attributes¶
query_names
property
¶
Return a deduplicated list of query sequence names (insertion order).
Returns:
| Type | Description |
|---|---|
list[str]
|
Unique query names in the order first seen. |
target_names
property
¶
Return a deduplicated list of target sequence names (insertion order).
Returns:
| Type | Description |
|---|---|
list[str]
|
Unique target names in the order first seen. |
groups
property
¶
Return the current group assignments.
If groups have not been set explicitly via :meth:set_groups or
:meth:rename_group, returns the default: all query sequence names
in group 'a' and all target sequence names in group 'b'.
Returns:
| Type | Description |
|---|---|
dict[str, list[str]]
|
Mapping of group label → list of sequence names. |
reversed_contigs
property
¶
Query contigs detected as reverse-oriented by :meth:reorder_contigs.
Empty until :meth:reorder_contigs has been called. These contigs
align in reverse orientation against their best-matching target and can
be passed to :meth:~dot_explorer.dotplot.DotPlotter.plot via
reverse_contigs= to be rendered flipped along the main diagonal.
Returns:
| Type | Description |
|---|---|
set[str]
|
Names of reverse-oriented query contigs. |
Methods:¶
from_file(path)
classmethod
¶
Load records from a PAF file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str or Path
|
Path to the PAF file. |
required |
Returns:
| Type | Description |
|---|---|
PafAlignment
|
New instance with all records loaded. |
Source code in dot_explorer/paf_io.py
from_records(records)
classmethod
¶
Construct from an iterable of :class:PafRecord objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
records
|
iterable of PafRecord
|
Source records. |
required |
Returns:
| Type | Description |
|---|---|
PafAlignment
|
New instance. |
Source code in dot_explorer/paf_io.py
sequence_names()
¶
Return a deduplicated list of all query and target sequence names.
The list contains each name at most once, in the order it was first
encountered (queries before targets within each record). This method
makes :class:PafAlignment compatible with :class:~dot_explorer.dotplot.DotPlotter.
Returns:
| Type | Description |
|---|---|
list[str]
|
All unique sequence names across query and target fields. |
Source code in dot_explorer/paf_io.py
get_sequence_length(name)
¶
Return the length of a sequence by name as stored in PAF records.
Looks up name in the query_name and target_name fields of
every record and returns the corresponding query_len or
target_len. This method makes :class:PafAlignment compatible
with :class:~dot_explorer.dotplot.DotPlotter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Sequence name to look up. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Length of the sequence. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If name is not found in any record. |
Source code in dot_explorer/paf_io.py
__len__()
¶
__repr__()
¶
Return a concise string representation.
Returns:
| Type | Description |
|---|---|
str
|
|
Source code in dot_explorer/paf_io.py
filter_by_query(names)
¶
Return a new :class:PafAlignment containing only the given query names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
names
|
iterable of str
|
Query names to keep. |
required |
Returns:
| Type | Description |
|---|---|
PafAlignment
|
Filtered alignment. |
Source code in dot_explorer/paf_io.py
filter_by_target(names)
¶
Return a new :class:PafAlignment containing only the given target names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
names
|
iterable of str
|
Target names to keep. |
required |
Returns:
| Type | Description |
|---|---|
PafAlignment
|
Filtered alignment. |
Source code in dot_explorer/paf_io.py
filter_by_min_length(min_length)
¶
Return a new :class:PafAlignment keeping only records of sufficient length.
Filters on the query aligned length (query_end - query_start), which
equals the alignment block span for both merged k-mer runs and PAF
alignments imported from a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
min_length
|
int
|
Minimum alignment length (inclusive). Records with a query aligned
length strictly less than |
required |
Returns:
| Type | Description |
|---|---|
PafAlignment
|
Filtered alignment containing only records with
|
Source code in dot_explorer/paf_io.py
set_groups(groups)
¶
Set custom group assignments for sequence names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
groups
|
dict[str, list[str]]
|
Mapping of group label → list of sequence names belonging to that group. |
required |
Warns:
| Type | Description |
|---|---|
Logs a warning for every sequence name that appears in more than one
|
|
group.
|
|
Source code in dot_explorer/paf_io.py
rename_group(old_name, new_name)
¶
Rename a group label.
If custom groups have not been set yet, the default assignment
('a' → query names, 'b' → target names) is materialised
first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
old_name
|
str
|
Current group label. |
required |
new_name
|
str
|
New group label. |
required |
Raises:
| Type | Description |
|---|---|
KeyError
|
If old_name is not a known group. |
ValueError
|
If new_name already exists as a different group label. |
Source code in dot_explorer/paf_io.py
reorder_contigs(query_names=None, target_names=None, query_group=None, target_group=None)
¶
Sort query and target contigs to maximise collinearity in the dotplot.
Uses the gravity-centre algorithm: each contig is assigned a gravity equal to the weighted mean position of its alignment blocks on the opposing axis. Contigs are then sorted by ascending gravity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query_names
|
list[str] or None
|
Query contigs to reorder. Ignored when query_group is given.
Defaults to :attr: |
None
|
target_names
|
list[str] or None
|
Target contigs to reorder. Ignored when target_group is given.
Defaults to :attr: |
None
|
query_group
|
str or None
|
Group label whose members are used as query contigs. When
provided, the corresponding entry in :attr: |
None
|
target_group
|
str or None
|
Group label whose members are used as target contigs. When
provided, the corresponding entry in :attr: |
None
|
Returns:
| Type | Description |
|---|---|
tuple[list[str], list[str]]
|
|
Raises:
| Type | Description |
|---|---|
KeyError
|
If a supplied group label is not present in :attr: |
Source code in dot_explorer/paf_io.py
Functions¶
parse_paf_file(path)
¶
Yield :class:PafRecord objects from a PAF file.
Lines beginning with # are treated as comments and skipped. Empty
lines are also skipped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str or Path
|
Path to the PAF file. |
required |
Yields:
| Type | Description |
|---|---|
PafRecord
|
One record per non-comment, non-empty line. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If |
ValueError
|
If a line cannot be parsed as a PAF record. |
Source code in dot_explorer/paf_io.py
compute_gravity_contigs(records, query_names, target_names, sort_targets=True)
¶
Return query/target contigs sorted by gravity centre, plus reversed set.
Implements the d-genies gravity algorithm: each contig is assigned to its
single best-matching chromosome (argmax of summed squared match weights,
(1 + euclidean_length) ** 2) and positioned by the squared-weighted mean
of its match mid-points on the concatenated opposing axis, using only the
matches to that best chromosome. Contigs are then sorted by ascending
position; contigs with no alignments are placed last, by descending length.
When sort_targets is True (default) the targets are ordered first
(against the queries in their input order) and the queries are then ordered
against the freshly sorted target axis, so contigs group according to the
displayed target arrangement. When False the target order is treated as
fixed (returned unchanged) and only the queries are reordered against it —
use this to reorder one assembly against another that must not move.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
records
|
iterable of PafRecord
|
Alignment records to use for computing gravity centres. |
required |
query_names
|
list[str]
|
The query contig names to reorder. |
required |
target_names
|
list[str]
|
The target contig names to reorder (or to keep fixed when
sort_targets is |
required |
sort_targets
|
bool
|
Whether to reorder the targets too. Default is |
True
|
Returns:
| Type | Description |
|---|---|
tuple[list[str], list[str], set[str]]
|
|
Source code in dot_explorer/paf_io.py
compute_reversed_contigs(query_names, matches, len_map, best_other)
¶
Return query contigs that are reverse-oriented against their best target.
Ports d-genies' is_contig_well_oriented: for each query contig, take its
matches on its best-matching chromosome, keep only "big" matches (euclidean
length greater than 10% of the longest match and at least 1% of
min(contig_len, chrom_len)), sort them by query mid-point, and check
whether the target mid-point trends upward. A contig is reverse-oriented
when the mean of the consecutive-pair direction signs is <= -0.1 (for a
single big match, when that match is on the - strand). Contigs with no
big matches are treated as forward (well oriented).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query_names
|
list[str]
|
Query contig names to test. |
required |
matches
|
dict[tuple[str, str], list[PafRecord]]
|
Records keyed by |
required |
len_map
|
dict[str, int]
|
Sequence lengths for every name. |
required |
best_other
|
dict[str, str | None]
|
Mapping of each query contig to its best-matching target (or |
required |
Returns:
| Type | Description |
|---|---|
set[str]
|
Names of query contigs detected as reverse-oriented. |
Source code in dot_explorer/paf_io.py
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 | |
reverse_complement(seq)
¶
Return the reverse complement of a nucleotide sequence.
Complements A/C/G/T/N (case preserved) and reverses the string. Any
other character is left unchanged before reversal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seq
|
str
|
Nucleotide sequence. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The reverse-complemented sequence. |