@@ -21,6 +21,7 @@ const EXCLUDE_SECTIONS = new Set([
2121 'ReportUserFilter' ,
2222 'users' ,
2323 'kbMatchedStatements' ,
24+ 'observedVariantAnnotations' ,
2425] ) ;
2526
2627/**
@@ -126,6 +127,42 @@ const createStatementMatching = async (reportId, content, createdKbMatches, tran
126127 }
127128} ;
128129
130+ /**
131+ * Creates a new section for the report with the provided data
132+ *
133+ * @param {Number } reportId - The id of the report this section belongs to
134+ * @param {Array|Object } sectionContent - The record or records to be created for this section
135+ * @param {object } options - Options for creating report sections
136+ * @property {object } options.transaction - Transaction to run bulkCreate under
137+ *
138+ * @returns {undefined }
139+ */
140+ const createReportObservedVariantAnnotationSection = async ( reportId , sectionContent , options = { } ) => {
141+ const records = Array . isArray ( sectionContent )
142+ ? sectionContent
143+ : [ sectionContent ] ;
144+ const retvals = [ ] ;
145+ try {
146+ for ( const record of records ) {
147+ const annotationData = {
148+ variantType : record . variantType ,
149+ variantId : record . variantId ,
150+ annotations : record . annotations ,
151+ } ;
152+
153+ const annotation = await db . models . observedVariantAnnotations . create ( {
154+ reportId,
155+ ...annotationData ,
156+ } , options ) ;
157+ annotation . dataValues . variant = record . variant ;
158+ retvals . push ( annotation . dataValues ) ;
159+ }
160+ return retvals ;
161+ } catch ( error ) {
162+ throw new Error ( `Unable to create section observedVariantAnnotation: ${ error . message || error } ` ) ;
163+ }
164+ } ;
165+
129166/**
130167 * Given the content for a report to be created, pull gene names from
131168 * the variant sections and create the genes which will be used as
@@ -237,10 +274,8 @@ const createReportVariantsSection = async (reportId, genesRecordsByName, modelNa
237274 keyCheck . add ( key ) ;
238275 }
239276 }
240-
241277 try {
242278 if ( modelName === 'structuralVariants' ) {
243- // add the gene FK associations
244279 records = await db . models [ modelName ] . bulkCreate ( sectionContent . map ( ( {
245280 key, gene1, gene2, ...newEntry
246281 } ) => {
@@ -252,7 +287,6 @@ const createReportVariantsSection = async (reportId, genesRecordsByName, modelNa
252287 } ;
253288 } ) , options ) ;
254289 } else {
255- // add the gene FK association
256290 records = await db . models [ modelName ] . bulkCreate ( sectionContent . map ( ( { key, gene, ...newEntry } ) => {
257291 return {
258292 ...newEntry ,
@@ -264,7 +298,6 @@ const createReportVariantsSection = async (reportId, genesRecordsByName, modelNa
264298 } catch ( error ) {
265299 throw new Error ( `Unable to create variant section (${ modelName } ) ${ error . message || error } ` ) ;
266300 }
267-
268301 const mapping = { } ;
269302 for ( let i = 0 ; i < sectionContent . length ; i ++ ) {
270303 if ( sectionContent [ i ] . key ) {
@@ -277,7 +310,6 @@ const createReportVariantsSection = async (reportId, genesRecordsByName, modelNa
277310const createReportVariantSections = async ( report , content , transaction ) => {
278311 // create the genes first since they will need to be linked to the variant records
279312 const geneDefns = await createReportGenes ( report , content , { transaction} ) ;
280-
281313 // create the variants and create a mapping from their input 'key' value to the new records
282314 const variantMapping = { } ;
283315 const variantPromises = Object . keys ( KB_PIVOT_MAPPING ) . map ( async ( variantType ) => {
@@ -287,16 +319,14 @@ const createReportVariantSections = async (report, content, transaction) => {
287319 if ( ! Array . isArray ( content [ variantModel ] ) && typeof content [ variantModel ] === 'object' ) {
288320 content [ variantModel ] = [ content [ variantModel ] ] ;
289321 }
290-
291322 const mapping = await createReportVariantsSection ( report . id , geneDefns , variantModel , content [ variantModel ] || [ ] , { transaction} ) ;
323+
292324 variantMapping [ variantType ] = mapping ;
293325 } ) ;
294-
295326 // create the probe results (linked to gene but not to kbMatches)
296327 variantPromises . push ( createReportVariantsSection ( report . id , geneDefns , 'probeResults' , content . probeResults || [ ] , { transaction} ) ) ;
297328
298329 await Promise . all ( variantPromises ) ;
299-
300330 // then the kb matches which must be linked to the variants
301331 const kbMatches = ( content . kbMatches || [ ] ) . map ( ( { variant, variantType, ...match } ) => {
302332 if ( variantMapping [ variantType ] === undefined ) {
@@ -313,6 +343,19 @@ const createReportVariantSections = async (report, content, transaction) => {
313343 const createdKbMatches = await createReportKbMatchSection ( report . id , 'kbMatches' , kbMatches , { transaction} ) ;
314344
315345 await createStatementMatching ( report . id , content , createdKbMatches , transaction ) ;
346+ // then the observed variant annotations which must be linked to the variants
347+
348+ const observedVariantAnnotations = ( content . observedVariantAnnotations || [ ] ) . map ( ( { variant, variantType, ...annotation } ) => {
349+ if ( variantMapping [ variantType ] === undefined ) {
350+ throw new Error ( `cannot link annotations to variant type ${ variantType } as none were specified` ) ;
351+ }
352+ if ( variantMapping [ variantType ] [ variant ] === undefined ) {
353+ throw new Error ( `invalid link (variant=${ variant } ) variant definition does not exist` ) ;
354+ }
355+ return { ...annotation , variantId : variantMapping [ variantType ] [ variant ] , variantType, variant} ;
356+ } ) ;
357+
358+ await createReportObservedVariantAnnotationSection ( report . id , observedVariantAnnotations , { transaction} ) ;
316359} ;
317360
318361/**
0 commit comments