-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_db_citations.py
More file actions
73 lines (56 loc) · 2.59 KB
/
Copy pathupdate_db_citations.py
File metadata and controls
73 lines (56 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import os
import django
import pandas as pd
import numpy as np
# 1. Initialize the Django environment
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'scdb_api.settings') # change to your actual project name if needed
django.setup()
from dataset.models import Dataset
# 2. Configure the path
csv_path = "/data3/platform/sc_db/scgpt/data/cellxgene/st/dataset_sources_formatted.csv" # the CSV you just generated
def update_database():
print(f"📂 Reading CSV: {csv_path} ...")
# Read the CSV and replace NaN (empty values) with empty strings to avoid DB errors
df = pd.read_csv(csv_path)
df = df.replace({np.nan: None})
success_count = 0
skip_count = 0
print(f"🔍 Processing {len(df)} rows...\n")
for index, row in df.iterrows():
# Get the key info from the CSV
file_path = row.get('File Path')
citation_label = row.get('citation_label') or ''
# Handle the DOI: if the CSV contains "10.1038/...", we need to build the full URL
clean_doi = row.get('clean_doi')
if clean_doi:
# Simple check: use it directly if it already contains http, otherwise prepend the prefix
if str(clean_doi).startswith('http'):
citation_url = clean_doi
else:
citation_url = f"https://doi.org/{clean_doi}"
else:
citation_url = ''
collection_url = row.get('collection_url') or ''
explorer_url = row.get('explorer_url') or ''
try:
# Core alignment logic: look up the database by file_path
dataset = Dataset.objects.get(file_path=file_path)
# Update the fields
dataset.citation_label = citation_label
dataset.citation_url = citation_url
dataset.collection_url = collection_url
dataset.explorer_url = explorer_url
# save() no longer reads h5ad by default (update_metadata=False),
# so this is safe without update_fields workaround
dataset.save()
print(f"✅ [{index+1}] Updated: {dataset.dataset_id}")
success_count += 1
except Dataset.DoesNotExist:
print(f"⚠️ [{index+1}] Skipped: file path not found in the database -> {os.path.basename(file_path)}")
skip_count += 1
except Exception as e:
print(f"❌ [{index+1}] Error: {e}")
print("\n" + "="*30)
print(f"🎉 Done! Updated: {success_count}, skipped: {skip_count}")
if __name__ == "__main__":
update_database()