Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reader class attributes immutable (Cannot edit "sample_id" field of mutable read object) #115

Closed
jennifergribble-nanopore opened this issue Feb 29, 2024 · 1 comment

Comments

@jennifergribble-nanopore
Copy link

jennifergribble-nanopore commented Feb 29, 2024

Issue Description

I have installed the pod5 package via pip and were attempting to use it via a custom Python script as referenced in this issue:

https://github.com/nanoporetech/pod5-file-format/issues/100

to edit the sample_id field. The error is:

Exception has occurred: FrozenInstanceError
cannot assign to field 'sample_id'
File "/data/TS_data_analysis/jgribble/abrf_methylation/pod5_rename/pod5_rename_test.py", line 12, in
read.run_info.sample_id = str('NA24385_MGI_Lib1_New_flowcell')
^^^^^^^^^^^^^^^^^^^^^^^
dataclasses.FrozenInstanceError: cannot assign to field 'sample_id'

To my understanding, this appears that the following code block does not create a mutable RunInfo attribute? Is this intended or should users be able to edit RunInfo fields using this package?

import pod5

# New output file for edited data
with pod5.Writer("output.pod5") as writer:
    # Read all records
    with pod5.Reader("input.pod5") as reader:
          # Iterate over immutable ReadRecords
          for record in reader:
               # Convert to mutable Read
               read = record.to_read()
               # Edit the value
               read.run_info.sample_id = str('NA24385_MGI_Lib1_New_flowcell')

Specifications

  • Pod5 Version: 0.3.6
  • Python Version: 3.11
  • Platform: conda env on Linux (Ubuntu 20.04)
@HalfPhoton
Copy link
Collaborator

HalfPhoton commented Mar 1, 2024

Hi @jennifergribble-nanopore,
One must assign a new RunInfo instance to replace the existing immutable one.

from copy import deepcopy
run_info = vars(deepcopy(read.run_info))
run_info["sample_id"] = str('NA24385_MGI_Lib1_New_flowcell')
read.run_info = pod5.RunInfo(**run_info)

Kind regards,
Rich

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants