Repository navigation
[BioSerDe-13] - Initial bed serde implementation - #1
Conversation
00c7958 to
88fa5bd
Compare
|
Making an update on the current state of this PR, decisions needed, and troubles found.
I've checked that json solves this using Read trait function like eat_char() and peek. But that would involve building the current Deserializer
But, since we also need to return a We would love to convert a noodles error into the SeqAccess error, but it seem like there is no way to do it, which points to the fact that we shouldn't be checking for this kind of error in this function, but we also cannot check later, so how to do it? |
88fa5bd to
2ea2769
Compare
There was a problem hiding this comment.
To answer some of the questions:
- Yes, I think a new PR might be good after closing this one.
- Tests are good, I think these are okay for now 🙂.
- I'm not sure exactly what you mean by this question, i.e. what do you mean by allowing for compatibility with the next formats? I think
from_readeris okay as noodles is the library that we are using to parse the input data itself. - It would be good to be able to use noodles as much as possible to do the parsing, rather than doing it manually or implementing it. I'm not sure how easy this is to accomplish though. It seems like noodles has FromStr implemented for struct fields/data types.
- See comment below.
Some additional thoughts:
- Is it necessary to manually implement
Deserialize? Shouldn't the derive macro do this for us? I think the main component that needs to be implemented isDeserializer, which maps the input into serde's data model. @multimeric @brainstorm, does that sound right?
Let me know if this helps, or if you have any other questions/concerns.
What I mean is that it feels like it could be confusing that in the same You can imagine that maybe an unitialized user using this functionality at some point may use
This is just my very convoluted way of saying that the second "keyword like" format we introduce in I will answer the other comments shortly, just testing some things about them before answering |
|
I don't think there should be two different ways of representing a For example, considering second test, to convert
It might be a little bit confusing to think about this for Let me know if this makes sense, or if I can elaborate on anything. |
Wow, I hadn't noticed that. However, they are using the bed format definition. However, I think you are onto something, in the spirit of your last comment, maybe we can just use the One thing that makes me apprehensive is that maybe we can get too tangled in external implementations. Maybe we will need to have our custom implementations in order to make optimizations in the future. But I am speaking out of ignorance, working on your suggestion will probably clarify some things out for me. |
6cc2c69 to
c900009
Compare
|
I think using serde-json will be a good idea for now. I believe one of the goals of Serde is to be as efficient and performant as possible, and I'm sure serde-json follows that reasoning (see here). Also, by using existing implementations, we can focus on just the unimplemented formats, utilizing work done by other authors. |
I think I stumbled at some problems with that, at least for the bed format:
I stored this experiment on the extravagantly named ERASEME: showcasing derive for Deserialize, I will leave it there if it is useful to someone reviewing, and remove it before merging. |
Actually I am a bit lost, could you give me an idea of how such a test would be made? To me it seems like we can test the high abstraction function (such as
I think I am understanding that we are thinking about a way to deal with both formats in relatively the same way. What I don't understand is how this would seem to the final user. Would there be functions to deal with the regular bed format and others to our convention? I understood that the If you could give me some examples of how these changes would show up to the user, that would be great (: |
mmalenic
left a comment
There was a problem hiding this comment.
Check out the Serde derive attributes. This allows for a lot of added flexibility when using the derive macro, without implementing Deserialize or Serialize manually.
| use crate::{Record, reader::Reader, error::{Error, Result}}; | ||
|
|
||
| const FIELDS: &'static [&'static str] = &["chrom", "start", "end"]; | ||
| enum Field { Chrom, Start, End } |
There was a problem hiding this comment.
One approach to allow lowercase deserialisation is to use the rename_all container attribute. This works for serialisation too.
#[derive(Deserialize, Debug)]
#[serde(rename_all = "lowercase")]
enum Field { Chrom, Start, End }Note, fields can be renamed arbitrarily using the rename field attribute, or multiple valid names can be assigned using alias.
| VariantAccess, Visitor, | ||
| }; | ||
|
|
||
| use crate::{Record, reader::Reader, error::{Error, Result}}; |
There was a problem hiding this comment.
bed::Record can be changed in record.rs.
noodles/noodles-bed/src/record.rs
Lines 111 to 116 in 07bd3d0
For example, to ignore the two extra structs, use the flatten field attribute. This means that during serialisation/deserialisation, standard_fields and optional_fields won't be present. See the example here.
/// A BED record.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct Record<const N: u8> {
#[serde(flatten)]
standard_fields: StandardFields,
#[serde(flatten)]
optional_fields: OptionalFields,
}There was a problem hiding this comment.
To me it seems like we can test the high abstraction function (such as from_str)
Yep, that's what I meant. Testing public, or user-facing functions (such as from_str) is okay for now. A test could test the visitor functions if we wanted to be sure of some behaviour, but I don't think that's necessary for now.
What I don't understand is how this would seem to the final user. Would there be functions to deal with the regular bed format and others to our convention? I understood that the serde-transcode suggestion is to link both serializers to be able to convert from one into the other, but I don't understand the final pipeline, do we aim to always read and write into files as the bed format, but print and represent internally as the bioserde keyword like manner?
To the user, b"sq0\t7\t13\nsq0\t20\t34\n" would be deserialized into a bed::Record using the Deserializer implemented in this PR, whereas r#"{"chrom":"sq0","start":8,"end":13}"# would be deserialized into a bed::Record using serde_json. The bed::Record struct stays the same, and what changes is the Deserializer. Since the first representation is the standard BED file format, it would use the Deserializer implemented here, whereas the second representation is a JSON representation of BED, so it would use the serde_json Deserializer.
Serialization is similar; a bed::Record would be serialized using the Serializer implemented here if the user wants the output to be in the standard BED file format, and it would use the serde_json Serializer if they want the output to be in the JSON representation of BED.
Note, in all these cases Serialize and Deserialize don't need to be implemented manually, as these traits are used for mapping between the Serde data model and the struct itself, not the input and output files. Also, the Deserializer and Serializer would not be used directly. Instead, convenience functions would be used. On the serde_json side, these could be from_str or to_string. Similar functions can be written here for the standard BED file format (e.g.b"sq0\t7\t13\nsq0\t20\t34\n").
Let me know if that makes sense.
I'll write up some examples as well, to illustrate this process.
There was a problem hiding this comment.
Check out some of the changes in the branch I made. This might help clarify how the Deserialization and Serialization will look to the user.
Also, have a look at the rust-csv Deserializer. BED is like a very restricted form of CSV, so the CSV Deserializer and Serializer might useful. Notably, have a look at how sequences are handled when the Deserializer encounters a struct or a sequence.
With regards to manually implementing Deserialize and Serialize, you might be right. We might indeed have to implement parts of it manually, as the definitions of bed::Record are quite complicated in noodles. There could be ways to avoid this, such as using the Serde try_from attribute with an intermediary struct or using the Serde with attributes. There's also extension crates for Serde's derive macros, for example serde_with or serde_state, which may provide a solution.
Initially I thought that the derive macros alone would be enough to implement Serialize and Deserialize, however, bed::Record doesn't match the underlying record representation closesly, making it more challenging to use. Although, I do still think we should try and avoid manually implementing Serialize and Deserialize if possible.
Let me know if this makes sense, or any other questions you have.
Twin PR on BioSerDe showcasing usage of this branch: umccr/BioSerDe#14
TODO:
--> solved by:
let result: Vec<Record::<3>> = from_str(input).unwrap();--> doesn't work if string has whitespaces, as described by:
-->
test de::serde_tests::test_bed_deserialization_with_whitespaces ... ignored"{\"chrom\":\"chr7\"...")-->
Displaydoesn't display backslashes, it'sDebugthat does