-
Notifications
You must be signed in to change notification settings - Fork 1
/
addRun.sh
executable file
·314 lines (275 loc) · 7.52 KB
/
addRun.sh
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/bin/sh
#
# this scripts sets up a run in the database
# to use it - e.g.
# addRun.sh 150630_D00390_0232_AC6K0WANXX
# jumps through various hoops due to idiosyncracies of
# \copy and copy
# only intended for importing GBS runs at this stage
#
function get_opts() {
help_text="\n
usage : addRun.sh -r run_name\n
example (dry run) : ./addRun.sh -n -r 151016_D00390_0236_AC6JURANXX\n
example : ./addRun.sh -r 151016_D00390_0236_AC6JURANXX\n
example : ./addRun.sh -r 171026_M02412_0043_000000000-D2N2Ua -m miseq\n
example : ./addRun.sh -r 190919_M02412_0124_000000000-CKPD4 -m miseq -s SQ2853\n
"
DRY_RUN=no
RUN_NAME=""
GBS_SAMPLE_LIB=""
MACHINE=hiseq
FORCE=no
FLOWCELL_MONIKER=""
while getopts ":nhr:m:d:s:fF:l:" opt; do
case $opt in
n)
DRY_RUN=yes
;;
i)
INTERACTIVE=yes
;;
r)
RUN_NAME=$OPTARG
;;
F)
FLOWCELL_MONIKER=$OPTARG
;;
l)
LANE=$OPTARG
;;
d)
RUN_PATH=$OPTARG
;;
f)
FORCE=yes
;;
m)
MACHINE=$OPTARG
;;
s)
GBS_SAMPLE_LIB=$OPTARG
;;
h)
echo -e $help_text
exit 0
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
KEY_DIR=/dataset/hiseq/active/key-files
}
function check_opts() {
if [ -z "$GBS_PRISM_BIN" ]; then
echo "GBS_PRISM_BIN not set - quitting"
exit 1
fi
if [ -z "$RUN_NAME" ]; then
echo -e $help_text
exit 1
fi
if [ ! -d $RUN_PATH ]; then
echo $RUN_PATH not found
exit 1
fi
#if [ $MACHINE == "miseq" ]; then
# echo "error , database import of miseq runs not currently supported"
# exit 1
#fi
if [[ ( ! -f $RUN_PATH/SampleSheet.csv ) && ( $MACHINE == "hiseq" ) ]]; then
echo $RUN_PATH/SampleSheet.csv not found , try using -d option to provide path to sample sheet
exit 1
fi
in_db=`$GBS_PRISM_BIN/is_run_in_database.sh $RUN_NAME`
if [ $in_db != "0" ]; then
if [ "$FORCE" != "yes" ]; then
echo "$RUN_NAME has already been added - quitting (use -f to override, e.g. adding samples)"
exit 1
fi
fi
# machine must be miseq , hiseq or novaseq
if [[ ( $MACHINE != "hiseq" ) && ( $MACHINE != "miseq" ) && ( $MACHINE != "novaseq" ) && ( $MACHINE != "iseq" ) ]]; then
echo "machine must be miseq or hiseq"
exit 1
fi
}
function echo_opts() {
echo "importing $RUN_NAME from $RUN_PATH"
echo "DRY_RUN=$DRY_RUN"
}
get_opts $@
check_opts
echo_opts
## from here , process the import
rm -f /tmp/${RUN_NAME}.txt
if [ -f /tmp/${RUN_NAME}.txt ]; then
echo "rm -f /tmp/${RUN_NAME}.txt failed - quitting"
exit 1
fi
rm -f /tmp/${RUN_NAME}.psql
if [ -f /tmp/${RUN_NAME}.psql ]; then
echo "rm -f /tmp/${RUN_NAME}.psql failed - quitting"
exit 1
fi
# if this is a hiseq run, parse and import the sample sheet and do all the rest
#cp $RUN_ROOT/${RUN_NAME}/SampleSheet.csv /tmp/${RUN_NAME}.txt
#awk -F, '{if(NF>5)print}' ${RUN_PATH}/SampleSheet.csv > /tmp/${RUN_NAME}.txt
if [ $MACHINE == "hiseq" ]; then
set -x
cat ${RUN_PATH}/SampleSheet.csv | $GBS_PRISM_BIN/sanitiseSampleSheet.py -r $RUN_NAME > /tmp/${RUN_NAME}.txt
set +x
# check we got something non-trivial
if [ ! -s /tmp/${RUN_NAME}.txt ]; then
echo "error parsed sample sheet /tmp/${RUN_NAME}.txt is missing or empty"
exit 1
fi
echo "
insert into bioSampleList (xreflsid, listName, listComment)
values(:run_name, :run_name, 'AgResearch Hiseq Run');
delete from samplesheet_temp;
\copy samplesheet_temp from /tmp/${RUN_NAME}.txt with CSV HEADER
insert into hiseqSampleSheetFact (
biosamplelist ,
FCID ,
Lane ,
SampleID ,
SampleRef ,
SampleIndex ,
Description ,
Control ,
Recipe,
Operator ,
SampleProject ,
sampleplate,
samplewell,
downstream_processing,
basespace_project)
select
obid,
FCID ,
Lane ,
SampleID ,
SampleRef ,
SampleIndex ,
Description ,
Control ,
Recipe,
Operator ,
SampleProject,
sampleplate,
samplewell ,
downstream_processing,
basespace_project
from
bioSampleList as s join samplesheet_temp as t
on s.listName = :run_name and
t. sampleid is not null;
insert into biosampleob(xreflsid, samplename, sampledescription, sampletype)
select distinct
SampleID,
SampleID,
description,
case when t.downstream_processing = 'GBS' then 'Illumina GBS Library'
else 'Illumina Library'
end
from
samplesheet_temp t where
sampleid is not null and
not exists (select obid from biosampleob where samplename = t.SampleID);
insert into biosamplelistmembershiplink (biosamplelist, biosampleob)
select
l.obid,
s.obid
from
(biosampleob as s join samplesheet_temp as t on
t.sampleid = s.samplename ) join biosamplelist as l on
l.listname = :run_name
except
select
biosamplelist,
biosampleob
from
biosamplelistmembershiplink as m join biosamplelist as l on
m.biosamplelist = l.obid and
l.listname = :run_name ;
" > /tmp/${RUN_NAME}.psql
elif [[ ( $MACHINE == "miseq" ) || ( $MACHINE == "novaseq" ) || ( $MACHINE == "iseq" ) ]]; then
# we don't bother with the sample sheet - just add any libraries from command line option
echo "
insert into bioSampleList (xreflsid, listName, listComment)
select
:run_name,:run_name,'AgResearch Illumina Run'
except
select
xreflsid, listName, listComment from bioSampleList
where
xreflsid = :run_name and
listName = :run_name and
listComment = 'AgResearch Illumina Run';
" > /tmp/${RUN_NAME}.psql
# if a sample lib provided on command line, add setup of that as well
if [ ! -z "$GBS_SAMPLE_LIB" ]; then
if [ -z "$FLOWCELL_MONIKER" ]; then
echo "please supply a flowcell moniker (-F option)"
exit 1
fi
if [ -z "$LANE" ]; then
echo "please supply a lane arg (-l option)"
exit 1
fi
echo "
insert into hiseqSampleSheetFact (
biosamplelist ,
fcid ,
Lane ,
SampleID ,
downstream_processing)
select
obid,
:fcid ,
:lane ,
:gbs_sample_lib,
'GBS'
from
bioSampleList where listName = :run_name ;
insert into biosampleob(xreflsid, samplename, sampletype)
select :gbs_sample_lib, :gbs_sample_lib, 'Illumina GBS Library'
except
select
xreflsid, samplename, sampletype
from
biosampleob
where
xreflsid = :gbs_sample_lib and samplename = :gbs_sample_lib and sampletype = 'Illumina GBS Library';
insert into biosamplelistmembershiplink (biosamplelist, biosampleob)
select
l.obid,
s.obid
from
biosampleob as s join biosamplelist as l on
l.listname = :run_name and s.xreflsid = :gbs_sample_lib
except
select
biosamplelist,
biosampleob
from
biosamplelistmembershiplink as m join biosamplelist as l on
m.biosamplelist = l.obid and
l.listname = :run_name ;
" >> /tmp/${RUN_NAME}.psql
fi # adding a mised sample library
fi # miseq
if [ $DRY_RUN == "no" ]; then
psql -U agrbrdf -d agrbrdf -h postgres -v run_name=\'${RUN_NAME}\' -v fcid=\'${FLOWCELL_MONIKER}\' -v lane=$LANE -v gbs_sample_lib=\'${GBS_SAMPLE_LIB}\' -f /tmp/${RUN_NAME}.psql
else
echo " will run
psql -U agrbrdf -d agrbrdf -h postgres -v run_name=\'${RUN_NAME}\' -v fcid=\'${FLOWCELL_MONIKER}\' -v lane=$LANE -v gbs_sample_lib=\'${GBS_SAMPLE_LIB}\' -f /tmp/${RUN_NAME}.psql"
fi
echo "done (URL to access run is http://agbrdf.agresearch.co.nz/cgi-bin/fetch.py?obid=${RUN_NAME}&context=default )"