Skip to content

Commit

Permalink
- support for data structure
Browse files Browse the repository at this point in the history
  • Loading branch information
boriskovar-m2ms committed Feb 23, 2021
1 parent 8d8a70a commit 42a74b8
Showing 1 changed file with 72 additions and 1 deletion.
73 changes: 72 additions & 1 deletion js/components/datasets/selectedCompoundsList.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,57 @@ export const SelectedCompoundList = memo(({ height }) => {
return line;
};

const populateMolObject = (molObj, compound, props) => {
const molecule = compound.molecule;

molObj = populateMolIds(molObj, compound);

let value = '';
for (let i = 0; i < props.length; i++) {
value = '';
const prop = props[i];
if (molecule.hasOwnProperty(prop)) {
value = molecule[prop];
} else {
let mapOfNumScores = undefined;
let mapOfTextScores = undefined;
if (molecule.hasOwnProperty('numerical_scores')) {
mapOfNumScores = molecule['numerical_scores'];
}
if (molecule.hasOwnProperty('text_scores')) {
mapOfTextScores = molecule['text_scores'];
}
if (mapOfNumScores !== undefined) {
if (mapOfNumScores.hasOwnProperty(prop)) {
value = mapOfNumScores[prop];
}
}
if (mapOfTextScores !== undefined) {
if (mapOfTextScores.hasOwnProperty(prop)) {
value = mapOfTextScores[prop];
}
}
}

molObj[prop] = value;

};

return molObj;
};

const populateMolIds = (molObj, compound) => {
if (compound.molecule.hasOwnProperty('compound_ids')) {
const ids = compound.molecule['compound_ids'];
for (let i = 0; i < ids.length; i++) {
const id = ids[i];
molObj[`compound-id${i}`] = id;
};
}

return molObj;
};

const getMaxNumberOfCmpIds = (mols) => {
let maxLength = 0;

Expand Down Expand Up @@ -308,18 +359,38 @@ export const SelectedCompoundList = memo(({ height }) => {
return setOfDataSets;
}

const getEmptyMolObject = (props, maxIdsCount) => {
let molObj = {};

for (let i = 0; i < maxIdsCount; i++) {
molObj[`compound-id${i}`] = '';
};
props.forEach(prop => {
molObj[prop] = '';
});

return molObj;
}

const downloadAsCsv = () => {
const usedDatasets = getUsedDatasets(moleculesObjectIDListOfCompoundsToBuy);
const props = getSetOfProps(usedDatasets);
let maxIdsCount = getMaxNumberOfCmpIds(moleculesObjectIDListOfCompoundsToBuy);
let data = prepareHeader(props, maxIdsCount);

const listOfMols = [];

moleculesObjectIDListOfCompoundsToBuy.forEach(compound => {
// data += `\n${compound.molecule.smiles},${compound.datasetID}`;
data += `\n${convertCompoundToCsvLine(compound, props, maxIdsCount)}`;
let molObj = getEmptyMolObject(props, maxIdsCount);
molObj = populateMolObject(molObj, compound, props)
listOfMols.push(molObj);
});
const dataBlob = new Blob([data], { type: 'text/csv;charset=utf-8' });

const jsonString = JSON.stringify(listOfMols);
console.log(jsonString);

FileSaver.saveAs(dataBlob, 'selectedCompounds.csv');
};

Expand Down

0 comments on commit 42a74b8

Please sign in to comment.