-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakefile
167 lines (146 loc) · 4.01 KB
/
Snakefile
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
from dotenv import load_dotenv
import os
load_dotenv()
dw_key = os.getenv('DW_AUTH_TOKEN')
# ---- SETUP ----
acs_year = 2022
cdc_year = 2023
cities = ['bridgeport', 'hartford', 'new_haven', 'stamford']
def r_with_args(script):
cmd = f'Rscript {script} {acs_year} {cdc_year}'
return cmd
envvars:
'DW_AUTH_TOKEN'
# ---- RULES ----
rule download_data:
output:
acs = f'input_data/acs_nhoods_by_city_{acs_year}.rds',
cdc = f'input_data/cdc_health_all_lvls_nhood_{cdc_year}.rds',
acs_head = '_utils/acs_indicator_headings.txt',
cdc_head = '_utils/cdc_indicators.txt',
flag = '.meta_downloaded.json',
params:
acs_year = acs_year,
cdc_year = cdc_year,
shell:
'''
bash ./scripts/00a_download_data.sh {params.acs_year} {params.cdc_year}
'''
rule headings:
input:
rules.download_data.output.acs_head,
rules.download_data.output.cdc_head,
output:
headings = 'to_viz/indicators.json',
script:
'scripts/00b_make_headings.R'
rule notes:
input:
'_utils/manual/sources.txt',
'_utils/manual/urls.txt',
rules.download_data.output.acs,
rules.download_data.output.cdc,
output:
notes = 'to_viz/notes.json',
geos = '_utils/city_geos.rds'
script:
'scripts/00c_make_geo_notes.R'
rule combine_datasets:
input:
rules.download_data.output.acs,
rules.download_data.output.cdc,
rules.notes.output.geos,
'scripts/01_join_acs_health.R',
params:
acs_year = acs_year,
cdc_year = cdc_year,
output:
comb = f'output_data/all_nhood_{acs_year}_acs_health_comb.rds',
script:
'scripts/01_join_acs_health.R'
rule distro:
input:
rules.headings.output.headings,
rules.combine_datasets.output.comb,
params:
acs_year = acs_year,
output:
expand('to_distro/{city}_nhood_{year}_acs_health_comb.csv', city = cities, year = acs_year),
script:
'scripts/02_prep_distro.R'
rule viz_data:
input:
rules.combine_datasets.output.comb,
params:
acs_year = acs_year,
output:
viz = f'to_viz/nhood_wide_{acs_year}.json',
script:
'scripts/03_prep_json_to_viz.R'
rule make_shapes:
output:
expand('to_viz/cities/{city}_topo.json', city = cities),
script:
'scripts/04_make_shapefiles.R'
# rule upload_shapes:
# input:
# rules.make_shapes.output,
# output:
# '.shapes_uploaded.json'
# shell:
# 'bash ./scripts/05_upload_shapes_release.sh {input}'
rule upload_viz_data:
input:
data = rules.viz_data.output.viz,
headings = rules.headings.output.headings,
notes = rules.notes.output.notes,
shapes = rules.make_shapes.output,
output:
'.viz_uploaded.json',
shell:
'bash ./scripts/07_upload_data_release.sh {input}'
rule sync_to_dw:
input:
rules.distro.output,
output:
'.dw_uploaded.json',
params:
key = os.environ['DW_AUTH_TOKEN'],
year = acs_year,
files = rules.distro.output,
shell:
'''
bash ./scripts/06_sync_to_dw.sh {params.key} {params.year} {params.files}
'''
# ---- MAIN TARGETS ----
rule readme:
input:
readme = 'README.qmd',
snakefile = 'Snakefile',
output:
md = 'README.md',
dag = 'dag.png',
shell:
'quarto render {input.readme}'
rule all:
default_target: True
input:
rules.readme.output.md,
rules.viz_data.output,
rules.distro.output,
# rules.upload_shapes.output,
rules.upload_viz_data.output,
rules.sync_to_dw.output,
rules.download_data.output.flag,
# ---- CLEANUP ----
rule clean:
shell:
'''
rm -f to_distro/*.csv \
to_viz/*.json \
to_viz/cities/*.json \
input_data/*.rds \
output_data/*.rds \
_utils/*.txt \
_utils/*.rds
'''