Skip to content

Commit

Permalink
Releasing the fix for the reset button and the updated bounding box.
Browse files Browse the repository at this point in the history
  • Loading branch information
NAnguiano committed Feb 3, 2015
2 parents 5108812 + 072767e commit 97b8201
Show file tree
Hide file tree
Showing 6 changed files with 289 additions and 284 deletions.
File renamed without changes
Binary file added documents/GRNsight State Diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
295 changes: 151 additions & 144 deletions server/controllers/spreadsheet-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,159 +3,166 @@ var multiparty = require('multiparty'),
util = require('util'),
path = require('path');

processGRNmap = function (path, res, app) {
var sheet,
currentSheet,
network = {
genes: [],
links: [],
errors: [],
positiveWeights: [],
negativeWeights: [],
sheetType: "unweighted",
},
currentLink,
currentGene,
sourceGene,
targetGene,
sourceGeneNumber,
targetGeneNumber,
genesList = [],
sourceGenes = [],
targetGenes = [],
errorArray = [];

try {
sheet = xlsx.parse(path);
} catch (err) {
return res.json(400, "Unable to read input. The file may be corrupt.");
}

// For the time being, send the result in a form readable by people
//TODO: Optimize the result for D3
res.header('Access-Control-Allow-Origin', app.get('corsOrigin'));
//Look for the worksheet containing the network data
for (var i = 0; i < sheet.worksheets.length; i++) {
if (sheet.worksheets[i].name == "network") {
//Here we have found a sheet containing simple data. We keep looking
//in case there is also a sheet with optimized weights
currentSheet = sheet.worksheets[i];
} else if (sheet.worksheets[i].name == "network_optimized_weights") {
//We found a sheet with optimized weights, which is the ideal data source.
//So we stop looking.
currentSheet = sheet.worksheets[i];
network.sheetType = "weighted";
break;
var processGRNmap = function (path, res, app) {
var sheet;
try {
sheet = xlsx.parse(path);
} catch (err) {
return res.json(400, "Unable to read input. The file may be corrupt.");
}

// For the time being, send the result in a form readable by people
//TODO: Optimize the result for D3
res.header('Access-Control-Allow-Origin', app.get('corsOrigin'));

parseSheet(sheet, res);
};

var parseSheet = function(sheet, res) {
var currentSheet,
network = {
genes: [],
links: [],
errors: [],
positiveWeights: [],
negativeWeights: [],
sheetType: "unweighted",
},
currentLink,
currentGene,
sourceGene,
targetGene,
sourceGeneNumber,
targetGeneNumber,
genesList = [],
sourceGenes = [],
targetGenes = [];

//Look for the worksheet containing the network data
for (var i = 0; i < sheet.worksheets.length; i++) {
if (sheet.worksheets[i].name == "network") {
//Here we have found a sheet containing simple data. We keep looking
//in case there is also a sheet with optimized weights
currentSheet = sheet.worksheets[i];
} else if (sheet.worksheets[i].name == "network_optimized_weights") {
//We found a sheet with optimized weights, which is the ideal data source.
//So we stop looking.
currentSheet = sheet.worksheets[i];
network.sheetType = "weighted";
break;
}
}

if (currentSheet === undefined) {
network.errors.push(new newError("This file does not have a 'network' sheet or a 'network_optimized_weights' sheet.",
"Please select another file, or rename the sheet containing the adjacency matrix accordingly. Please refer to the " +
"<a href='http://dondi.github.io/GRNsight/documentation.html#section1' target='_blank'>Documentation page</a> for more information."))
return res.json(400, network);
}

for (var row = 0, column = 1; row < currentSheet.data.length; row++) {
// Genes found when row = 0 are targets. Genes found when column = 0 are source genes.
// At some point, we'll want to look through all 256 rows for random data.
// column = 1 so it skips the first line on the first row.
try {
while(column < currentSheet.data[row].length) {
if (row === 0) {
// These genes are the source genes
try {
currentGene = {name: currentSheet.data[row][column].value};
sourceGenes.push(String(currentGene.name.toUpperCase())); // For use in error checking later.
genesList.push(String(currentGene.name.toUpperCase()));
network.genes.push(currentGene);
} catch (err) {
network.errors.push(new newError("One of your gene names appears to be corrupt.", "Please fix the error and try uploading again."));
return res.json(400, network);
}
}

if (currentSheet === undefined) {
return res.json(400, "This file cannot be loaded because:<br><br>This file does not have a 'network' sheet or a 'network_optimized_weights' sheet.<br> Please select another" +
" file, or rename the sheet containing the adjacency matrix accordingly. Please refer to the " +
"<a href='http://dondi.github.io/GRNsight/documentation.html#section1' target='_blank'>Documentation page</a> for more information.");
}

for (var row = 0, column = 1; row < currentSheet.data.length; row++) {
// Genes found when row = 0 are targets. Genes found when column = 0 are source genes.
// At some point, we'll want to look through all 256 rows for random data.
// column = 1 so it skips the first line on the first row.
try {
while(column < currentSheet.data[row].length) {
if (row === 0) {
// These genes are the source genes
try {
currentGene = {name: currentSheet.data[row][column].value};
sourceGenes.push(String(currentGene.name.toUpperCase())); // For use in error checking later.
genesList.push(String(currentGene.name.toUpperCase()));
network.genes.push(currentGene);
} catch (err) {
return res.json(400, "One of your gene names appears to be corrupt. Please fix the error and try uploading again.");
} else if (column === 0) {
// These genes are the target genes
try {
currentGene = {name: currentSheet.data[row][column].value};
targetGenes.push(String(currentGene.name.toUpperCase()));
if(genesList.indexOf(String(currentGene.name.toUpperCase())) === -1) {
genesList.push(String(currentGene.name));
network.genes.push(currentGene);
}
} catch (err) {
network.errors.push(new newError("One of your gene names appears to be corrupt.", "Please fix the error and try uploading again."));
return res.json(400, network);
};
} else {
try {
if (currentSheet.data[row][column].value != 0) {
sourceGene = currentSheet.data[0][column].value.toUpperCase();
sourceGeneNumber = genesList.indexOf(sourceGene);
targetGene = currentSheet.data[row][0].value.toUpperCase();
targetGeneNumber = genesList.indexOf(targetGene);
currentLink = {source: sourceGeneNumber, target: targetGeneNumber, value: currentSheet.data[row][column].value};
if (currentLink.value > 0) {
currentLink.type = "arrowhead";
currentLink.stroke = "MediumVioletRed";
network.positiveWeights.push(currentLink.value);
} else {
currentLink.type = "repressor";
currentLink.stroke = "DarkTurquoise";
network.negativeWeights.push(currentLink.value);
}
} else if (column === 0) {
// These genes are the target genes
try {
currentGene = {name: currentSheet.data[row][column].value};
targetGenes.push(String(currentGene.name.toUpperCase()));
if(genesList.indexOf(String(currentGene.name.toUpperCase())) === -1) {
genesList.push(String(currentGene.name));
network.genes.push(currentGene);
}
} catch (err) {
return res.json(400, "One of your gene names appears to be corrupt. Please fix the error and try uploading again.");
};
} else {
try {
if (currentSheet.data[row][column].value != 0) {
sourceGene = currentSheet.data[0][column].value.toUpperCase();
sourceGeneNumber = genesList.indexOf(sourceGene);
targetGene = currentSheet.data[row][0].value.toUpperCase();
targetGeneNumber = genesList.indexOf(targetGene);
currentLink = {source: sourceGeneNumber, target: targetGeneNumber, value: currentSheet.data[row][column].value};
if (currentLink.value > 0) {
currentLink.type = "arrowhead";
currentLink.stroke = "MediumVioletRed";
network.positiveWeights.push(currentLink.value);
} else {
currentLink.type = "repressor";
currentLink.stroke = "DarkTurquoise";
network.negativeWeights.push(currentLink.value);
}
network.links.push(currentLink);
};
} catch (err) {
network.errors.push(err.message);
};
network.links.push(currentLink);
};
column++;
} catch (err) {
// TO DO: Customize this error message to the specific issue that occurred.
network.errors.push(new newError("One of the cells in the adjacency matrix appears to have a missing value.",
"Please ensure that all cells have a value, then upload the file again."));
return res.json(400, network);
};
column = 0;
} catch (err) {
res.json(400, "An unexpected error occurred.");
}
};
column++;
};

sourceGenes.sort();
targetGenes.sort();

checkDuplicates(errorArray, sourceGenes, targetGenes);
checkGeneLength(errorArray, genesList);

if(errorArray.length != 0) {
var errorString = "Your graph failed to load.<br><br>";
for(var i = 0; i < errorArray.length; i++) {
errorString += errorArray[i].possibleCause + " " + errorArray[i].suggestedFix + "<br><br>";
}
return res.json(400, errorString);
} else {
return res.json(network);
}
};

newError = function(possibleCause, suggestedFix) {
this.possibleCause = possibleCause;
this.suggestedFix = suggestedFix;
column = 0;
} catch (err) {
network.errors.push(new newError("An unexpected error occurred.", ""));
return res.json(400, network);
}

checkDuplicates = function(errorArray, sourceGenes, targetGenes) {
for(var i = 0; i < sourceGenes.length - 1; i++) {
if(sourceGenes[i] === sourceGenes[i + 1]) {
errorArray.push(new newError("There exist a duplicate for source gene " + sourceGenes[i] + ".", "Please remove the duplicate gene and submit again."));
}
}
for(var j = 0; j < targetGenes.length - 1; j++) {
if(targetGenes[j] === targetGenes[j + 1]) {
errorArray.push(new newError("There exist a duplicate for target gene " + targetGenes[i] + ".", "Please remove the duplicate gene and submit again."));
}
}
};

sourceGenes.sort();
targetGenes.sort();

checkDuplicates(network.errors, sourceGenes, targetGenes);
checkGeneLength(network.errors, genesList);

if(network.errors.length != 0) {
return res.json(400, network);
} else {
return res.json(network);
}
};

newError = function(possibleCause, suggestedFix) {
this.possibleCause = possibleCause;
this.suggestedFix = suggestedFix;
}

checkDuplicates = function(errorArray, sourceGenes, targetGenes) {
for(var i = 0; i < sourceGenes.length - 1; i++) {
if(sourceGenes[i] === sourceGenes[i + 1]) {
errorArray.push(new newError("There exist a duplicate for source gene " + sourceGenes[i] + ".", "Please remove the duplicate gene and submit again."));
}
}
for(var j = 0; j < targetGenes.length - 1; j++) {
if(targetGenes[j] === targetGenes[j + 1]) {
errorArray.push(new newError("There exist a duplicate for target gene " + targetGenes[i] + ".", "Please remove the duplicate gene and submit again."));
}
}
}

checkGeneLength = function(errorArray, genesList) {
for(var i = 0; i < genesList.length; i++) {
if(genesList[i].length > 12) {
errorArray.push(new newError("Gene " + genesList[i] + " is more than 12 characters in length. ", "Genes may only be between 1 and 12 characters in length. Please shorten the name and submit again. "));
}
}
checkGeneLength = function(errorArray, genesList) {
for(var i = 0; i < genesList.length; i++) {
if(genesList[i].length > 12) {
errorArray.push(new newError("Gene " + genesList[i] + " is more than 12 characters in length. ", "Genes may only be between 1 and 12 characters in length. Please shorten the name and submit again. "));
}
}
}

module.exports = function (app) {
//parse the incoming form data, then parse the spreadsheet. Finally, send back json.
Expand Down
Loading

0 comments on commit 97b8201

Please sign in to comment.