ggonnella/gfapy

Setting 'or' tag in merge_linear_paths

Opened this issue · 1 comments

W-L commented

Hi Giorgio,
Thanks for the great library!
I came across an issue when using merge_linear_paths with enable_tracking=True. Not sure if it's a general problem or maybe a version incompatibility? The 'or' tag of the merged segments is correctly created, but at the end of merging (within __create_merged_segment, line 329 overwriting the 'or' tag by joining the list causes an error with a traceback that I don't understand. If the tag is instead assigned to a new name it works as expected. If it's helpful I attached a gfa file with a single overlap that causes the error for me, but I suspect any would do in my case.
Any ideas what the problem is?
Thanks!

Traceback (most recent call last):
  File "XXX/lib/python3.9/site-packages/gfapy/field/json.py", line 18, in validate_encoded
    json.loads(string)
  File "XXX/lib/python3.9/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "XXX/lib/python3.9/json/decoder.py", line 340, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 3 (char 2)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "XXX/lib/python3.9/site-packages/gfapy/field/writer.py", line 61, in _to_gfa_field
    return mod.encode(obj)
  File "XXX/lib/python3.9/site-packages/gfapy/field/json.py", line 42, in encode
    validate_encoded(obj)
  File "XXX/lib/python3.9/site-packages/gfapy/field/json.py", line 20, in validate_encoded
    raise Exception(
AttributeError: 'Exception' object has no attribute 'format'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "XXX/lib/python3.9/site-packages/gfapy/line/common/writer.py", line 58, in to_list
    fstr = self.field_to_s(fn, tag = True)
  File "XXX/lib/python3.9/site-packages/gfapy/line/common/writer.py", line 103, in field_to_s
    return gfapy.Field._to_gfa_tag(v, fieldname, datatype = t, line = self)
  File "XXX/lib/python3.9/site-packages/gfapy/field/writer.py", line 93, in _to_gfa_tag
    Writer._to_gfa_field(obj, datatype = datatype,
  File "XXX/lib/python3.9/site-packages/gfapy/field/writer.py", line 68, in _to_gfa_field
    raise err.__class__(
AttributeError: Field: or
Datatype: J
Content: '43a47abf-4c9c-470e-833b-b01524a996eb,aa603d54-b794-4fc8-a0f3-ea22497181b3'
'Exception' object has no attribute 'format'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "XXX/lib/python3.9/site-packages/gfapy/field/parser.py", line 61, in _parse_gfa_field
    return mod.decode(string)
  File "XXX/lib/python3.9/site-packages/gfapy/field/json.py", line 10, in decode
    return unsafe_decode(string)
  File "XXX/lib/python3.9/site-packages/gfapy/field/json.py", line 6, in unsafe_decode
    return json.loads(string)
  File "XXX/lib/python3.9/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "XXX/lib/python3.9/json/decoder.py", line 340, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 3 (char 2)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "XXX/lib/python3.9/site-packages/gfapy/gfa.py", line 246, in to_file
    f.write(str(line)+"\n")
  File "XXX/lib/python3.9/site-packages/gfapy/line/segment/writer_wo_sequence.py", line 16, in __str__
    return super().__str__()
  File "XXX/lib/python3.9/site-packages/gfapy/line/common/writer.py", line 12, in __str__
    return gfapy.Line.SEPARATOR.join(self.to_list())
  File "XXX/lib/python3.9/site-packages/gfapy/line/common/writer.py", line 60, in to_list
    fstr = str(self.get(fn))
  File "XXX/lib/python3.9/site-packages/gfapy/line/common/field_data.py", line 105, in get
    self._data[fieldname] = gfapy.Field._parse_gfa_field(v, t,
  File "XXX/lib/python3.9/site-packages/gfapy/field/parser.py", line 78, in _parse_gfa_field
    raise err.__class__(
TypeError: __init__() missing 2 required positional arguments: 'doc' and 'pos'

test.gfa.txt

Hi @W-L

thank you for finding this bug.
I think I find out, why the error arises. A JSON tag is created, since a list is assigned to it, but later, I tried to rewrite the tag as a string, failing since the datatype was J and the format incorrect for that datatype. Thus, I changed the datatype to Z manually before rewriting the content, and now it seems to work correctly.

I did not close the issue yet (or rather temporarily re-opened it), since I have not yet added a test for this and probably more checks would be better.