Skip to content

Commit

Permalink
F get info all genomes (#17)
Browse files Browse the repository at this point in the history
* initial commmit

* get all genome info

* fixing edge cases

* fixing edge cases

* 1.5.1

* less logs during tests
  • Loading branch information
daviortega committed Jul 8, 2018
1 parent c9b60e5 commit 9f6ec0f
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-mist3",
"version": "1.5.0",
"version": "1.5.1",
"description": "Helper package to deal with MiST3 API",
"main": "src/index.js",
"scripts": {
Expand Down
69 changes: 68 additions & 1 deletion src/Genomes.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class Genomes extends NodeMist3 {
getGenomeInfoByVersion(version) {
this.httpsOptions.method = 'GET'
this.httpsOptions.path = '/v1/genomes/' + version
this.httpsOptions.method = 'GET'
this.httpsOptions.header = {}
return new Promise((resolve, reject) => {
this.log.info('Fetching info from genome: ' + version)
Expand All @@ -39,4 +38,72 @@ class Genomes extends NodeMist3 {
req.end()
})
}

getInfoAll() {
const self = this
const per_page = 100
this.httpsOptions.method = 'GET'
const getOnePage = (page) => {
self.httpsOptions.path = `/v1/genomes?per_page=${per_page}&page=${page}`
const chunks = []
return new Promise((resolve, reject) => {
const req = https.request(self.httpsOptions, (res) => {
if (res.statusCode === 400) {
buffer.push(Buffer.from([null]))
}
if (res.statusCode !== 200) {
reject(res.statusMessage)
}
res.on('data', function(chunk) {
chunks.push(chunk)
})
res.on('end', function() {
let newGenes = ''
const buffer = Buffer.concat(chunks)
try {
newGenes = JSON.parse(buffer)
resolve(newGenes)
}
catch(err) {
self.log.error(buffer.toString())
reject(err)
}
})
})
req.end()
})
}
async function getAll() {
const per_page = 100
const infoAllGenomes = []
let page = 1
let maxIterations = 1000
let morePages = true
while (morePages === true && maxIterations > 0) {
self.log.debug(`Requesting page ${page}`)
await getOnePage(page).then(function(data) {
self.log.debug(`Page ${page} received`)
if (data.length !== 0) {
data.forEach((genome) => {
infoAllGenomes.push(genome)
})
page++
}
else {
morePages = false
}
})
.catch((err) => {
morePages = false
throw err
})
maxIterations--
}
return infoAllGenomes
}
return getAll().then((allInfo) => {
this.log.debug('Done fetching genomes')
return allInfo
})
}
}
10 changes: 10 additions & 0 deletions src/Genomes.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,14 @@ describe('Genomes', function() {
})
})
})
describe('getInfoAll', function() {
it('should get info of all genomes', function() {
this.timeout(240000)
const genomes = new Genomes()
const minNumGenomes = 5000
return genomes.getInfoAll().then((genomeInfoAll) => {
expect(genomeInfoAll.length).greaterThan(minNumGenomes)
})
})
})
})
9 changes: 7 additions & 2 deletions src/MakeFasta.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,20 @@ class MakeFasta {
}

makeFastaEntry_(gene) {
this.log.debug(gene)
const header = this.generateTag_(gene)
const sequence = this.getSequence_(gene)
return '>' + header + '\n' + sequence + '\n'
}

generateTag_(geneInfo) {
const genus = this.genomeInfo_.genus
this.log.debug(this.genomeInfo_)
let genus = this.genomeInfo_.genus
if (!genus)
genus = this.genomeInfo_.species.split(' ')[0]
this.log.debug(genus)
const species = this.genomeInfo_.species.split(' ')[1]

this.log.debug(species)
let orgID = genus.substring(0, fastaTagDefaults.numOfLettersForGenus)
orgID += fastaTagDefaults.orgIdSeparator
orgID += species.substring(0, fastaTagDefaults.numOfLettersForSpecies)
Expand Down
21 changes: 21 additions & 0 deletions src/MakeFasta.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,27 @@ describe('MakeFasta', function() {
expect(fastaEntry).eql(expected)
})
})
it('should pass with this gene too', function() {
const geneStableId = 'GCF_000224475.1-HALAR_RS01010'
const genomeVersion = 'GCF_000224475.1'
const expectedFastaEntry = '>ha_arc|GCF_000224475.1-HALAR_RS01010\nMTDQEVQQTNTLRQVDVMVRPTTRFPVPLSDGYSVYSALLGVLEDVDADVSAHIHDSPLGSLHSSGLQGVFGDSDRDYHKTLRPNESYQLRLGVVDPADLDIFQALVNALVLDGDTIELSHGTLQVDRFESVNTTHEDIVTEAGSMDNPTIELSFETATCIEEAGEITTMFPHRGAVFSSLLGKWNRSVSDDLELELDRETIERNVIEKPIARTYNTHSVLVNRVKNKDGETRNLFRQGFTGECSYDFKNASDSVQNAVTALGLFAEYSGVGSAVARGCGCVSAEVAGQ\n'
const genomes = new Genomes()
const genes = new Genes()
return genomes.getGenomeInfoByVersion(genomeVersion).then((genomeInfo) => {
const mkFasta = new MakeFasta(genomeInfo)
return genes.info(geneStableId)
.then((geneInfo) => {
return genes.addAseqInfo([geneInfo])
})
.then((geneInfoPlus) => {
console.log(geneInfoPlus)
return mkFasta.processOne(geneInfoPlus[0])
})
.then((fastaEntry) => {
expect(fastaEntry).eql(expectedFastaEntry)
})
})
})
})
describe('processMany', function() {
it('should pass', function() {
Expand Down
4 changes: 3 additions & 1 deletion src/NodeMist3Abstract.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ const should = chai.should()

const NodeMist3Abstract = require('./NodeMist3Abstract')

describe.skip('NodeMist3Abstract')
describe('NodeMist3Abstract', function() {
it.skip('Find a valid API endpoint')
})
2 changes: 1 addition & 1 deletion test/mochabunyan.opts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"mute": false,
"level": "debug",
"level": "warn",
"reporter": "spec"
}

0 comments on commit 9f6ec0f

Please sign in to comment.