Skip to content

Commit

Permalink
Add refined test for pathway genes, remove debug
Browse files Browse the repository at this point in the history
  • Loading branch information
eweitz committed Mar 22, 2022
1 parent c60e0f6 commit 8364274
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 242 deletions.
6 changes: 3 additions & 3 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ module.exports = function(config) {
// list of files / patterns to load in the browser
files: [
'src/js/index.js',
// 'test/offline/**.test.js',
// 'test/online/**.test.js',
'test/online/related-genes.test.js',
'test/offline/**.test.js',
'test/online/**.test.js',
// 'test/online/related-genes.test.js',
{pattern: 'dist/data/**', watched: false, included: false, served: true, nocache: false}
],

Expand Down
73 changes: 8 additions & 65 deletions src/js/gene-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,8 @@ let perfTimes;
/** Get URL for gene cache file */
function getCacheUrl(orgName, cacheDir, ideo) {
const organism = slug(orgName);
console.log('in getCacheUrl, cacheDir')
console.log(cacheDir)
if (!cacheDir) {
// const splitDataDir = ideo.config.dataDir.split('/');
// const dataIndex = splitDataDir.indexOf('data');
// const baseDir = splitDataDir.slice(0, dataIndex).join('/') + '/data/';
// const baseDir = getDir
cacheDir = getDir('cache/');
console.log('in getCacheUrl, new cacheDir')
console.log(cacheDir)
}

const cacheUrl = cacheDir + organism + '-genes.tsv';
Expand Down Expand Up @@ -149,62 +141,28 @@ function hasGeneCache(orgName) {
}

async function cacheFetch(url) {
try {
console.log('&&&&&& in cacheFetch, url:')
console.log(url)
const response = await Ideogram.cache.match(url);
console.log('&&&&&& in cacheFetch, response:')
console.log(response)
if (typeof response === 'undefined') {
console.log('&&&&&& in cacheFetch, Ideogram.cache:')
console.log(Ideogram.cache)
// If cache miss, then fetch and add response to cache
try {
// await Ideogram.cache.add(url);
console.log('&&&&& before fetch, url:')
console.log(url)
const res = await fetch(url)
console.log('&&&&& after fetch, res:')
console.log(res)
if (!res.ok) {
console.log('&&&&& oops!')
throw new TypeError('bad response status');
}
console.log('&&&&& after if (!res.ok) {')
await Ideogram.cache.put(url, res);
console.log('&&&&& after await Ideogram.cache.put(url, res);')
return await Ideogram.cache.match(url);
} catch (e) {
console.log('&&&&& Failed to fetch web cache, url:')
console.log(url)
console.log('&&&&& Error:')
console.log(e)
}
console.log('&&&&&& in cacheFetch, after Ideogram.cache.add:')
}
console.log('&&&&&& exiting cacheFetch:')
const response = await Ideogram.cache.match(url);
if (typeof response === 'undefined') {
// If cache miss, then fetch and add response to cache
// await Ideogram.cache.add(url);
const res = await fetch(url);
await Ideogram.cache.put(url, res);
return await Ideogram.cache.match(url);
} catch (e) {
console.log('&&&& top-level error handler, e:')
console.log(e)
}
return await Ideogram.cache.match(url);
}

/**
* Fetch cached gene data, transform it usefully, and set it as ideo prop
*/
export default async function initGeneCache(orgName, ideo, cacheDir=null) {

console.log('in initGeneCache')

const startTime = performance.now();
perfTimes = {};

// Skip initialization if files needed to make cache don't exist
if (!hasGeneCache(orgName)) return;

console.log('!!!!!!!! in initGeneCache, after hasGeneCache')

// Skip initialization if cache is already populated
if (Ideogram.geneCache && Ideogram.geneCache[orgName]) {
// Simplify chief use case, i.e. for single organism
Expand All @@ -216,34 +174,23 @@ export default async function initGeneCache(orgName, ideo, cacheDir=null) {
Ideogram.geneCache = {};
}

console.log('!!!!!!!! in initGeneCache, after !Ideogram.geneCache')

Ideogram.cache = await caches.open(`ideogram-${version}`);

console.log('!!!!!!!! in initGeneCache, after await caches.open')

const cacheUrl = getCacheUrl(orgName, cacheDir, ideo);

const fetchStartTime = performance.now();
const response = await cacheFetch(cacheUrl);
console.log('!!!!!!!! in initGeneCache, after cacheFetch, response:')
console.log(response)

const data = await response.text();
// console.log('!!!!!!!! in initGeneCache, data:')
// console.log(data)
const fetchEndTime = performance.now();
perfTimes.fetch = Math.round(fetchEndTime - fetchStartTime);

console.log('!!!!!!!! in initGeneCache, after perfTimes.fetch')

const [
interestingNames, nameCaseMap, namesById, idsByName,
lociByName, lociById, sortedAnnots
] = parseCache(data, orgName);
perfTimes.parseCache = Math.round(performance.now() - fetchEndTime);

console.log('!!!!!!!! in initGeneCache, after perfTimes.parseCache')

ideo.geneCache = {
interestingNames, // Array ordered by general or scholarly interest
nameCaseMap, // Maps of lowercase gene names to proper gene names
Expand All @@ -255,12 +202,8 @@ export default async function initGeneCache(orgName, ideo, cacheDir=null) {
};
Ideogram.geneCache[orgName] = ideo.geneCache;

console.log('!!!!!!!! in initGeneCache, Ideogram.geneCache[orgName] = ideo.geneCache;')

if (ideo.config.debug) {
perfTimes.total = Math.round(performance.now() - startTime);
console.log('perfTimes in initGeneCache:', perfTimes);
}

console.log('!!!!!!!! exiting initGeneCache')
}
1 change: 0 additions & 1 deletion src/js/ideogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,6 @@ export default class Ideogram {
* @param {Object} config Ideogram configuration object
*/
static initRelatedGenes(config, annotsInList='all') {
console.log('in top-level initRelatedGenes')
return _initRelatedGenes(config, annotsInList);
}

Expand Down
18 changes: 0 additions & 18 deletions src/js/kit/related-genes.js
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,6 @@ function finishPlotRelatedGenes(type, ideo) {
moveLegend();

analyzePlotTimes(type, ideo);
console.log('after finishPlotRelatedGenes, type: ' + type)
}

/** Fetch position of searched gene, return corresponding annotation */
Expand Down Expand Up @@ -844,20 +843,14 @@ function sortByPathwayIxn(a, b) {
}

async function fetchPathwayGeneAnnots(searchedGene, pathway, ideo) {
console.log('in fetchPathwayGeneAnnots')

const annots = [];

const pathwayIxns =
await fetchPathwayInteractions(searchedGene.name, pathway.id, ideo);

console.log('in fetchPathwayGeneAnnots, after pathwayIxns')

const pathwayGenes = Object.keys(pathwayIxns);
const data = await fetchGenes(pathwayGenes, 'symbol', ideo);

console.log('in fetchPathwayGeneAnnots, after data')

const ixnColors = {
'Stimulates': 'green',
'Stimulated by': 'green',
Expand All @@ -880,8 +873,6 @@ async function fetchPathwayGeneAnnots(searchedGene, pathway, ideo) {
};

data.hits.forEach(gene => {
console.log('in fetchPathwayGeneAnnots, gene:')
console.log(gene)
// If hit lacks position
// or is same as searched gene (e.g. search for human SRC),
// then skip processing
Expand Down Expand Up @@ -919,7 +910,6 @@ async function fetchPathwayGeneAnnots(searchedGene, pathway, ideo) {
*
*/
async function plotPathwayGenes(searchedGene, pathway, ideo) {
console.log('in plotPathwayGenes')
const headerTitle = 'Genes in pathway';
initAnnotDescriptions(ideo, headerTitle);

Expand All @@ -931,10 +921,8 @@ async function plotPathwayGenes(searchedGene, pathway, ideo) {
ideo.relatedAnnots = [];

await processSearchedGene(searchedGene.name, ideo);
console.log('in plotPathwayGenes, after processSearchedGene')

const annots = await fetchPathwayGeneAnnots(searchedGene, pathway, ideo);
console.log('in plotPathwayGenes, after fetchPathwayGeneAnnots')
ideo.relatedAnnots.push(...annots);
finishPlotRelatedGenes('pathway', ideo);
}
Expand Down Expand Up @@ -1049,7 +1037,6 @@ function handleTooltipClick(ideo) {
* Manage click on pathway links in annotation tooltips
*/
function managePathwayClickHandlers(searchedGene, ideo) {
console.log('in managePathwayClickHandlers')
setTimeout(function() {
const pathways = document.querySelectorAll('.ideo-pathway-link');
if (pathways.length > 0 && !ideo.addedPathwayClickHandler) {
Expand Down Expand Up @@ -1187,7 +1174,6 @@ function setRelatedDecorPad(kitConfig) {
* @param {Object} config Ideogram configuration object
*/
function _initRelatedGenes(config, annotsInList) {
console.log('in _initRelatedGenes')

if (annotsInList !== 'all') {
annotsInList = annotsInList.map(name => name.toLowerCase());
Expand Down Expand Up @@ -1242,12 +1228,8 @@ function _initRelatedGenes(config, annotsInList) {

initAnalyzeRelatedGenes(ideogram);

console.log('#### before initGeneCache')

initGeneCache(ideogram.config.organism, ideogram);

console.log('#### after initGeneCache')

return ideogram;
}

Expand Down
23 changes: 1 addition & 22 deletions src/js/kit/wikipathways.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,16 +291,13 @@ function getMatches(gpml, label) {
}

async function fetchGpml(pathwayId) {
console.log('in fetchGpml')
const pathwayFile = `${pathwayId}.xml.gz`;
const gpmlUrl = `https://cdn.jsdelivr.net/npm/ixn2/${pathwayFile}`;
const response = await fetch(gpmlUrl);
const blob = await response.blob();
const uint8Array = new Uint8Array(await blob.arrayBuffer());
const rawGpml = strFromU8(decompressSync(uint8Array));

console.log('in fetchGpml, after rawGpml')

const gpml = new DOMParser().parseFromString(rawGpml, 'text/xml');

// console.log('gpml:')
Expand Down Expand Up @@ -404,39 +401,21 @@ function parseInteractionGraphic(graphic, graphIds) {
* Get all genes in the given pathway GPML
*/
export async function fetchPathwayInteractions(searchedGene, pathwayId, ideo) {
console.log('in fetchPathwayInteractions')
const gpml = await fetchGpml(pathwayId);
// console.log('in fetchPathwayInteractions, after gpml, gpml')
// Gets IDs and elements for searched gene and interacting gene, and,
// if they're in any groups, the IDs of those groups
const genes = {};

// console.log('')
let nodes
try {
nodes = Array.from(gpml.querySelectorAll('DataNode'));
} catch (e) {
console.log('in fetchPathwayInteractions, in catch')
}
console.log('in fetchPathwayInteractions, after nodes, nodes: ' + nodes)
const nodes = Array.from(gpml.querySelectorAll('DataNode'));
nodes.forEach(node => {
console.log('in fetchPathwayInteractions, before label')
const label = node.getAttribute('TextLabel');
console.log('in fetchPathwayInteractions, after label')
const normLabel = label.toLowerCase();
// console.log('ideo')
// console.log(ideo)
// console.log('ideo.geneCache')
// console.log(ideo.geneCache)
const isKnownGene = normLabel in ideo.geneCache.nameCaseMap;
console.log('in fetchPathwayInteractions, after isKnownGene')
if (isKnownGene) {
genes[label] = 1;
}
});

console.log('in fetchPathwayInteractions, after nodes.forEach')

const pathwayGenes = Object.keys(genes);
const pathwayIxns = {};
pathwayGenes.map(gene => {
Expand Down
Loading

0 comments on commit 8364274

Please sign in to comment.