|
| 1 | +import { hectarCollection } from '/imports/api/genes/hectar/hectarCollection.js'; |
| 2 | +import jobQueue, { Job } from '/imports/api/jobqueue/jobqueue.js'; |
| 3 | +import { ValidatedMethod } from 'meteor/mdg:validated-method'; |
| 4 | +import { Genes } from '/imports/api/genes/geneCollection.js'; |
| 5 | +import logger from '/imports/api/util/logger.js'; |
| 6 | +import { Roles } from 'meteor/alanning:roles'; |
| 7 | +import SimpleSchema from 'simpl-schema'; |
| 8 | +import { Meteor } from 'meteor/meteor'; |
| 9 | + |
| 10 | +class HectarProcessor { |
| 11 | + constructor() { |
| 12 | + // Not a bulk mongo suite. |
| 13 | + this.genesDb = Genes.rawCollection(); |
| 14 | + this.nHectar = 0; |
| 15 | + } |
| 16 | + |
| 17 | + /** |
| 18 | + * Function that returns the total number of insertions or updates in the |
| 19 | + * hectar collection. |
| 20 | + * @function |
| 21 | + * @return {Number} Return the total number of insertions or updates of |
| 22 | + * hectar. |
| 23 | + */ |
| 24 | + getNumberHectar() { |
| 25 | + return this.nHectar; |
| 26 | + } |
| 27 | + |
| 28 | + parse = (line) => { |
| 29 | + if (!(line.slice(0,10) === 'protein id' || line.split('\t').length <= 1)) { |
| 30 | + // Get all hectar informations line by line and separated by tabs. |
| 31 | + const [ |
| 32 | + proteinId, |
| 33 | + predictedTargetingCategory, |
| 34 | + signalPeptideScore, |
| 35 | + signalPeptideCleavageSite, |
| 36 | + typeIISignalAnchorScore, |
| 37 | + chloroplastScore, |
| 38 | + mitochondrionScore, |
| 39 | + otherScore, |
| 40 | + ] = line.split('\t'); |
| 41 | + |
| 42 | + // Organize data in a dictionary. |
| 43 | + const annotations = { |
| 44 | + protein_id: proteinId, |
| 45 | + predicted_targeting_category: predictedTargetingCategory, |
| 46 | + signal_peptide_score: signalPeptideScore, |
| 47 | + signal_peptide_cleavage_site: signalPeptideCleavageSite, |
| 48 | + typeII_signal_anchor_score: typeIISignalAnchorScore, |
| 49 | + chloroplast_score: chloroplastScore, |
| 50 | + mitochondrion_score: mitochondrionScore, |
| 51 | + other_score: otherScore, |
| 52 | + }; |
| 53 | + |
| 54 | + // Filters undefined data (with a dash) and splits into an array for |
| 55 | + // comma-separated data. |
| 56 | + for (const [key, value] of Object.entries(annotations)) { |
| 57 | + if (value[0] === '-') { |
| 58 | + annotations[key] = undefined; |
| 59 | + } |
| 60 | + if (value.indexOf(',') > -1) { |
| 61 | + annotations[key] = value.split(','); |
| 62 | + } |
| 63 | + } |
| 64 | + // If subfeatures is found in genes database (e.g: ID = |
| 65 | + // MMUCEDO_000002-T1). |
| 66 | + const subfeatureIsFound = Genes.findOne({ |
| 67 | + $or: [ |
| 68 | + { 'subfeatures.ID': proteinId }, |
| 69 | + { 'subfeatures.protein_id': proteinId }, |
| 70 | + ], |
| 71 | + }); |
| 72 | + |
| 73 | + if (typeof subfeatureIsFound !== 'undefined') { |
| 74 | + console.log("if loop" + typeof subfeatureIsFound); |
| 75 | + // Increment hectar. |
| 76 | + this.nHectar += 1; |
| 77 | + |
| 78 | + // Update or insert if no matching documents were found. |
| 79 | + const documentHectar = hectarCollection.upsert( |
| 80 | + { protein_id: proteinId }, // selector. |
| 81 | + annotations, // modifier. |
| 82 | + ); |
| 83 | + |
| 84 | + // Update hectarId in genes database. |
| 85 | + if (typeof documentHectar.insertedId !== 'undefined') { |
| 86 | + // Hectar _id is created. |
| 87 | + return this.genesDb.update({ |
| 88 | + $or: [ |
| 89 | + { 'subfeatures.ID': proteinId }, |
| 90 | + { 'subfeatures.protein_id': proteinId }, |
| 91 | + ]}, |
| 92 | + { $set: { hectarId: documentHectar.insertedId } }, |
| 93 | + ); |
| 94 | + } else { |
| 95 | + // Hectar already exists. |
| 96 | + const hectarIdentifiant = hectarCollection.findOne({ protein_id: proteinId })._id; |
| 97 | + return this.genesDb.update( |
| 98 | + { $or: [{'subfeatures.ID': proteinId}, {'subfeatures.protein_id': proteinId}] }, |
| 99 | + { $set: { hectarId: hectarIdentifiant } }, |
| 100 | + ); |
| 101 | + } |
| 102 | + } else { |
| 103 | + logger.warn(` |
| 104 | +Warning ! ${proteinId} hectar annotation did |
| 105 | +not find a matching protein domain in the genes database. |
| 106 | +${proteinId} is not added to the hectar database.`); |
| 107 | + } |
| 108 | + } |
| 109 | + }; |
| 110 | +} |
| 111 | + |
| 112 | +const addHectar = new ValidatedMethod({ |
| 113 | + name: 'addHectar', |
| 114 | + validate: new SimpleSchema({ |
| 115 | + fileName: { type: String }, |
| 116 | + }).validator(), |
| 117 | + applyOptions: { |
| 118 | + noRetry: true, |
| 119 | + }, |
| 120 | + run({ fileName }) { |
| 121 | + if (!this.userId) { |
| 122 | + throw new Meteor.Error('not-authorized'); |
| 123 | + } |
| 124 | + if (!Roles.userIsInRole(this.userId, 'admin')) { |
| 125 | + throw new Meteor.Error('not-authorized'); |
| 126 | + } |
| 127 | + |
| 128 | + logger.log('file :', { fileName }); |
| 129 | + const job = new Job(jobQueue, 'addHectar', { fileName }); |
| 130 | + const jobId = job.priority('high').save(); |
| 131 | + |
| 132 | + let { status } = job.doc; |
| 133 | + logger.debug(`Job status: ${status}`); |
| 134 | + while ((status !== 'completed') && (status !== 'failed')) { |
| 135 | + const { doc } = job.refresh(); |
| 136 | + status = doc.status; |
| 137 | + } |
| 138 | + return { result: job.doc.result, jobStatus: status}; |
| 139 | + }, |
| 140 | +}); |
| 141 | + |
| 142 | +export default addHectar; |
| 143 | +export { HectarProcessor }; |
0 commit comments