-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathseqdb.js
143 lines (133 loc) · 4.33 KB
/
seqdb.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// import modules
var fs = require('fs');
var async = require('async');
var mongoose = require('mongoose');
// import defined models
var models = require('./models');
// fetch database connection
// it gives access to the models
var dbConnection = models.dbConnection;
var Feature = dbConnection.model("Feature");
var RefSeq = dbConnection.model("RefSeq");
var GenomeStrains = dbConnection.model("genomestrains");
var ChromInfo = dbConnection.model("chromInfo");
function getRefRegion(region, callback) {
async.waterfall([
function(wfCbk) {
// get all those that start within region
RefSeq.find({
genome: region.genome,
chrom: region.chrom,
$or: [
{starts: {'$gte': region.start, '$lte': region.end}},
{ends: {'$gte': region.start, '$lte': region.end}},
{starts: {'$lte': region.start}, ends: {'$gte': region.end}}
]
}, wfCbk);
},
function(fragments, wfCbk) {
async.sortBy(fragments, function(fragment, sortCbk) {
return sortCbk(null, fragment.starts);
},wfCbk);
},
function(fragments, wfCbk) {
async.reduce(fragments, '', function(memo, fragment, redCbk) {
return redCbk(null, memo += fragment.sequence);
}, function(err, refseq) {
if (err) {throw err;}
return wfCbk(null, {'fragments': fragments, 'refseq': refseq});
});
},
function(data, wfCbk) {
if (!data.fragments.length) {
return callback(null, "");
}
var fragStart = data.fragments[0].starts;
var sliceStart = region.start - fragStart;
var sliceEnd = region.end - fragStart + 1;
refSeq = data.refseq.slice(sliceStart, sliceEnd);
return callback(null, refSeq);
}
]);
}
function getFeatureRegion(genome, name, flank, callback) {
Feature.find({
'attributes.genome': genome,
'attributes.Name': name
}, function(err, doc) {
if (err) {callback(err);}
else if (doc.length === 0) {callback(null, {});}
else {
var genome = doc[0].attributes.genome;
var start = doc[0].start - flank;
start = start > 0 ? start : 0;
var end = doc[0].end + flank;
var chrom = doc[0].seqid;
callback(
null,
{genome: genome, start: start, end: end, chrom: chrom}
);
}
});
}
function getFeatures(region, callback) {
console.log(region);
var regionQuery = {
'attributes.genome': region.genome,
type: {$in: [/^SNP/, 'gene', 'five_prime_UTR', 'three_prime_UTR', 'CDS', 'trait']},
seqid: {$regex: region.chrom},
start: {$gte: region.start, $lte: region.end}
};
Feature.find(regionQuery, callback);
}
function getRegion(region, callback) {
async.parallel({
features: function(paraCbk) {
getFeatures(region, function(err, data) {
if (err) {throw err;}
paraCbk(null, data);
});
},
refseq: function(paraCbk) {
getRefRegion(region, function(err, data) {
if (err) {throw err;}
paraCbk(null, data);
});
}
},
function(err, data) {
if (err) {throw err;}
data.region = region;
callback(null, data);
});
}
// fetch list of all strains in this genome
function getAllStrains(genome, callback) {
GenomeStrains.findOne({'genome': genome}, function(err, data) {
if (err) {throw err;}
callback(data.strains);
});
// callback(null, data)
}
// fetch list of genomes
function getRefList(callback) {
GenomeStrains.find().distinct('genome', function(err, data) {
if (err) {throw err;}
callback(data);
});
}
// fetch chromInfo for queried genome
function getChromInfo(callback) {
ChromInfo.find({}, function(err, data) {
if (err) {throw err;}
callback(data);
});
}
////////////////////
exports.Feature = Feature;
exports.getRegion = getRegion;
exports.getFeatures = getFeatures;
exports.getFeatureRegion = getFeatureRegion;
exports.getAllStrains = getAllStrains;
exports.getRefList = getRefList;
exports.getChromInfo = getChromInfo;