1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 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 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
|
import os
import os.path as op
import shlex
import subprocess
import sys
import click
from ..util import cmd_exists
from . import cli, get_logger
try:
from subprocess import DEVNULL # py3
except ImportError:
DEVNULL = open(os.devnull, "wb")
logger = get_logger(__name__)
SORT_POS = "sort -k{C1},{C1} -k{P1},{P1}n -k{C2},{C2} -k{P2},{P2}n"
SORT_BLK = "sort -k{C1},{C1} -k{C2},{C2} -k{P1},{P1}n -k{P2},{P2}n"
INDEX_TBX = "tabix -f -s{C1} -b{P1} -e{P1}"
INDEX_PX2 = "pairix -f -s{C1} -d{C2} -b{P1} -e{P1} -u{P2} -v{P2}"
FLIP_TEMPLATE = """\
import sys
from signal import signal, SIGPIPE, SIG_DFL
signal(SIGPIPE, SIG_DFL)
instream = getattr(sys.stdin, "buffer", sys.stdin)
outstream = getattr(sys.stdout, "buffer", sys.stdout)
with open("{chromosomes_path}", "rb") as f:
chrIDs = {{}}
for i, line in enumerate(f, 1):
chrom = line.split(b"\\t")[0].strip()
if chrom:
chrIDs[chrom] = i
for line in instream:
if line.startswith(b"{comment_char}"):
continue
parts = line.strip().split(b"{sep}")
chrom1, chrom2 = parts[{c1}], parts[{c2}]
if chrom1 in chrIDs and chrom2 in chrIDs:
cid1, cid2 = chrIDs[chrom1], chrIDs[chrom2]
pos1, pos2 = int(parts[{p1}]), int(parts[{p2}])
if (cid1 > cid2) or ((cid1 == cid2) and (pos1 > pos2)):
parts[{c1}], parts[{c2}] = parts[{c2}], parts[{c1}]
parts[{p1}], parts[{p2}] = parts[{p2}], parts[{p1}]
outstream.write(b"\\t".join(parts) + b"\\n")
"""
def _has_parallel_sort():
return (
subprocess.call(
["sort", "--parallel=1"], stdin=DEVNULL, stdout=DEVNULL, stderr=DEVNULL
) == 0
)
def _validate_fieldnum(ctx, param, value):
if value <= 0:
raise click.BadParameter("Field numbers are one-based")
return value
def make_read_command(infile):
"""
If input file appears gzipped based its extension, read using one of pigz
or gzip for decompression. Otherwise assume uncompressed and use cat.
Note
----
Gzip decompression can't actually be parallelized, but pigz will create
three extra threads that may help.
See <https://github.com/madler/pigz/issues/36>.
"""
ingzip = infile.endswith(".gz")
if ingzip:
if cmd_exists("pigz"):
read_cmd = ["pigz", "-dc", infile]
elif cmd_exists("gzip"):
read_cmd = ["gzip", "-dc", infile]
else:
print("No gzip decompressor found.", file=sys.stderr)
sys.exit(1)
else:
read_cmd = ["cat", infile]
return read_cmd
def make_flip_command(chromosomes_path, sep, comment_char, fields):
with open(chromosomes_path) as f:
logger.info("Enumerating requested chromosomes...")
for i, line in enumerate(f, 1):
chrom = line.split("\t")[0].strip()
if chrom:
logger.info(chrom + "\t" + str(i))
# zero-based column indices
c1 = fields["C1"] - 1
c2 = fields["C2"] - 1
p1 = fields["P1"] - 1
p2 = fields["P2"] - 1
flip_code = FLIP_TEMPLATE.format(
chromosomes_path=chromosomes_path,
sep=sep,
comment_char=comment_char,
c1=c1,
c2=c2,
p1=p1,
p2=p2,
)
return [sys.executable, "-c", flip_code]
def make_sort_command(index, fields, sort_options):
os.environ["LC_ALL"] = "C"
if index == "tabix":
sort_cmd = shlex.split(SORT_POS.format(**fields))
elif index == "pairix":
sort_cmd = shlex.split(SORT_BLK.format(**fields))
sort_cmd += sort_options
return sort_cmd
def make_index_command(index, fields, zero_based, outfile):
if index == "tabix":
index_cmd = shlex.split(INDEX_TBX.format(**fields))
elif index == "pairix":
index_cmd = shlex.split(INDEX_PX2.format(**fields))
if zero_based:
index_cmd += ["-0"]
return index_cmd + [outfile]
@cli.command()
@click.argument(
"pairs_path",
type=click.Path(exists=True, allow_dash=True),
metavar="PAIRS_PATH"
)
@click.argument(
"chromosomes_path",
type=click.Path(exists=True),
metavar="CHROMOSOMES_PATH"
)
@click.option(
"--chrom1",
"-c1",
required=True,
help="chrom1 field number in the input file (starting from 1)",
type=int,
callback=_validate_fieldnum,
)
@click.option(
"--chrom2",
"-c2",
required=True,
help="chrom2 field number",
type=int,
callback=_validate_fieldnum,
)
@click.option(
"--pos1",
"-p1",
required=True,
help="pos1 field number",
type=int,
callback=_validate_fieldnum,
)
@click.option(
"--pos2",
"-p2",
required=True,
help="pos2 field number",
type=int,
callback=_validate_fieldnum,
)
@click.option(
"--index",
"-i",
help="Select the preset sort and indexing options",
type=click.Choice(["tabix", "pairix"]),
default="pairix",
show_default=True,
)
@click.option(
"--flip-only",
help="Only flip mates; no sorting or indexing. Write to stdout.",
is_flag=True,
default=False,
show_default=True,
)
@click.option(
"--nproc", "-p",
help="Number of processors",
type=int,
default=8,
show_default=True
)
@click.option(
"--zero-based",
"-0",
help="Read positions are zero-based",
is_flag=True,
default=False,
show_default=True,
)
@click.option(
"--sep",
help="Data delimiter in the input file",
type=str,
default=r"\t",
show_default=True,
)
@click.option(
"--comment-char",
help="Comment character to skip header",
type=str,
default="#",
show_default=True,
)
@click.option(
"--sort-options",
help="Quoted list of additional options to `sort` command",
type=str,
)
@click.option(
"--out", "-o",
help="Output gzip file"
)
@click.option(
"--strand1", "-s1",
help="strand1 field number (deprecated)",
type=int
)
@click.option(
"--strand2", "-s2",
help="strand2 field number (deprecated)",
type=int
)
def csort(
pairs_path,
chromosomes_path,
index,
chrom1,
chrom2,
pos1,
pos2,
flip_only,
nproc,
zero_based,
sep,
comment_char,
sort_options,
out,
**kwargs
):
"""
Sort and index a contact list.
Order the mates of each pair record so that all contacts are upper
triangular with respect to the chromosome ordering given by the chromosomes
file, sort contacts by genomic location, and index the resulting file.
PAIRS_PATH : Contacts (i.e. read pairs) text file, optionally compressed.
CHROMOSOMES_PATH : File listing desired chromosomes in the desired order.
May be tab-delimited, e.g. a UCSC-style chromsizes file. Contacts mapping to
other chromosomes will be discarded.
**Notes**
\b
- csort can also be used to sort and index a text representation of
a contact *matrix* in bedGraph-like format. In this case, substitute
`pos1` and `pos2` with `start1` and `start2`, respectively.
- Requires Unix tools: sort, bgzip + tabix or pairix.
If indexing with Tabix, the output file will have the following properties:
\b
- Upper triangular: the read pairs on each row are assigned to side 1 or 2
in such a way that (chrom1, pos1) is always "less than" (chrom2, pos2)
- Rows are lexicographically sorted by chrom1, pos1, chrom2, pos2;
i.e. "positionally sorted"
- Compressed with bgzip [*]
- Indexed using Tabix [*] on chrom1 and pos1.
If indexing with Pairix, the output file will have the following properties:
\b
- Upper triangular: the read pairs on each row are assigned to side 1 or 2
in such a way that (chrom1, pos1) is always "less than" (chrom2, pos2)
- Rows are lexicographically sorted by chrom1, chrom2, pos1, pos2; i.e.
"block sorted"
- Compressed with bgzip [*]
- Indexed using Pairix [+] on chrom1, chrom2 and pos1.
\b
[*] Tabix manpage: <http://www.htslib.org/doc/tabix.html>.
[+] Pairix on Github: <https://github.com/4dn-dcic/pairix>
"""
if os.name == "nt":
raise click.Abort(
'"cooler csort" does not work on Windows. To ingest unsorted pairs '
'data, see the "cooler cload pairs" command.'
)
from signal import SIG_DFL, SIGPIPE, signal
signal(SIGPIPE, SIG_DFL)
# Check for required Unix tools
for tool in ["sort", "bgzip"] + [index]:
if not cmd_exists(tool):
print(f"Command {tool} not found", file=sys.stderr)
sys.exit(1)
# If output path is not given, produce output path by stripping any .txt,
# .gz or .txt.gz extension from the input path and appending .sorted[.txt].gz
infile = pairs_path
if out is None:
if infile == "-" and not flip_only:
logger.error("Output name required when input is stdin")
raise click.Abort
prefix = infile
ext = ".gz"
if prefix.endswith(".gz"):
prefix = op.splitext(prefix)[0]
if prefix.endswith(".txt"):
prefix = op.splitext(prefix)[0]
ext = ".txt.gz"
if index == "pairix":
sort_style = ".blksrt"
else:
sort_style = ".possrt"
outfile = prefix + sort_style + ext
else:
outfile = out
# Parse extra sort options and determine if sort supports --parallel option
if sort_options is not None:
sort_options = shlex.split(sort_options)
elif _has_parallel_sort():
sort_options = [f"--parallel={nproc}", "--buffer-size=50%"]
else:
sort_options = []
# 1-based column numbers
fields = {"C1": chrom1, "P1": pos1, "C2": chrom2, "P2": pos2}
# build commands
read_cmd = make_read_command(infile)
flip_cmd = make_flip_command(chromosomes_path, sep, comment_char, fields)
if flip_only:
# run pipeline
logger.info("Reordering pair mates...")
pipeline = []
logger.debug(" ".join(read_cmd))
pipeline.append(
subprocess.Popen(
read_cmd,
stdin=sys.stdin if infile == "-" else None,
stdout=subprocess.PIPE,
)
)
logger.debug(" ".join(flip_cmd))
pipeline.append(
subprocess.Popen(flip_cmd, stdin=pipeline[-1].stdout, stdout=sys.stdout)
)
for p in pipeline[::-1]:
p.communicate()
if p.returncode != 0:
sys.exit(1)
else:
sort_cmd = make_sort_command(index, fields, sort_options)
write_cmd = ["bgzip", "-c"]
index_cmd = make_index_command(index, fields, zero_based, outfile)
# run pipeline
logger.info(f"Input: '{infile}'")
logger.info(f"Output: '{outfile}'")
assert infile != outfile
with open(outfile, "wb") as fout:
pipeline = []
logger.debug(" ".join(read_cmd))
pipeline.append(
subprocess.Popen(
read_cmd,
stdin=sys.stdin if infile == "-" else None,
stdout=subprocess.PIPE,
)
)
logger.info("Reordering pair mates and sorting pair records...")
logger.debug(" ".join(flip_cmd))
pipeline.append(
subprocess.Popen(
flip_cmd, stdin=pipeline[-1].stdout, stdout=subprocess.PIPE
)
)
if index == "pairix":
logger.info("Sort order: block (chrom1, chrom2, pos1, pos2)")
else:
logger.info("Sort order: positional (chrom1, pos1, chrom2, pos2)")
logger.info(" ".join(sort_cmd))
pipeline.append(
subprocess.Popen(
sort_cmd, stdin=pipeline[-1].stdout, stdout=subprocess.PIPE
)
)
logger.debug(" ".join(write_cmd))
pipeline.append(
subprocess.Popen(write_cmd, stdin=pipeline[-1].stdout, stdout=fout)
)
for p in pipeline[::-1]:
p.communicate()
if p.returncode != 0:
logger.error(" ".join(p.args))
sys.exit(1)
# Create index file
logger.info("Indexing...")
logger.info(f"Indexer: {index}")
logger.info(" ".join(index_cmd))
p = subprocess.Popen(index_cmd)
p.communicate()
if p.returncode != 0:
sys.exit(1)
|