Skip to content

Commit

Permalink
Adds the refactored forms in seperate file. They were removed from th…
Browse files Browse the repository at this point in the history
…e route file, they had to much logic. #85 Adds new functions to select the file headers, from default or from a line in file
  • Loading branch information
netbofia committed Jan 29, 2020
1 parent 37d6e4b commit 6b8f6dd
Show file tree
Hide file tree
Showing 6 changed files with 358 additions and 142 deletions.
137 changes: 137 additions & 0 deletions components/forms/upload_data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
var fs=require('fs')
var path=require('path')
var formidable=require('formidable')
var detect=require('detect-file-type')
var formidable = require('formidable')
var processTargetsFile=require('./../miRNADB/targets/psRNAtargetFile')

function uploadFile(req,uploadDir){
return new Promise((res,rej)=>{

// create an incoming form object
var form = new formidable.IncomingForm();

// specify that we want to allow the user to upload multiple files in a single request
form.multiples = false;
//Calculate file hash
form.hash = 'md5';
// store all uploads in the /uploads directory
form.uploadDir = path.join(uploadDir,"/de_matrices");

// every time a file has been uploaded successfully,
// rename it to it's original name
form.on('file', function(field, file) {
detect.fromFile(file.path,function(err,result){
if (err) rej(err)
if (result===null){
fs.rename(file.path, path.join(form.uploadDir, file.name), (err)=>{
if(err) rej(err)
file={hash:form.openedFiles[0].hash, name:form.openedFiles[0].name}
res(file);
});
}else{
fs.unlink(file.path, (err)=>{
err ? rej(err) : res({hash:'',name:"UnsupportedFile"})
})
}
})
});

// log any errors that occur
form.on('error', function(err) {
rej(err);
});

// once all the files have been uploaded, send a response to the client
form.on('end', function() {
//Not necessary for single file
});

// parse the incoming request containing the form data
form.parse(req);

})
}


function uploadTargets(req,uploadDir,){
return new Promise((res,rej)=>{
// create an incoming form object
var form = new formidable.IncomingForm();

// specify that we want to allow the user to upload multiple files in a single request
form.multiples = false;
//Calculate file hash
form.hash = 'md5';
// store all uploads in the /uploads directory
form.uploadDir = uploadDir;

// every time a file has been uploaded successfully,
// rename it to it's original name
form.on('file', function(field, file) {
detect.fromFile(file.path,function(err,result){
if (err) rej(err);
if (result===null){
let destinationDir=path.join(uploadDir,`/${req.params.studyid}/targets`)
let destinationFile=path.join(destinationDir, file.name)
fs.exists(destinationDir, (exists)=>{
if(exists){
rename(file.path, destinationFile)
}else{
fs.mkdir(destinationDir, { recursive: true }, (err)=>{
if (err){
rej(err);
}else{
rename(file.path, destinationFile)
}
})
}
function rename(inFile,outFile){
fs.rename(inFile,outFile, (err)=>{
if(err){
rej(err);
}else{
file={hash:form.openedFiles[0].hash, name:form.openedFiles[0].name}
processTargetsFile.getPreview(outFile,0).then(result=>{
result instanceof Error ? rej(result) : res({filePreview:result,file});
})
}
});
}
})
}else{
fs.unlink(file.path, (err)=>{
err ? rej(err) : res({hash:'',name:"UnsupportedFile"})
})
}
})
});

// log any errors that occur
form.on('error', function(err) {
console.log('An error has occured: \n' + err);
rej(err);
});

// once all the files have been uploaded, send a response to the client
form.on('end', function() {
//Not necessary for single file
});

// parse the incoming request containing the form data
form.parse(req);

})
}



module.exports={uploadFile,uploadTargets}








69 changes: 56 additions & 13 deletions components/miRNADB/targets/psRNAtargetFile.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,79 @@
//Process data to insert into database
var db = require('./../sqldb/index');
const fs = require('fs')
const minsize=2
module.exports=function(targetsFile){
const MINSIZE=2
const PREVIEW_LINES=10

module.exports={getPreview}




function getPreview(targetsFile){
fs.readFile(targetsFile,'utf8', (err, data) => {
if (err) throw err;
//console.log(data);
let lines=data.split("\n")
lines.forEach((line)=>{
line=line.split("\t")
if(line.length>minsize) insertLine(line)
if(line.length>MINSIZE) insertLine(line)
})
})
}




function getPreview(targetsFile,iteration){
return new Promise((res,rej)=>{
var rs = fs.createReadStream(targetsFile, {encoding: 'utf8'});
var acc = '';
var pos = 0;
let savedLines=0
var index = 0;

rs.on('data', function (chunk) {
matches = chunk.match(/\n/g);
acc += chunk;
lines=matches.length

if(lines>=PREVIEW_LINES){
rs.close()
if(matches.length>1){
var lastLines=PREVIEW_LINES-savedLines
let i=0;
while (i<lastLines){
index=chunk.indexOf("\n",index)+1
i+=1
}
}else{
index=chunk.indexOf('\n')
}
}else{
pos+=chunk.length;
savedLines+=lines
}
}).on('close', function () {
res(acc.slice(0, pos + index).split('\n'));
}).on('error', function (err) {
rej(err);
})
})
}


function insertLine(line){

return db.sequelize.transaction(function (t) {
// chain all your queries here. make sure you return them.
//miRNA_Acc. Target_Acc. Expectation UPE miRNA_start miRNA_end Target_start Target_end miRNA_aligned_fragment Target_aligned_fragment Inhibition Target_Desc.
return db.Feature.create({
genome_id:3,
name: line[1],
source: "psRNAtarget",
type: "targetPrediction",
start:line[4],
end:line[5],
name: line[1],
source: "psRNAtarget",
type: "targetPrediction",
start:line[4],
end:line[5],
}, {transaction: t}).then(function (feature) {
let featureId=feature.dataValues.id
let miRNA_sequence=line[0].split("-")[0]
Expand All @@ -35,12 +83,7 @@ function insertLine(line){
sequence=sequence
throw "lklk"
})
// return user.setShooter({
// firstName: 'John',
// lastName: 'Boothe'
// }, {transaction: t});
});

}).then(function (result) {
result
// Transaction has been committed
Expand Down
Loading

0 comments on commit 6b8f6dd

Please sign in to comment.