Skip to content

Commit

Permalink
update-project-details API
Browse files Browse the repository at this point in the history
  • Loading branch information
sureshhewabi committed Nov 3, 2023
1 parent 85f6a47 commit d0a8057
Showing 1 changed file with 62 additions and 2 deletions.
64 changes: 62 additions & 2 deletions app/routes/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
from fastapi import APIRouter
from fastapi import APIRouter, Depends
from sqlalchemy import select, update

from app.models.dbsequence import DBSequence
from app.models.peptideevidence import PeptideEvidence
from app.models.spectrumidentification import SpectrumIdentification
from app.models.upload import Upload
from process_dataset import convert_pxd_accession_from_pride
from sqlalchemy.orm import Session
from app.routes.index import get_session
import os

import requests

main_router = APIRouter()

Expand All @@ -18,3 +26,55 @@ async def parse(px_accession: str, temp_dir: str | None = None, dont_delete: boo
@main_router.get("/health-test", tags=["Main"])
async def health():
return "OK"


@main_router.get("/update-project-details", tags=["Maintenance"])
async def update_project_details(session: Session = Depends(get_session)):
uploads = session.execute(select(Upload)).all()
projectListToUpdate = []
for upload in uploads:
isNeedToUpdate = False
# get project details from PRIDE API
# TODO: need to move URL to a configuration variable
px_url = 'https://www.ebi.ac.uk/pride/ws/archive/v2/projects/' + upload[0].project_id
print('GET request to PRIDE API: ' + px_url)
pride_response = requests.get(px_url)
r = requests.get(px_url)
if r.status_code == 200:
print('PRIDE API returned status code 200')
pride_json = pride_response.json()
pubmedId = ''
if len(pride_json['references']) > 0:
pubmedId = pride_json['references'][0]['pubmedId']
if pride_json is not None:
if upload[0].title is None or upload[0].title != pride_json['title']:
isNeedToUpdate = True
upload[0].title = pride_json['title']
if upload[0].description is None or upload[0].description != pride_json['projectDescription']:
isNeedToUpdate = True
upload[0].description = pride_json['projectDescription']
if (upload[0].pubmed_id is None and pubmedId != '') or upload[0].pubmed_id != pubmedId:
isNeedToUpdate = True
upload[0].pubmed_id = pubmedId

if isNeedToUpdate:
projectListToUpdate.append(upload[0])

for upload in projectListToUpdate:
stmt = update(Upload).where(Upload.id == upload.id).values(title=upload.title, description=upload.description,
pubmed_id=upload.pubmed_id)
session.execute(stmt)
session.commit()


@main_router.get("/calculate-stats", tags=["Maintenance"])
async def calculate_stats(session: Session = Depends(get_session)):
rows = session.query(Upload).all()
for row in rows:
number_of_proteins = session.query(DBSequence).filter(DBSequence.upload_id == row.id).count()
print(f"Number of proteins: {number_of_proteins}")
number_of_peptides = session.query(PeptideEvidence).filter(PeptideEvidence.upload_id == row.id).count()
print(f"Number of peptides: {number_of_peptides}")
number_of_spectra = session.query(SpectrumIdentification).filter(SpectrumIdentification.upload_id == row.id).count()
print(f"Number of Spectra: {number_of_spectra}")
print(".......................................")

0 comments on commit d0a8057

Please sign in to comment.