Skip to content

Commit

Permalink
Merge pull request #13 from shamseen/external-api
Browse files Browse the repository at this point in the history
Calls ENA search endpoint via fixed search
  • Loading branch information
shamseen authored May 19, 2021
2 parents 49841ff + 20e04ac commit b668ff5
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 5 deletions.
38 changes: 38 additions & 0 deletions controllers/searchController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const express = require('express');
const router = express.Router();
const ENADataService = require('../dataServices/enaDataService');
const ena = new ENADataService();

// TEST GET!
router.get('/', async (req, res) => {

try {
const found = ena.enaData;
res.status(200).json(found);

} catch (err) {
console.log(err);
res.status(400).json({
msg: err.message
});
}
});

//
router.get('/:query', async (req, res) => {
// const filter = { [req.params.prop]: req.params.val };
// TODO: parse query, search mongo too

try {
const found = [];
// const found = await enaService.search();
res.status(200).json(found);

} catch (err) {
res.status(400).json({
msg: err.message
});
}
});

module.exports = router;
79 changes: 79 additions & 0 deletions dataServices/enaDataService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// structure from https://www.codementor.io/@evanbechtol/node-service-oriented-architecture-12vjt9zs9i

/* -------------------------------------------------------------- */
// European Nucleotide Archive (ENA): database of sequences
// The European Bioinformatics Institute exposes an API to access ENA
// In ENA, record IDs are called accessions
/* -------------------------------------------------------------- */
const axios = require('axios')


class ENADataService {
/* - populating with fixed search on instantiation - */
constructor() {
this.searchENA();
}

/* -- variables for JOINs -- */
// static baseURL = 'https://www.ebi.ac.uk/ena/portal/api/search?dataPortal=ena&query=';
// static resultLimit = 10;

/* -- API Calls -- */
// BLOCKED: ena POST endpoint docs are missing, waiting to hear back

// using a custom rule i made on their side instead
async searchENA() {
try {
const response = await axios.get('https://www.ebi.ac.uk/ena/portal/api/search?rule=8624c855-9921-4b98-b763-ac0d79b6a567');

this.enaData = response.data; // axios returns JSON already
return this.enaData;

} catch (error) {
// console.log(error);

return error.message;
}
}

/* -- Search Functionality -- */

filterByAccession(accType, value, arr) {

}

filterByKeyword(keyword, arr) {

}

search(seq_accession, study_accession, keyword) {
let results = [...this.enaData];

if (seq_accession !== '')
results = this.filterByAccession('accession', seq_accession, results);

if (study_accession !== '')
results = this.filterByAccession('study_accession', study_accession, results);

if (keyword !== '')
results = this.filterByKeyword(keyword, results);

return results;
}

/* Returns array of:
{
"accession": "CP034527",
"study_accession": "PRJNA504496",
"scientific_name": "eukaryotic synthetic construct",
"cell_type": "",
"dataclass": "STD",
"description": "Eukaryotic synthetic construct chromosome 3.",
"mol_type": "other DNA",
"plasmid": "",
"keywords": ""
}
*/
}

module.exports = ENADataService;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"dev": "nodemon server.js"
},
"dependencies": {
"axios": "^0.21.1",
"cors": "^2.8.5",
"dotenv": "^9.0.2",
"express": "^4.17.1",
Expand Down
11 changes: 6 additions & 5 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* --- Required modules --- */
require('dotenv').config() // inject .env into process.env
require('dotenv').config(); // inject .env into process.env
const express = require('express'); // http server
const cors = require('cors'); // expose resources for external websites
const mongoose = require('mongoose'); // talks to mongo db
Expand All @@ -14,7 +14,7 @@ mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false
})
});
// confirm connection
mongoose.connection.once('connected', () => console.log('>> mongoose is connected to mongoDB'));

Expand All @@ -29,11 +29,12 @@ app.use((req, res, next) => {
app.use(cors()); // exposes endpoints for apps to request

/* --- Routes --- */
app.use('/pngcats', require('./controllers/pngcatsController'))
app.use('/pngcats', require('./controllers/pngcatsController'));
app.use('/search', require('./controllers/searchController'));

app.get('/', (req, res) => {
res.send(`<h1>.pnGCAT API</h1>`)
res.send(`<h1>.pnGCAT API</h1>`);
})

/* --- Leggggoooooooo --- */
app.listen(PORT, () => console.log(`>> API Server: Listening on port ${PORT}. waiting for database...`))
app.listen(PORT, () => console.log(`>> API Server: Listening on port ${PORT}. waiting for database...`));
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ [email protected]:
resolved "https://registry.yarnpkg.com/array-flatten/-/array-flatten-1.1.1.tgz#9a5f699051b1e7073328f2a008968b64ea2955d2"
integrity sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=

axios@^0.21.1:
version "0.21.1"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.1.tgz#22563481962f4d6bde9a76d516ef0e5d3c09b2b8"
integrity sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==
dependencies:
follow-redirects "^1.10.0"

bl@^2.2.1:
version "2.2.1"
resolved "https://registry.yarnpkg.com/bl/-/bl-2.2.1.tgz#8c11a7b730655c5d56898cdc871224f40fd901d5"
Expand Down Expand Up @@ -212,6 +219,11 @@ finalhandler@~1.1.2:
statuses "~1.5.0"
unpipe "~1.0.0"

follow-redirects@^1.10.0:
version "1.14.1"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.1.tgz#d9114ded0a1cfdd334e164e6662ad02bfd91ff43"
integrity sha512-HWqDgT7ZEkqRzBvc2s64vSZ/hfOceEol3ac/7tKwzuvEyWx3/4UegXh5oBOIotkGsObyk3xznnSRVADBgWSQVg==

forwarded@~0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/forwarded/-/forwarded-0.1.2.tgz#98c23dab1175657b8c0573e8ceccd91b0ff18c84"
Expand Down

0 comments on commit b668ff5

Please sign in to comment.