From c8821b9a28d2aa46e8fd4bf6c40cbd8a41549cd2 Mon Sep 17 00:00:00 2001 From: Thomas Rich Date: Tue, 16 Aug 2022 13:20:44 -0700 Subject: [PATCH] updating tidyUpAnnotation.js to auto-parse annotation.notes into JSON and gracefully handle errors --- src/tidyUpAnnotation.js | 11 +++++++++++ src/tidyUpSequenceData.test.js | 27 +++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/tidyUpAnnotation.js b/src/tidyUpAnnotation.js index 7a39cff..325525d 100644 --- a/src/tidyUpAnnotation.js +++ b/src/tidyUpAnnotation.js @@ -109,6 +109,17 @@ module.exports = function tidyUpAnnotation( ); annotation.type = "misc_feature"; } + if (annotation.notes && typeof annotation.notes === "string") { + try { + annotation.notes = JSON.parse(annotation.notes); + } catch (error) { + console.info( + `warning 33y00a0912 - couldn't parse notes for ${annotation.name || + ""} ${annotation.notes}:`, + error + ); + } + } if (!annotation.color) { annotation.color = featureColors[annotation.type]; diff --git a/src/tidyUpSequenceData.test.js b/src/tidyUpSequenceData.test.js index bee19e8..460f192 100644 --- a/src/tidyUpSequenceData.test.js +++ b/src/tidyUpSequenceData.test.js @@ -211,6 +211,33 @@ describe("tidyUpSequenceData", function() { }); res.features[0].type.should.equal("misc_feature"); }); + it("should try to auto-parse annotation.notes into JSON and gracefully handle errors", function() { + const res = tidyUpSequenceData({ + features: [ + { + start: 4, + end: 5, + notes: + '{"gene":["Ampicillin"],"note":["ORF frame 1"],"translation":["MSIQHFRVALIPFFAAFCLPVFAHPETLVKVKDAEDQLGARVGYIELDLNSGKILESFRPEERFPMMSTFKVLLCGAVLSRIDAGQEQLGRRIHYSQNDLVEYSPVTEKHLTDGMTVRELCSAAITMSDNTAANLLLTTIGGPKELTAFLHNMGDHVTRLDRWEPELNEAIPNDERDTTMPVAMATTLRKLLTGELLTLASRQQLIDWMEADKVAGPLLRSALPAGWFIADKSGAGERGSRGIIAALGPDGKPSRIVVIYTTGSQATMDERNRQIAEIGASLIKHW*"],"ApEinfo_fwdcolor":["pink"],"ApEinfo_revcolor":["pink"],"ApEinfo_graphicformat":["arrow_data {{0 1 2 0 0 -1} {} 0}"]}' + } + ] + }); + res.features[0].notes.gene[0].should.equal("Ampicillin"); + const res2 = tidyUpSequenceData({ + features: [ + { + start: 4, + end: 5, + //messed up JSON notes here: + notes: + '{"gene:["Ampicillin"],"note":["ORF frame 1"],"translation":["MSIQHFRVALIPFFAAFCLPVFAHPETLVKVKDAEDQLGARVGYIELDLNSGKILESFRPEERFPMMSTFKVLLCGAVLSRIDAGQEQLGRRIHYSQNDLVEYSPVTEKHLTDGMTVRELCSAAITMSDNTAANLLLTTIGGPKELTAFLHNMGDHVTRLDRWEPELNEAIPNDERDTTMPVAMATTLRKLLTGELLTLASRQQLIDWMEADKVAGPLLRSALPAGWFIADKSGAGERGSRGIIAALGPDGKPSRIVVIYTTGSQATMDERNRQIAEIGASLIKHW*"],"ApEinfo_fwdcolor":["pink"],"ApEinfo_revcolor":["pink"],"ApEinfo_graphicformat":["arrow_data {{0 1 2 0 0 -1} {} 0}"]}' + } + ] + }); + res2.features[0].notes.should.equal( + '{"gene:["Ampicillin"],"note":["ORF frame 1"],"translation":["MSIQHFRVALIPFFAAFCLPVFAHPETLVKVKDAEDQLGARVGYIELDLNSGKILESFRPEERFPMMSTFKVLLCGAVLSRIDAGQEQLGRRIHYSQNDLVEYSPVTEKHLTDGMTVRELCSAAITMSDNTAANLLLTTIGGPKELTAFLHNMGDHVTRLDRWEPELNEAIPNDERDTTMPVAMATTLRKLLTGELLTLASRQQLIDWMEADKVAGPLLRSALPAGWFIADKSGAGERGSRGIIAALGPDGKPSRIVVIYTTGSQATMDERNRQIAEIGASLIKHW*"],"ApEinfo_fwdcolor":["pink"],"ApEinfo_revcolor":["pink"],"ApEinfo_graphicformat":["arrow_data {{0 1 2 0 0 -1} {} 0}"]}' + ); + }); it("should add feature type = misc_feature if an invalid type is provided", function() { const res = tidyUpSequenceData({ features: [{ start: 4, end: 5, type: "idontexist" }]