Skip to content

Commit

Permalink
fixed formatting with ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
fawaz-dabbaghieh committed Jun 20, 2024
1 parent 5adfe0f commit 1d880fc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 53 deletions.
37 changes: 10 additions & 27 deletions gaftools/cli/order_gfa.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import os
import logging
import time
from collections import namedtuple, defaultdict
from collections import defaultdict
from gaftools.gfa import GFA

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -49,8 +49,7 @@ def run_order_gfa(
chromosome_order=None,
with_sequence=False,
):

if not chromosome_order is None:
if chromosome_order is not None:
chromosome_order = chromosome_order.split(sep=",")

if not os.path.isdir(outdir):
Expand All @@ -67,7 +66,6 @@ def run_order_gfa(
logging.error(f"were not able to create directory {outdir}, OSError")
sys.exit()


logger.info(f"Reading {gfa_filename}")
if with_sequence:
graph = GFA(gfa_filename, low_memory=False)
Expand Down Expand Up @@ -114,8 +112,8 @@ def run_order_gfa(
# Initialize files
# f_gfa = open(outdir+'/'+gfa_filename.split("/")[-1][:-4]+'-'+chromosome+'.gfa', 'w')

scaffold_nodes, inside_nodes, node_order, bo, bubble_count = (
decompose_and_order(graph, component_nodes, chromosome, bo)
scaffold_nodes, inside_nodes, node_order, bo, bubble_count = decompose_and_order(
graph, component_nodes, chromosome, bo
)

# skip a chromosome if something went wrong
Expand All @@ -130,12 +128,7 @@ def run_order_gfa(
)
out_files.append(f_gfa)
f_colors = open(
outdir
+ os.sep
+ gfa_filename.split(os.sep)[-1][:-4]
+ "-"
+ chromosome
+ ".csv",
outdir + os.sep + gfa_filename.split(os.sep)[-1][:-4] + "-" + chromosome + ".csv",
"w",
)
f_colors.write("Name,Color,SN,SO,BO,NO\n")
Expand Down Expand Up @@ -163,9 +156,7 @@ def run_order_gfa(
else:
so_tag = "NA"
f_colors.write(
"{},{},{},{},{},{}\n".format(
node_name, color, sn_tag, so_tag, bo_tag, no_tag
)
"{},{},{},{},{},{}\n".format(node_name, color, sn_tag, so_tag, bo_tag, no_tag)
)

graph.write_gfa(
Expand All @@ -180,11 +171,7 @@ def run_order_gfa(
else:
logger.warning(f"Chromosome {chromosome} was skipped")
final_gfa = (
outdir
+ os.sep
+ gfa_filename.split(os.sep)[-1].split(".")[0]
+ "-complete"
+ ".gfa"
outdir + os.sep + gfa_filename.split(os.sep)[-1].split(".")[0] + "-complete" + ".gfa"
)
with open(final_gfa, "w") as outfile:
# outputting all the S lines first
Expand Down Expand Up @@ -251,12 +238,8 @@ def decompose_and_order(graph, component, component_name, bo_start=0):
logger.info(f" Scaffold graph: {len(scaffold_graph)} nodes")

# Find start/end points of the line by looking for nodes with degree 1
degree_one = [
x.id for x in scaffold_graph.nodes.values() if len(x.neighbors()) == 1
]
degree_two = [
x.id for x in scaffold_graph.nodes.values() if len(x.neighbors()) == 2
]
degree_one = [x.id for x in scaffold_graph.nodes.values() if len(x.neighbors()) == 1]
degree_two = [x.id for x in scaffold_graph.nodes.values() if len(x.neighbors()) == 2]

try:
assert len(degree_one) == 2
Expand Down Expand Up @@ -314,7 +297,7 @@ def count_sn(graph, comp):
"""
counts = defaultdict(int)
for n in comp:
if not "SN" in graph[n].tags:
if "SN" not in graph[n].tags:
continue
counts[graph[n].tags["SN"][1]] += 1
return counts
Expand Down
11 changes: 4 additions & 7 deletions gaftools/cli/realign.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from gaftools.timer import StageTimer
from gaftools.gaf import GAF
from gaftools.gfa import GFA
from pywfa.align import WavefrontAligner, cigartuples_to_str
from pywfa.align import WavefrontAligner


logger = logging.getLogger(__name__)
Expand All @@ -23,6 +23,7 @@ class PriorityAlignment:
"""
Simple data class to store the sequences and their priorities so the GAF output matches the GAf input
"""

priority: int
seq: str
field(compare=False)
Expand Down Expand Up @@ -76,9 +77,7 @@ def run_realign(gaf, graph, fasta, output=None, cores=1):
output = open(output, "w")

if cores > mp.cpu_count():
logger.warning(
"Number of cores requested is greater than the total number of CPU cores."
)
logger.warning("Number of cores requested is greater than the total number of CPU cores.")
cores = min(mp.cpu_count() - 1, cores)

# realign_gaf(gaf, graph, fasta, output, ext, cores)
Expand Down Expand Up @@ -214,9 +213,7 @@ def realign_gaf(gaf, graph, fasta, output, cores=1):
"One of the processes had a none-zero exit code. One reason could be that one of the processes consumed too much memory and was killed"
)
sys.exit(1)
if (
out_string_obj is None
): # sentinel counter to count finished processes
if out_string_obj is None: # sentinel counter to count finished processes
n_sentinels += 1
else: # priority queue to keep the output order same as input order
p_queue.put(out_string_obj)
Expand Down
37 changes: 18 additions & 19 deletions tests/test_run_realign.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,52 @@
Tests for 'gaftools realign'
"""


from gaftools.gaf import GAF
from gaftools.cli.realign import run_realign


def test_order_gfa(tmp_path):
#gaftools realign alignments.gaf smallgraph.gfa reads.fa
input_gaf = 'tests/data/alignments-graphaligner.gaf'
output_gaf = str(tmp_path) + '/output.gaf'
# gaftools realign alignments.gaf smallgraph.gfa reads.fa
input_gaf = "tests/data/alignments-graphaligner.gaf"
output_gaf = str(tmp_path) + "/output.gaf"
run_realign(
gaf = input_gaf,
graph = 'tests/data/smallgraph.gfa',
fasta = 'tests/data/reads.fa',
output = output_gaf,
cores=1
gaf=input_gaf,
graph="tests/data/smallgraph.gfa",
fasta="tests/data/reads.fa",
output=output_gaf,
cores=1,
)

gaf = GAF(output_gaf)
gaf_lines = list(gaf.read_file())
assert len(gaf_lines) == 2

assert gaf_lines[0].query_name == 'read_s8_s9'
assert gaf_lines[0].query_name == "read_s8_s9"
assert gaf_lines[0].query_length == 398
assert gaf_lines[0].query_start == 0
assert gaf_lines[0].query_end == 398
assert gaf_lines[0].strand == '+'
assert gaf_lines[0].path == '>s8>s9'
assert gaf_lines[0].strand == "+"
assert gaf_lines[0].path == ">s8>s9"
assert gaf_lines[0].path_length == 10109
assert gaf_lines[0].path_start == 6166
assert gaf_lines[0].path_end == 6564
assert gaf_lines[0].residue_matches == 398
assert gaf_lines[0].alignment_block_length == 398
assert gaf_lines[0].mapping_quality == 60
assert gaf_lines[0].cigar == '398='
#assert gaf_lines[0].is_primary
assert gaf_lines[0].cigar == "398="
# assert gaf_lines[0].is_primary

assert gaf_lines[1].query_name == 'read_s8_s9_deletion15'
assert gaf_lines[1].query_name == "read_s8_s9_deletion15"
assert gaf_lines[1].query_length == 383
assert gaf_lines[1].query_start == 0
assert gaf_lines[1].query_end == 383
assert gaf_lines[1].strand == '+'
assert gaf_lines[1].path == '>s8>s9'
assert gaf_lines[1].strand == "+"
assert gaf_lines[1].path == ">s8>s9"
assert gaf_lines[1].path_length == 10109
assert gaf_lines[1].path_start == 6166
assert gaf_lines[1].path_end == 6564
assert gaf_lines[1].residue_matches == 383
assert gaf_lines[1].alignment_block_length == 398
assert gaf_lines[1].mapping_quality == 60
assert gaf_lines[1].cigar == '189=15D194='
#assert gaf_lines[1].is_primary
assert gaf_lines[1].cigar == "189=15D194="
# assert gaf_lines[1].is_primary

0 comments on commit 1d880fc

Please sign in to comment.