Spaces:
Running
Running
Update mtdna_backend.py
Browse files- mtdna_backend.py +44 -6
mtdna_backend.py
CHANGED
@@ -424,6 +424,37 @@ def save_batch_output(all_rows, output_type, summary_text=None, flag_text=None):
|
|
424 |
# print(f"β οΈ Failed to load known samples: {e}")
|
425 |
# return None
|
426 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
427 |
def check_known_output(accession):
|
428 |
try:
|
429 |
# β
Load credentials from Hugging Face secret
|
@@ -432,18 +463,19 @@ def check_known_output(accession):
|
|
432 |
creds = ServiceAccountCredentials.from_json_keyfile_dict(creds_dict, scope)
|
433 |
client = gspread.authorize(creds)
|
434 |
|
435 |
-
|
436 |
-
spreadsheet = client.open("known_samples") # Replace with your sheet name
|
437 |
sheet = spreadsheet.sheet1
|
438 |
|
439 |
-
# β
Read all rows
|
440 |
data = sheet.get_all_values()
|
441 |
if not data:
|
|
|
442 |
return None
|
443 |
|
444 |
-
df = pd.DataFrame(data[1:], columns=data[0])
|
|
|
|
|
|
|
445 |
|
446 |
-
# β
Normalize accession pattern
|
447 |
match = re.search(r"\b[A-Z]{2,4}\d{4,}", accession)
|
448 |
if match:
|
449 |
accession = match.group(0)
|
@@ -451,11 +483,17 @@ def check_known_output(accession):
|
|
451 |
matched = df[df["Sample ID"].str.contains(accession, case=False, na=False)]
|
452 |
if not matched.empty:
|
453 |
return matched.iloc[0].to_dict()
|
|
|
|
|
|
|
454 |
|
455 |
except Exception as e:
|
456 |
-
|
|
|
|
|
457 |
return None
|
458 |
|
|
|
459 |
def hash_user_id(user_input):
|
460 |
return hashlib.sha256(user_input.encode()).hexdigest()
|
461 |
|
|
|
424 |
# print(f"β οΈ Failed to load known samples: {e}")
|
425 |
# return None
|
426 |
|
427 |
+
# def check_known_output(accession):
|
428 |
+
# try:
|
429 |
+
# # β
Load credentials from Hugging Face secret
|
430 |
+
# creds_dict = json.loads(os.environ["GCP_CREDS_JSON"])
|
431 |
+
# scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
|
432 |
+
# creds = ServiceAccountCredentials.from_json_keyfile_dict(creds_dict, scope)
|
433 |
+
# client = gspread.authorize(creds)
|
434 |
+
|
435 |
+
# # β
Open the known_samples sheet
|
436 |
+
# spreadsheet = client.open("known_samples") # Replace with your sheet name
|
437 |
+
# sheet = spreadsheet.sheet1
|
438 |
+
|
439 |
+
# # β
Read all rows
|
440 |
+
# data = sheet.get_all_values()
|
441 |
+
# if not data:
|
442 |
+
# return None
|
443 |
+
|
444 |
+
# df = pd.DataFrame(data[1:], columns=data[0]) # Skip header row
|
445 |
+
|
446 |
+
# # β
Normalize accession pattern
|
447 |
+
# match = re.search(r"\b[A-Z]{2,4}\d{4,}", accession)
|
448 |
+
# if match:
|
449 |
+
# accession = match.group(0)
|
450 |
+
|
451 |
+
# matched = df[df["Sample ID"].str.contains(accession, case=False, na=False)]
|
452 |
+
# if not matched.empty:
|
453 |
+
# return matched.iloc[0].to_dict()
|
454 |
+
|
455 |
+
# except Exception as e:
|
456 |
+
# print(f"β οΈ Failed to load known samples from Google Sheets: {e}")
|
457 |
+
# return None
|
458 |
def check_known_output(accession):
|
459 |
try:
|
460 |
# β
Load credentials from Hugging Face secret
|
|
|
463 |
creds = ServiceAccountCredentials.from_json_keyfile_dict(creds_dict, scope)
|
464 |
client = gspread.authorize(creds)
|
465 |
|
466 |
+
spreadsheet = client.open("known_samples")
|
|
|
467 |
sheet = spreadsheet.sheet1
|
468 |
|
|
|
469 |
data = sheet.get_all_values()
|
470 |
if not data:
|
471 |
+
print("β οΈ Google Sheet 'known_samples' is empty.")
|
472 |
return None
|
473 |
|
474 |
+
df = pd.DataFrame(data[1:], columns=data[0])
|
475 |
+
if "Sample ID" not in df.columns:
|
476 |
+
print("β Column 'Sample ID' not found in Google Sheet.")
|
477 |
+
return None
|
478 |
|
|
|
479 |
match = re.search(r"\b[A-Z]{2,4}\d{4,}", accession)
|
480 |
if match:
|
481 |
accession = match.group(0)
|
|
|
483 |
matched = df[df["Sample ID"].str.contains(accession, case=False, na=False)]
|
484 |
if not matched.empty:
|
485 |
return matched.iloc[0].to_dict()
|
486 |
+
else:
|
487 |
+
print(f"π Accession {accession} not found in known_samples.")
|
488 |
+
return None
|
489 |
|
490 |
except Exception as e:
|
491 |
+
import traceback
|
492 |
+
print("β Exception occurred during check_known_output:")
|
493 |
+
traceback.print_exc()
|
494 |
return None
|
495 |
|
496 |
+
|
497 |
def hash_user_id(user_input):
|
498 |
return hashlib.sha256(user_input.encode()).hexdigest()
|
499 |
|