Skip to content

Commit

Permalink
adding an exception case to get_path
Browse files Browse the repository at this point in the history
For the GRCh38 paths in the CHM13-based graph of HPRC, it does not form a path. Hence conversion now uses an exception case to not throw this warning.
  • Loading branch information
samarendra-pani committed Aug 16, 2024
1 parent c84f1b9 commit feaf7f4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
5 changes: 2 additions & 3 deletions gaftools/cli/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ def run(gaf_path, gfa=None, output=None, index=None, nodes=[], regions=[], forma
}
ref_contig = [contig for contig in gfa_file.contigs if gfa_file.contigs[contig] == 0]
for contig in gfa_file.contigs:
contig_len[contig] = gfa_file.get_contig_length(contig)
print(contig_len)
contig_len[contig] = gfa_file.get_contig_length(contig, throw_warning=False)
del gfa_file
else:
assert format == "unstable"
Expand All @@ -79,7 +78,7 @@ def run(gaf_path, gfa=None, output=None, index=None, nodes=[], regions=[], forma
gfa_file = GFA(graph_file=gfa, low_memory=True)
contigs = list(gfa_file.contigs.keys())
for contig in contigs:
path = gfa_file.get_path(contig)
path = gfa_file.get_path(contig, throw_warning=False)
for node in path:
reference[contig].append(gfa_file[node])
del gfa_file
Expand Down
23 changes: 16 additions & 7 deletions gaftools/gfa.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,10 @@ def return_gfa_path(self, list_of_nodes):

return ",".join(path)

def get_path(self, chrom):
# TODO: Need to deal with non-path contigs like GRCh38-based paths in the CHM13-based minigraph rGFAs of HPRC.
# The nodes have the same SN tag but do not form a path. Need to work with such cases.
# Currently just create an exception case to use.
def get_path(self, chrom, throw_warning=True):
"""
takes a chromosome name (matching the SN tag) and returns the path of that chromosome
"""
Expand All @@ -699,16 +702,22 @@ def get_path(self, chrom):
if self.list_is_path(sorted_nodes):
return sorted_nodes
else:
logging.warning(
f"The sorted nodes with SN tag {chrom} did not create a linear path. Stopping! Returning empty list"
)
return list()
if throw_warning:
logging.warning(
f"The sorted nodes with SN tag {chrom} did not create a linear path. Stopping! Returning empty list"
)
return list()
else:
logging.warning(
f"The sorted nodes with SN tag {chrom} did not create a linear path. The sorted node list is returned for conversion."
)
return sorted_nodes

def get_contig_length(self, chrom):
def get_contig_length(self, chrom, throw_warning=True):
"""
returns the length of the chromosome or contig name
"""
sorted_nodes = self.get_path(chrom)
sorted_nodes = self.get_path(chrom, throw_warning)
if not sorted_nodes:
logging.error(
"Was not able to return the length of the chromosome, check warning message(s)"
Expand Down

0 comments on commit feaf7f4

Please sign in to comment.