Skip to content

Commit

Permalink
updated prstab;le
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenasandoval88 committed Sep 25, 2024
1 parent 7f160e9 commit 3d99357
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 71 deletions.
14 changes: 7 additions & 7 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ <h4 class="hero_title">Diseasome</h3>
</div> -->

<div class="tab">
<button id = "prsButton" class="tablinks" onclick="openCity(event, 'PRS')">Calculate PRS</button>
<button class="tablinks" onclick="openCity(event, 'PGS catalog')">Explore the PGS catalog</button>
<button class="tablinks" onclick="openCity(event, 'OpenSNP')">Explore OpenSNP</button>
<!-- <button id = "prsButton" class="tablinks" onclick="openCity(event, 'PRS')">Calculate PRS</button> -->
<button id = "prsButton" class="tablinks" onclick="openTab(event, 'PRS')">Calculate PRS</button>
<button class="tablinks" onclick="openTab(event, 'PGS catalog')">Explore the PGS catalog</button>
<button class="tablinks" onclick="openTab(event, 'OpenSNP')">Explore OpenSNP</button>
<!-- <button id = "prsButton" class="tablinks" onclick="openTab(event, 'PRS')">Calculate PRS</button> -->
</div>

<div id = "selection"></div>
Expand Down Expand Up @@ -133,9 +133,9 @@ <h3>The Polygenic Score (PGS) Catalog</h3>


<div class="tab">
<button class="tablinks" onclick="openCity(event, 'Categories', true)">Categories</button>
<button class="tablinks" onclick="openCity(event, 'Traits', true)">Traits</button>
<button class="tablinks" onclick="openCity(event, 'EFO_ids', true)">EFO_ids</button>
<button class="tablinks" onclick="openTab(event, 'Categories', true)">Categories</button>
<button class="tablinks" onclick="openTab(event, 'Traits', true)">Traits</button>
<button class="tablinks" onclick="openTab(event, 'EFO_ids', true)">EFO_ids</button>

</div>

