|
| 1 | +/** |
| 2 | + * Migrations for sample data structure changes |
| 3 | + */ |
| 4 | + |
| 5 | +/** |
| 6 | + * Migrates sequence data from the old location to the new biology structure. |
| 7 | + * Old location: $content.general.sequence |
| 8 | + * New location: $content.biology.peptidic[0].seq[0].sequence |
| 9 | + * By default we expect it is a peptidic sequence. |
| 10 | + * |
| 11 | + * @param {object} sample - The sample object to migrate |
| 12 | + */ |
| 13 | +function migrateSequence(sample) { |
| 14 | + if (!sample.$content.general) { |
| 15 | + sample.$content.general = {}; |
| 16 | + } |
| 17 | + |
| 18 | + if (sample.$content.general.sequence) { |
| 19 | + // eslint-disable-next-line no-console |
| 20 | + console.log('Migrating sequence', sample.$content.general.sequence); |
| 21 | + if (!sample.$content.biology) sample.$content.biology = {}; |
| 22 | + if (!sample.$content.biology.peptidic) { |
| 23 | + sample.$content.biology.peptidic = []; |
| 24 | + } |
| 25 | + if (!sample.$content.biology.peptidic.length > 0) { |
| 26 | + sample.$content.biology.peptidic[0] = {}; |
| 27 | + } |
| 28 | + if (!sample.$content.biology.peptidic[0].seq) { |
| 29 | + sample.$content.biology.peptidic[0].seq = []; |
| 30 | + } |
| 31 | + if (!sample.$content.biology.peptidic[0].seq.length > 0) { |
| 32 | + sample.$content.biology.peptidic[0].seq[0] = {}; |
| 33 | + } |
| 34 | + sample.setChildSync( |
| 35 | + ['$content', 'biology', 'peptidic', 0, 'seq', 0, 'sequence'], |
| 36 | + sample.$content.general.sequence, |
| 37 | + ); |
| 38 | + sample.$content.general.sequence = undefined; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Migrates thermogravimetric analysis metadata from "zone" to "zones". |
| 44 | + * Old location: $content.spectra.thermogravimetricAnalysis[].meta.zone |
| 45 | + * New location: $content.spectra.thermogravimetricAnalysis[].meta.zones |
| 46 | + * |
| 47 | + * @param {object} sample - The sample object to migrate |
| 48 | + */ |
| 49 | +function migrateTGAZones(sample) { |
| 50 | + const tgaEntries = sample.$content?.spectra?.thermogravimetricAnalysis; |
| 51 | + if (!Array.isArray(tgaEntries)) return; |
| 52 | + |
| 53 | + for (const entry of tgaEntries) { |
| 54 | + if (entry.zone && !entry.zones) { |
| 55 | + // eslint-disable-next-line no-console |
| 56 | + console.log('Migrating TGA zone to zones'); |
| 57 | + entry.zones = entry.zone; |
| 58 | + delete entry.zone; |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Run all migrations on a sample |
| 65 | + * |
| 66 | + * @param {object} sample - The sample object to migrate |
| 67 | + */ |
| 68 | +function updateSample(sample) { |
| 69 | + migrateSequence(sample); |
| 70 | + migrateTGAZones(sample); |
| 71 | +} |
| 72 | + |
| 73 | +module.exports = { updateSample }; |
0 commit comments