-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_latest_month.py
More file actions
109 lines (85 loc) · 3.32 KB
/
Copy pathcheck_latest_month.py
File metadata and controls
109 lines (85 loc) · 3.32 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python3
"""
Check for latest available ClinVar data month
Compares what's available in the archive vs what we have locally
"""
import os
import re
import requests
from pathlib import Path
import json
# Configuration
ARCHIVE_URL = "https://ftp.ncbi.nlm.nih.gov/pub/clinvar/tab_delimited/archive/"
DATA_DIR = "data"
def get_latest_available_month():
"""Check archive directory for latest month with variant_summary files"""
try:
response = requests.get(ARCHIVE_URL, timeout=30)
response.raise_for_status()
# Parse FTP directory listing for year folders
year_pattern = r'href="(\d{4})/"'
years = re.findall(year_pattern, response.text)
years = sorted([int(y) for y in years], reverse=True)
if not years:
return None
# Check the most recent year first
for year in years:
year_url = f"{ARCHIVE_URL}{year}/"
try:
year_response = requests.get(year_url, timeout=30)
year_response.raise_for_status()
# Look for variant_summary files
file_pattern = r'variant_summary_(\d{4})-(\d{2})-\d{2}\.txt\.gz'
matches = re.findall(file_pattern, year_response.text)
if matches:
# Sort by month and get the latest
months = sorted([(int(y), int(m)) for y, m in matches], reverse=True)
latest_year, latest_month = months[0]
return f"{latest_year}-{latest_month:02d}"
except requests.RequestException:
continue
return None
except requests.RequestException:
return None
def get_latest_local_month():
"""Check what JSON files we have locally to determine latest processed month"""
data_path = Path(DATA_DIR)
if not data_path.exists():
return None
# Look for JSON files with pattern: variant_summary_YYYY-MM.json
json_pattern = r'variant_summary_(\d{4})-(\d{2})\.json'
json_files = []
for file_path in data_path.glob("variant_summary_*.json"):
match = re.match(json_pattern, file_path.name)
if match:
year, month = int(match.group(1)), int(match.group(2))
json_files.append((year, month))
if not json_files:
return None
# Get the latest
latest_year, latest_month = max(json_files)
return f"{latest_year}-{latest_month:02d}"
def set_github_output(name, value):
"""Set GitHub Actions output variable"""
if os.getenv('GITHUB_ACTIONS'):
with open(os.environ['GITHUB_OUTPUT'], 'a') as f:
f.write(f"{name}={value}\n")
def main():
"""Main function"""
latest_available = get_latest_available_month()
latest_local = get_latest_local_month()
if not latest_available:
set_github_output("new_data_available", "false")
set_github_output("error", "Could not check archive")
return
if not latest_local:
set_github_output("new_data_available", "true")
set_github_output("target_month", latest_available)
return
if latest_available > latest_local:
set_github_output("new_data_available", "true")
set_github_output("target_month", latest_available)
else:
set_github_output("new_data_available", "false")
if __name__ == "__main__":
main()