Public
Edited
Mar 31, 2023
Insert cell
Insert cell
async function asyncPool(poolLimit, array, iteratorFn) {
const ret = [];
const executing = [];
for (const item of array) {
const p = iteratorFn(item);
ret.push(p);
if (poolLimit <= array.length) {
const e = p.then(() => executing.splice(executing.indexOf(e), 1));
executing.push(e);
if (executing.length >= poolLimit) {
await Promise.race(executing);
}
}
}
return Promise.all(ret);
}
Insert cell
async function httpRequest(url) {
try {
const response = await fetch(url);

if (!response.ok) {
throw new Error(`Request failed with status ${response.status}`);
}

return await response.json();
} catch (error) {
console.error(`Error fetching data from URL: ${error.message}`);
return null;
}
}
Insert cell
{
const rsID = "rs123".split('rs')[1];
return httpRequest(`https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=snp&id=123&retmode=json`);
}
Insert cell
async function getChromosomeAndPosition(rsIDs, genomeBuild, apiKey) {
const requestLimit = 10;

const results = await asyncPool(requestLimit, rsIDs, async (rsID) => {
rsID = rsID.split('rs')[1];
const eutilsURL = `https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=snp&id=${rsID}&retmode=json&api_key=${apiKey}`;

try {
const response = await httpRequest(eutilsURL);
const assembly = response.refsnp[0].placements_with_allele.filter(
(item) => item.assembly_name === genomeBuild
)[0];

if (!assembly) {
throw new Error(`Genome build ${genomeBuild} not found for rsID ${rsID}.`);
}

const chromosome = assembly.seq_id;
const position = assembly.alleles[0].hgvs.lct.position;

return { rsID, chromosome, position };
} catch (error) {
console.error(`Error fetching SNP information for rsID ${rsID}: ${error.message}`);
return null;
}
});

return results;
}
Insert cell
async function getRsIDs(coordinates, apiKey) {
const requestLimit = 10;

const results = await asyncPool(requestLimit, coordinates, async (coordinate) => {
const { chromosome, position } = coordinate;
const eutilsURL = `https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=snp&term=${chromosome}[Chromosome]%20AND%20${position}[Base%20Position]&retmode=json&api_key=${apiKey}`;

try {
const response = await httpRequest(eutilsURL);
const rsID = response.esearchresult.idlist[0];

if (!rsID) {
throw new Error(`No SNP found at chromosome ${chromosome} and position ${position}.`);
}

return { chromosome, position, rsID };
} catch (error) {
console.error(`Error fetching rsID for chromosome ${chromosome} and position ${position}: ${error.message}`);
return null;
}
});

return results;
}
Insert cell
rsIDs = ['rs112750067', 'rs11385949', 'rs7'];
Insert cell
genomeBuild = 'GRCh38';
Insert cell
Insert cell
getChromosomeAndPosition(rsIDs, genomeBuild, Secret("NCBI_API_KEY"))
.then(results => results)
.catch(error => error);
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more