|
| 1 | +/* eslint-disable no-return-await */ |
| 2 | +const _ = require('lodash'); |
| 3 | +const { parseVariant, jsonifyVariant } = require('@bcgsc-pori/graphkb-parser'); |
| 4 | +const { util: { looksLikeRID } } = require('@bcgsc-pori/graphkb-schema'); |
| 5 | + |
| 6 | +const { create, select } = require('../commands'); |
| 7 | +const { parse } = require('../query_builder/index'); |
| 8 | +const { |
| 9 | + NoRecordFoundError, |
| 10 | + RecordConflictError, |
| 11 | + ValidationError, |
| 12 | +} = require('../error'); |
| 13 | + |
| 14 | +/** |
| 15 | + * Given a reference displayName from user input, format and |
| 16 | + * make sure any chromosomes is represented in a GraphKB-compatible way |
| 17 | + * |
| 18 | + * @param {string} ref the reference displayName |
| 19 | + * @returns {string} the formatted displayName |
| 20 | + */ |
| 21 | +const formatReferenceDisplayName = (ref) => { |
| 22 | + let match; |
| 23 | + |
| 24 | + // Chromosome reference handling |
| 25 | + if ((match = ref.match(/^CHR\d{1,2}$/i))) return ref.toLowerCase(); |
| 26 | + if ((match = ref.match(/^\d{1,2}$/i))) return `chr${ref}`; |
| 27 | + if ((match = ref.match(/^(MT|X|Y)$/i))) return ref.toLowerCase(); |
| 28 | + if ((match = ref.match(/^CHR(MT|X|Y)$/i))) return match[1].toLowerCase(); |
| 29 | + if ((match = ref.match(/^NC_0*(\d+)(?:\.\d+)?$/i))) return `chr${match[1]}`; // RefSeq NC_ |
| 30 | + |
| 31 | + // default, all caps for all non-chromosome references |
| 32 | + return ref.toUpperCase(); |
| 33 | +}; |
| 34 | + |
| 35 | +/** |
| 36 | + * Given a Vocabulary name as variant's type, returns its RID |
| 37 | + * |
| 38 | + * @param {Object} session the DB session object |
| 39 | + * @param {string} opt.type the variant type name |
| 40 | + * @returns {string} the strignified record's RID |
| 41 | + */ |
| 42 | +const getTypeRID = async (session, type) => { |
| 43 | + const result = await select( |
| 44 | + session, |
| 45 | + parse({ |
| 46 | + filters: { name: type.toLowerCase() }, |
| 47 | + returnProperties: ['@rid'], |
| 48 | + target: 'Vocabulary', |
| 49 | + }), |
| 50 | + ); |
| 51 | + |
| 52 | + if (result.length === 0) { |
| 53 | + throw new NoRecordFoundError({ |
| 54 | + message: `Vocabulary record ${type} dosen't exists. Vocabulary ontology must be uploaded first`, |
| 55 | + }); |
| 56 | + } |
| 57 | + return String(result[0]['@rid']); |
| 58 | +}; |
| 59 | + |
| 60 | +/** |
| 61 | + * Given a Feature displayName (reference), returns its RID |
| 62 | + * |
| 63 | + * @param {Object} session the DB session object |
| 64 | + * @param {string} reference the reference displayName |
| 65 | + * @param {string} [opt.preferedRefSrcName] the name of the prefered reference source |
| 66 | + * @returns {string} the strignify record's RID |
| 67 | + */ |
| 68 | +const getReferenceRID = async (session, ref, { preferedRefSrcName } = {}) => { |
| 69 | + // Chromosome support |
| 70 | + const referenceDisplayName = formatReferenceDisplayName(ref); |
| 71 | + |
| 72 | + // Feature |
| 73 | + // Lookup on displayName for easier version support |
| 74 | + const result = await select( |
| 75 | + session, |
| 76 | + parse({ |
| 77 | + filters: { displayName: referenceDisplayName }, |
| 78 | + returnProperties: ['@rid', 'source.name'], |
| 79 | + target: 'Feature', |
| 80 | + }), |
| 81 | + ); |
| 82 | + |
| 83 | + if (result.length === 0) { |
| 84 | + // TODO: Add support for new Feature upload |
| 85 | + throw new NoRecordFoundError({ |
| 86 | + message: `Feature record ${referenceDisplayName} dosen't exists`, |
| 87 | + }); |
| 88 | + } |
| 89 | + if (result.length > 1) { |
| 90 | + // pick the one from prefered source |
| 91 | + if (preferedRefSrcName) { |
| 92 | + for (const r of result) { |
| 93 | + if (r.source.name === preferedRefSrcName) { |
| 94 | + return String(r['@rid']); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + // defaults to 1st one |
| 100 | + return String(result[0]['@rid']); |
| 101 | +}; |
| 102 | + |
| 103 | +/** |
| 104 | + * Extract and format content from an HGVS-like variant notation. |
| 105 | + * Type and references are replaced by their RIDs. |
| 106 | + * |
| 107 | + * @param {Object} session the DB session object |
| 108 | + * @param {string} notation the variant notation |
| 109 | + * @param {string} [opt.preferedRefSrcName] the name of the prefered reference source |
| 110 | + * @returns {object} the upload content of a PositionalVariant |
| 111 | + */ |
| 112 | +const getContent = async (session, notation, { preferedRefSrcName } = {}) => { |
| 113 | + // Validation |
| 114 | + if (typeof notation !== 'string' || notation.trim().length === 0) { |
| 115 | + throw new ValidationError( |
| 116 | + { message: 'notation is required and must be a non-empty string' }, |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + // Notation parsing |
| 121 | + const content = jsonifyVariant( |
| 122 | + parseVariant(notation), |
| 123 | + ); |
| 124 | + |
| 125 | + // Replacing variant type by its RID |
| 126 | + content.type = await getTypeRID(session, content.type); |
| 127 | + |
| 128 | + // Replacing variant references by their RIDs |
| 129 | + content.reference1 = await getReferenceRID( |
| 130 | + session, |
| 131 | + content.reference1, |
| 132 | + { preferedRefSrcName }, |
| 133 | + ); |
| 134 | + |
| 135 | + if (content.reference2) { |
| 136 | + content.reference2 = await getReferenceRID( |
| 137 | + session, |
| 138 | + content.reference2, |
| 139 | + { preferedRefSrcName }, |
| 140 | + ); |
| 141 | + } |
| 142 | + |
| 143 | + return content; |
| 144 | +}; |
| 145 | + |
| 146 | +/** |
| 147 | + * Given the upload content for a new PositionalVariant, |
| 148 | + * get the filters object for a select command |
| 149 | + * |
| 150 | + * @param {Object} content initially used to attempt creating a new PositionalVariant |
| 151 | + * @returns {object} the filters object |
| 152 | + */ |
| 153 | +const positionalVariantQueryFilters = (content) => ({ |
| 154 | + AND: [ |
| 155 | + 'break1Repr', |
| 156 | + 'break2Repr', |
| 157 | + 'reference1', |
| 158 | + 'reference2', |
| 159 | + 'refSeq', |
| 160 | + 'truncation', |
| 161 | + 'type', |
| 162 | + 'untemplatedSeq', |
| 163 | + 'untemplatedSeqSize', |
| 164 | + ] |
| 165 | + .filter((prop) => prop in content) |
| 166 | + .map((prop) => ({ [prop]: content[prop] })), |
| 167 | +}); |
| 168 | + |
| 169 | +/** |
| 170 | + * Upload a new PositionalVariant based on content |
| 171 | + * Will attempt to replace type and references by RIDs if needed |
| 172 | + * Existing record can optionally (default) be fetched and returned |
| 173 | + * |
| 174 | + * @param {Object} session the DB session object |
| 175 | + * @param {Object} user the user object |
| 176 | + * @param {Object} content the content's payload |
| 177 | + * @param {boolean} [opt.existsOk=true] to return existing record |
| 178 | + * @param {string} [opt.preferedRefSrcName] the name of the prefered reference source |
| 179 | + * @returns {[Record<string, any>, string]} Array containing: |
| 180 | + * - first element: the record object |
| 181 | + * - second element: the status name |
| 182 | + */ |
| 183 | +const uploadPositionalVariant = async (session, user, content, { |
| 184 | + existsOk = true, |
| 185 | + preferedRefSrcName, |
| 186 | +} = {}) => { |
| 187 | + const payload = _.cloneDeep(content); |
| 188 | + |
| 189 | + // Make sure type is an RID |
| 190 | + if (!looksLikeRID(payload.type, true)) { |
| 191 | + payload.type = await getTypeRID(session, payload.type); |
| 192 | + } |
| 193 | + // Make sure references are RIDs |
| 194 | + if (!looksLikeRID(payload.reference1, true)) { |
| 195 | + payload.reference1 = await getReferenceRID( |
| 196 | + session, |
| 197 | + payload.reference1, |
| 198 | + { preferedRefSrcName }, |
| 199 | + ); |
| 200 | + } |
| 201 | + if (payload.reference2 && !looksLikeRID(payload.reference2, true)) { |
| 202 | + payload.reference2 = await getReferenceRID( |
| 203 | + session, |
| 204 | + payload.reference2, |
| 205 | + { preferedRefSrcName }, |
| 206 | + ); |
| 207 | + } |
| 208 | + |
| 209 | + // Upload |
| 210 | + try { |
| 211 | + return [ |
| 212 | + await create(session, { |
| 213 | + content: payload, |
| 214 | + modelName: 'PositionalVariant', |
| 215 | + user, |
| 216 | + }), |
| 217 | + 'CREATED', |
| 218 | + ]; |
| 219 | + } catch (err) { |
| 220 | + // Return existing record |
| 221 | + if (err instanceof RecordConflictError && existsOk) { |
| 222 | + return [ |
| 223 | + ...await select( |
| 224 | + session, |
| 225 | + parse({ |
| 226 | + filters: positionalVariantQueryFilters(payload), |
| 227 | + target: 'PositionalVariant', |
| 228 | + }), |
| 229 | + { user }, |
| 230 | + ), |
| 231 | + 'OK', |
| 232 | + ]; |
| 233 | + } |
| 234 | + throw err; |
| 235 | + } |
| 236 | +}; |
| 237 | + |
| 238 | +module.exports = { |
| 239 | + formatReferenceDisplayName, |
| 240 | + getContent, |
| 241 | + getReferenceRID, |
| 242 | + getTypeRID, |
| 243 | + positionalVariantQueryFilters, |
| 244 | + uploadPositionalVariant, |
| 245 | +}; |
0 commit comments