Expand Down
32 changes: 16 additions & 16 deletions sdk/get23.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ get23.allUsers = async function () {
get23.getTxts = async function (usersData) {
console.log("--------------------------")
console.log("Retreiving OpenSnp users!")
console.log("getTxts function running, even retreiving from storage is slow.")
console.log("retrieving 23andme files from storage is still slow.")
// clearTableUsingKeyLength(table,maxKeys)
let arr = []
let urls = (await usersData).map(x => x["genotype.download_url"])
Expand All @@ -59,10 +59,10 @@ get23.getTxts = async function (usersData) {

for (let i = 0; i < urls.length; i++) {
let parsedUser2 = await userTxts.getItem(urls[i]);
console.log("processing user #", i)
// console.log("processing user #", i)

if (parsedUser2 == null) {
console.log("user",i," NOT found in storage")
console.log("processing user",i,": NOT found in storage")
let url2 = 'https://corsproxy.io/?' + urls[i]
const user = (await (await fetch(url2)).text())
let parsedUser = (await get23.parseTxts(user, usersData[i]))
Expand All @@ -71,7 +71,7 @@ get23.getTxts = async function (usersData) {
arr.push(parsedUser)
userTxts.setItem(urls[i], parsedUser);
} else {
console.log("user",i," found in storage");
console.log("processing user",i,": found in storage");
arr.push(parsedUser2)
}
}
Expand Down Expand Up @@ -114,7 +114,7 @@ get23.parseTxts = async function (txt, usersData) {
// filter users without 23andme data (type = "23andme")
get23.usersByFileType = async function (type, users) {
let arr = []
console.log("usersByFileType,users----,",users)
// console.log("usersByFileType,users----,",users)
users.filter(row => row.genotypes.length > 0).map(dt => {

// keep user with one or more 23andme files
Expand Down Expand Up @@ -146,10 +146,10 @@ get23.usersInfoOnePhenotype = async function (phenoId){
let onePhenotypeUrl = `https://opensnp.org/phenotypes/json/variations/${phenoId}.json`
let users = (await (await fetch(cors + onePhenotypeUrl)).json())
let userIds = users.users.map(x => x.user_id)
console.log("users",users)
// console.log("users",users)
// // get user info with phenotype data (even those without genotype data)
const users2 = allUsers.filter(({id}) => userIds.includes(id));
console.log("users2",users2)
// console.log("users2",users2)

// let cleanUsers
// if (userIds2.length < 6) {
Expand Down Expand Up @@ -217,16 +217,16 @@ get23.usersInfoOnePhenotype(50)

// todo: remove pgs database
get23.userTxtsByPhenotypeId = async function (phenoId, keysLen, maxKeys) {
console.log("userTxtsByPhenotypeId ------------------------------" )
// console.log("userTxtsByPhenotypeId ------------------------------" )

let allUsers = await get23.allUsers()
console.log("allUsers",allUsers)
// console.log("allUsers",allUsers)
const cors = `https://corsproxy.io/?`
let onePhenotypeUrl = `https://opensnp.org/phenotypes/json/variations/${phenoId}.json`

// get users with selected phenotype
let users = await usersByPhenotype.getItem(onePhenotypeUrl)
console.log("users ",users )
// console.log("users ",users )

if (users == null){

Expand All @@ -240,28 +240,28 @@ get23.userTxtsByPhenotypeId = async function (phenoId, keysLen, maxKeys) {

// get user info
const users2 = allUsers.filter(({id}) => usersIds.includes(id));
console.log("users2 ",users2 )
// console.log("users2 ",users2 )

// add variation inof and remove "n/a"'s
let combined = users2.map(item => ({ ...item,
variation: (users.users.filter(f => f.user_id == item.id).map(x => x.variation)).toString(),//&& f.variation != "N/a"&& f.variation != "N/A"&& f.variation.length!= 0
trait: users.characteristic
})).filter(x=> x.variation !== "N/a" && x.variation !== "N/A")
console.log("combined ",combined,combined.map(x=>x.variation!=="N/a") )
// console.log("combined ",combined,combined.map(x=>x.variation!=="N/a") )


let cleanUsers
let maxUsers = 5
if (combined.length < maxUsers) {
cleanUsers = get23.usersByFileType("23andme", combined)
} else {
cleanUsers = await (get23.usersByFileType("23andme", combined.slice(9, 22)))//.slice(0,maxUsers)
cleanUsers = await (get23.usersByFileType("23andme", combined.slice(9, 20)))//.slice(0,maxUsers)
//console.log("Warning: user txts for phenotypeID", phenoId, "> 7. First 6 files used.")
console.log("cleanUsers",cleanUsers)
// console.log("cleanUsers",cleanUsers)
}
// get 23 and me texts from urls using getTxts function
let snpTxts = await get23.getTxts(cleanUsers, keysLen, maxKeys)
console.log("snpTxts",snpTxts)
// console.log("snpTxts",snpTxts)

return snpTxts
}
Expand Down Expand Up @@ -293,7 +293,7 @@ get23.getPhenotypeIdFromName = async function (characteristic) {
const dt = await get23.getUserPhenotypes()
// console.log("dt",dt)
const id = dt.filter(x => x.characteristic == characteristic)[0].id
console.log("Phenotype name", id, "corresponds to:", characteristic)
console.log("PGS Catalog Phenotype id", id, "corresponds to:", characteristic)
return id
}

Expand Down
36 changes: 19 additions & 17 deletions sdk/getPgs.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,16 @@ let pgsCategories = localforage.createInstance({
getPgs.traitFiles = async function(){
//console.log("---------------------------")
//console.log("running getPgs.traitFiles function")

const tf =(await storage.fetchAll("traitFilesTable",'https://www.pgscatalog.org/rest/trait/all')).flatMap(x => x)
return tf
let keys = await traitFilesTable.keys()
let traitFiles = (await Promise.all(keys.flatMap(async key => {return traitFilesTable.getItem(key)}))).flatMap(x=>x)
if (traitFiles==null){
traitFiles =(await storage.fetchAll("traitFilesTable",'https://www.pgscatalog.org/rest/trait/all')).flatMap(x => x)
}
return traitFiles

}

const traitFiles = await getPgs.traitFiles()

getPgs.idsFromCategory = async function(category) {
console.log("---------------------------")
Expand All @@ -50,14 +55,12 @@ getPgs.idsFromCategory = async function(category) {
let pgsIds = []
// get trait files that match selected category from drop down
// let traitFiles = await traitFilesTable.getItem("traitFiles")
let keys = await traitFilesTable.keys()
let traitFiles = (await Promise.all(keys.flatMap(async key => {return traitFilesTable.getItem(key)}))).flatMap(x=>x)
console.log("traitFiles",traitFiles)
if (traitFiles == null){
traitFiles = await getPgs.traitFiles()
}
console.log("traitFiles",traitFiles)

// let keys = await traitFilesTable.keys()
// // let traitFiles = (await Promise.all(keys.flatMap(async key => {return traitFilesTable.getItem(key)}))).flatMap(x=>x)
// // console.log("traitFiles",traitFiles)
// if (traitFiles == null){
// traitFiles = await getPgs.traitFiles()
// }
traitFiles.map(tfile => {
if (category.includes(tfile["trait_categories"][0])) {
arr.push(tfile)
Expand All @@ -77,7 +80,6 @@ const timeout = (ms) => {

// get score files using pgs ids list and subset those with less than 30 variants
getPgs.scoreFiles = async function(pgsIds) {

// var scores = await scoreFilesTable.getItem("scoreFiles")
let scores = storage.saveData(scoreFilesTable,`https://www.pgscatalog.org/rest/score/`,[pgsIds])
return scores
Expand All @@ -86,7 +88,7 @@ getPgs.scoreFiles = async function(pgsIds) {

getPgs.traits = async function(){
const arr = []
Array.from(new Set((await getPgs.traitFiles()).flatMap(x => {const obj = {}; obj[x["id"]] = x["label"]; arr.push(obj)})))
Array.from(new Set((traitFiles).flatMap(x => {const obj = {}; obj[x["id"]] = x["label"]; arr.push(obj)})))
//.sort().filter(e => e.length).map(JSON.stringify)), JSON.parse)
return arr
}
Expand All @@ -99,7 +101,7 @@ getPgs.traitsData = async function(traits) {
storeName: "traitsData"
})
let dt
let traitFiles = getPgs.traitFiles()
// let traitFiles = getPgs.traitFiles()
if ((await traitsData.getItem("traitsData")) === null) {

dt = traits.map(trait => {
Expand Down Expand Up @@ -135,9 +137,9 @@ getPgs.traitsData = async function(traits) {
// get pgs text file filtered by variants number
getPgs.loadScoreHm = async function(entry, build = 37, range) {
const scoreFiles = await getPgs.scoreFiles(entry)
console.log("entry, scoreFiles",entry, scoreFiles)
console.log("scoreFile for ",entry, scoreFiles)
const variants_number = scoreFiles[0].variants_number
console.log("variants_number",variants_number)
console.log("Number of variants:",variants_number)

let txt
let dt
Expand Down Expand Up @@ -184,7 +186,7 @@ getPgs.loadScoreHm = async function(entry, build = 37, range) {
pgsTxts.setItem(entry, txt)

} else if (dt != null){
console.log("pgs txt file for",entry,"found in storage",dt)
console.log("pgs txt file for",entry,"found in storage")
txt = dt
}else{
console.log(`pgs file too large: ${variants_number} variants`)
Expand Down
3 changes: 3 additions & 0 deletions sdk/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import localforage from 'https://cdn.skypack.dev/localforage';
import {getPgs} from "./getPgs.js"

localforage.config({
driver: [
Expand Down Expand Up @@ -202,6 +203,8 @@ functions.removeLocalStorageValues = async function(target, dbName) {

// get pgsids for all 17 traits ------------------------------------------------
functions.traitsData = async function(traits) {
const traitFiles = await getPgs.traitFiles()
// console.log("traitFiles",traitFiles)
let dt
if ((await pgs.getItem("traitsData")) === null) {

Expand Down
12 changes: 2 additions & 10 deletions sdk/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let traitFilesTable = localforage.createInstance({
storeName: "traitFilesTable"
})


const traitFiles = getPgs.traitFiles()
let singleUserAllPhenotypesTable = localforage.createInstance({name: "openSnpDb2",storeName: "singleUserAllPhenotypesTable"})
let phenotypesTable = localforage.createInstance({name: "openSnpDb",storeName: "phenotypesTable"})
let phenotypeUsersTable = localforage.createInstance({name: "openSnpDb",storeName: "phenotypesTable"})
Expand Down Expand Up @@ -277,15 +277,7 @@ snpPhenoDiv.on('plotly_click', async function (data) {
})

// PGS ////////////////////////////////////////////////////////////////////////////////////////////////////
let traitFiles
if (await traitFilesTable.getItem("traitFiles")==null){
// console.log(traitFilesTable.getItem("traitFiles"))
traitFiles = (await functions.fetchAll2('https://www.pgscatalog.org/rest/trait/all')).flatMap(x => x)
// console.log("plot.js traitFiles",traitFiles)
}else{
traitFiles = await traitFilesTable.getItem("traitFiles")
console.log("plot.js traitFiles",traitFiles)
}


const traits = Array.from(new Set((await traitFiles).flatMap(x => x["trait_categories"]).filter(e => e.length).map(JSON.stringify)), JSON.parse).sort()
// traits.map(x => functions.getAllPgsIdsByCategory(x))
Expand Down
9 changes: 7 additions & 2 deletions sdk/prs.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ PRS.calc = async function (matrix) {
// console.log("input",input)
// console.log("pgs:",matrix.pgs[j], "my23:",matrix.users[i])
let label = matrix.pgs.txts[j].id + "," + matrix.users.txts[i].openSnp.id
console.log()
let res = await prsTable.getItem(label)
// console.log("label, res:",label,res)

// console.log("res", res)
// check if prs has already been caclulated for this person and pgs entry
if (await prsTable.getItem(label) == null) {
Expand All @@ -157,10 +160,12 @@ PRS.calc = async function (matrix) {

// if prs qc fails for one user, remove the connected pgs entry
const obj = {}
obj.users = matrix.users
obj.users = matrix.users//.openSnp
// obj.pgs = matrix.pgs.txts.filter(x=>!badIds2.includes(x.id))
obj.prs = arr //.filter(x=> !badIds.includes(x.pgsId))
console.log("arr", arr)
//TODO: remove user and pgs txts from prsTable, takes double the storage space.
// instead save only the prs info and pull from the table to consoel.log entire results
// console.log("arr", arr)
return arr
}
// data object defined here ---
Expand Down
6 changes: 5 additions & 1 deletion sdk/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ storage.fetchAll = async function(newTable,url, maxPolls = null) {
storeName: newTable
})
const allResults = []
// let cachedData = await newTable.getItem(url);
// console.log("newTable,cachedData",newTable,cachedData)
const counts = (await (await (fetch(url))).json())
console.log("counts,",counts)

if (maxPolls == null) maxPolls = Infinity

// loop throught the pgs catalog API to get all files using "offset"
Expand All @@ -44,7 +48,7 @@ storage.fetchAll = async function(newTable,url, maxPolls = null) {
break
}
}
console.log("allResults",allResults)
// console.log("allResults",allResults)

return allResults
}
Expand Down
Loading

0 comments on commit 3d99357

Please sign in to comment.