Skip to content

Commit

Permalink
Add verbose option for fragments_to_bigwig
Browse files Browse the repository at this point in the history
  • Loading branch information
SeppeDeWinter committed Jan 29, 2024
1 parent 5a3f538 commit a84af92
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/scatac_fragment_tools/cli/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def command_fragment_to_bigwigs(args):
args.normalize,
args.scaling_factor,
args.cut_sites,
args.verbose
)

def command_split_fragments_by_cell_type(args):
Expand Down
8 changes: 8 additions & 0 deletions src/scatac_fragment_tools/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ def add_fragments_to_bigwig_subparser(
default=False,
help="Add chromosome prefix to each chromosome name found in the fragments file.",
)
parser.add_optional_argument(
"-v",
"--verbose",
dest = "verbose",
action = "store_true",
default = False,
help = "Whether to print progress.",
)
return parser.get_parser()


Expand Down
29 changes: 18 additions & 11 deletions src/scatac_fragment_tools/library/bigwig/fragments_to_bigwig.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,27 +310,32 @@ def fragments_to_bw(
normalize: bool = True,
scaling_factor: float = 1.0,
cut_sites: bool = False,
verbose: bool = False
):
chrom_arrays = {}

with pyBigWig.open(bw_filename, "wb") as bw:
print("Add chromosome sizes to bigWig header")
if verbose:
print("Add chromosome sizes to bigWig header")
bw.addHeader(list(chrom_sizes.items()))

for chrom, chrom_size in chrom_sizes.items():
chrom_arrays[chrom] = np.zeros(chrom_size, dtype=np.uint32)

n_fragments = 0

print(f"Number of fragments: {fragments_df.height}")
print("Split fragments df by chromosome")
if verbose:
print(f"Number of fragments: {fragments_df.height}")
print("Split fragments df by chromosome")
per_chrom_fragments_dfs = fragments_df.partition_by("Chromosome", as_dict=True)

print("Calculate depth per chromosome:")
if verbose:
print("Calculate depth per chromosome:")
for chrom in per_chrom_fragments_dfs:
print(f" - {chrom} ...")
if chrom not in chrom_sizes:
print(f" Skipping {chrom} as it is not in chrom sizes file.")
if verbose:
print(f" Skipping {chrom} as it is not in chrom sizes file.")
continue
starts, ends = (
per_chrom_fragments_dfs[chrom].select(["Start", "End"]).to_numpy().T
Expand All @@ -346,12 +351,13 @@ def fragments_to_bw(

# Calculate RPM scaling factor.
rpm_scaling_factor = n_fragments / 1_000_000.0

print(
"Compact depth array per chromosome (make ranges for consecutive the same values and remove zeros):"
)
if verbose:
print(
"Compact depth array per chromosome (make ranges for consecutive the same values and remove zeros):"
)
for chrom in chrom_sizes:
print(f" - Compact {chrom} ...")
if verbose:
print(f" - Compact {chrom} ...")
idx, values, lengths = collapse_consecutive_values(chrom_arrays[chrom])
non_zero_idx = np.flatnonzero(values)

Expand All @@ -371,5 +377,6 @@ def fragments_to_bw(
elif scaling_factor != 1.0:
values *= scaling_factor

print(f" - Write {chrom} to bigWig ...")
if verbose:
print(f" - Write {chrom} to bigWig ...")
bw.addEntries(chroms=chroms, starts=starts, ends=ends, values=values)

0 comments on commit a84af92

Please sign in to comment.