Debian Codegraph @

download-flatfile

raw
1 #!/bin/sh
2
3 # Public domain notice for all NCBI EDirect scripts is located at:
4 # https://www.ncbi.nlm.nih.gov/books/NBK179288/#chapter6.Public_Domain_Notice
5
6 # download-flatfile BCT PLN
7
8 # to remove corrupt files prior to downloading again, use:
9 # download-flatfile -verify BCT PLN
10
11 useFtp=true
12 useHttps=false
13 justVerify=false
14
15 while [ $# -gt 0 ]
16 do
17 case "$1" in
18 -ftp )
19 useFtp=true
20 useHttps=false
21 shift
22 ;;
23 -http | -https )
24 useFtp=false
25 useHttps=true
26 shift
27 ;;
28 -verify | -validate )
29 justVerify=true
30 shift
31 ;;
32 * )
33 break
34 ;;
35 esac
36 done
37
38 if [ "$#" -lt 1 ]
39 then
40 echo "Must have at least one sequence division abbreviation"
41 exit 1
42 fi
43
44 # construct grep command to filter desired GenBank divisions
45 filt="grep"
46 while [ "$#" -gt 0 ]
47 do
48 filt=$( echo "$filt -e $1" )
49 shift
50 done
51 filt=$( echo "$filt" | tr '[A-Z]' '[a-z]' )
52
53 # if -verify flag is set, check existing *.seq.gz files, remove any that fail validation
54 if [ "$justVerify" = true ]
55 then
56 didFail=false
57 ls *.seq.gz | eval "$filt" | sort -V |
58 while read fl
59 do
60 # delete files that fail validation
61 if [ -s "$fl" ]
62 then
63 errs=$( (gunzip -c "$fl" | gbf2xml | xtract -mixed -verify) 2>&1 )
64 if [ -n "$errs" ]
65 then
66 # delete
67 didFail=true
68 rm -f "$fl"
69 echo "Removed corrupted file '$fl'" >&2
70 fi
71 else
72 didFail=true
73 rm -f "$fl"
74 echo "Removed empty file '$fl'" >&2
75 fi
76 done
77 if [ "$didFail" = true ]
78 then
79 echo "Rerun download-flatfile command on selected GenBank divisions without -verify flag"
80 fi
81 exit 0
82 fi
83
84 DownloadOneByFTP() {
85
86 fl="$1"
87
88 url="ftp.ncbi.nlm.nih.gov/genbank"
89
90 echo "$fl" |
91 nquire -asp "${url}"
92
93 # delete if file is present but empty
94 if [ -f "$fl" ] && [ ! -s "$fl" ]
95 then
96 rm -f "$fl"
97 fi
98
99 # retry if no file
100 if [ ! -f "$fl" ]
101 then
102 sleep 10
103 echo "First Failed Download Retry" >&2
104 echo "$fl" |
105 nquire -asp "${url}"
106 fi
107
108 # retry again if still no file
109 if [ ! -f "$fl" ]
110 then
111 sleep 10
112 echo "Second Failed Download Retry" >&2
113 echo "$fl" |
114 nquire -asp "${url}"
115 fi
116
117 # verify contents
118 if [ -s "$fl" ]
119 then
120 errs=$( (gunzip -c "$fl" | gbf2xml | xtract -mixed -verify) 2>&1 )
121 if [ -n "$errs" ]
122 then
123 # delete and retry one more time
124 rm -f "$fl"
125 sleep 10
126 echo "Invalid Contents Retry" >&2
127 echo "$fl" |
128 nquire -asp "${url}"
129 if [ -s "$fl" ]
130 then
131 errs=$( (gunzip -c "$fl" | gbf2xml | xtract -mixed -verify) 2>&1 )
132 if [ -n "$errs" ]
133 then
134 rm -f "$fl"
135 frst=$( echo "$errs" | head -n 1 )
136 echo "ERROR invalid file '$fl' deleted, errors start with '$frst'" >&2
137 fi
138 else
139 echo "Download Attempts Failed" >&2
140 fi
141 fi
142 else
143 rm -f "$fl"
144 echo "Download of '$fl' Failed" >&2
145 fi
146 }
147
148 DownloadOneByHTTPS() {
149
150 fl="$1"
151
152 url="https://ftp.ncbi.nlm.nih.gov/genbank"
153
154 nquire -bulk -get "${url}" "$fl" > "$fl"
155
156 # delete if file is present but empty
157 if [ -f "$fl" ] && [ ! -s "$fl" ]
158 then
159 rm -f "$fl"
160 fi
161
162 # retry if no file
163 if [ ! -f "$fl" ]
164 then
165 sleep 10
166 echo "First Failed Download Retry" >&2
167 nquire -bulk -get "${url}" "$fl" > "$fl"
168 fi
169
170 # retry again if still no file
171 if [ ! -f "$fl" ]
172 then
173 sleep 10
174 echo "Second Failed Download Retry" >&2
175 nquire -bulk -get "${url}" "$fl" > "$fl"
176 fi
177
178 # verify contents
179 if [ -s "$fl" ]
180 then
181 errs=$( (gunzip -c "$fl" | xtract -mixed -verify) 2>&1 )
182 if [ -n "$errs" ]
183 then
184 # delete and retry one more time
185 rm -f "$fl"
186 sleep 10
187 echo "Invalid Contents Retry" >&2
188 nquire -bulk -get "${url}" "$fl" > "$fl"
189 if [ -s "$fl" ]
190 then
191 errs=$( (gunzip -c "$fl" | xtract -mixed -verify) 2>&1 )
192 if [ -n "$errs" ]
193 then
194 rm -f "$fl"
195 frst=$( echo "$errs" | head -n 1 )
196 echo "ERROR invalid file '$fl' deleted, errors start with '$frst'" >&2
197 fi
198 fi
199 fi
200 else
201 rm -f "$fl"
202 echo "Download of '$fl' Failed" >&2
203 fi
204 }
205
206 DownloadSet() {
207
208 if [ "$useFtp" = true ]
209 then
210 nquire -lst ftp.ncbi.nlm.nih.gov genbank |
211 grep "seq.gz" | eval "$filt" | sort -V |
212 skip-if-file-exists |
213 while read fl
214 do
215 sleep 1
216 echo "$fl"
217 DownloadOneByFTP "$fl"
218 done
219 elif [ "$useHttps" = true ]
220 then
221 nquire -get https://ftp.ncbi.nlm.nih.gov genbank |
222 xtract -pattern a -if a -starts-with gb -and a -ends-with ".seq.gz" -element a |
223 eval "$filt" | sort -V |
224 skip-if-file-exists |
225 while read fl
226 do
227 sleep 1
228 echo "$fl"
229 DownloadOneByHTTPS "$fl"
230 done
231 fi
232 }
233
234 DownloadSet
235 if [ $? -ne 0 ]
236 then
237 DownloadSet
238 fi
239
240 exit 0