Debian Codegraph @

transmute

raw
1 #!/bin/bash
2
3 # ===========================================================================
4 #
5 # PUBLIC DOMAIN NOTICE
6 # National Center for Biotechnology Information (NCBI)
7 #
8 # This software/database is a "United States Government Work" under the
9 # terms of the United States Copyright Act. It was written as part of
10 # the author's official duties as a United States Government employee and
11 # thus cannot be copyrighted. This software/database is freely available
12 # to the public for use. The National Library of Medicine and the U.S.
13 # Government do not place any restriction on its use or reproduction.
14 # We would, however, appreciate having the NCBI and the author cited in
15 # any work or product based on this material.
16 #
17 # Although all reasonable efforts have been taken to ensure the accuracy
18 # and reliability of the software and data, the NLM and the U.S.
19 # Government do not and cannot warrant the performance or results that
20 # may be obtained by using this software or data. The NLM and the U.S.
21 # Government disclaim all warranties, express or implied, including
22 # warranties of performance, merchantability or fitness for any particular
23 # purpose.
24 #
25 # ===========================================================================
26 #
27 # File Name: transmute
28 #
29 # Author: Jonathan Kans, Aaron Ucko
30 #
31 # Version Creation Date: 09/28/2018
32 #
33 # ==========================================================================
34
35 pth=$( dirname "$0" )
36
37 case "$pth" in
38 /* )
39 ;; # already absolute
40 * )
41 pth=$(cd "$pth" && pwd)
42 ;;
43 esac
44
45 case ":$PATH:" in
46 *:"$pth":* )
47 ;;
48 * )
49 PATH="$PATH:$pth"
50 export PATH
51 ;;
52 esac
53
54 check_for_perl() {
55
56 binary=$( command -v perl )
57 if [ ! -x "$binary" ]
58 then
59 echo "ERROR: required perl helper is not present" >&2
60 exit 1
61 fi
62 }
63
64 group_phrases() {
65 uniq |
66 paste -sd "," - |
67 sed -e 's/^+//g' -e 's/+$//g' -e 's/,+,/+/g' -e 's/^,//g' -e 's/,$//g' -e 's/+/ /g'
68 }
69
70 word_pairs() {
71 while read first rest
72 do
73 if [ -z "$rest" ]
74 then
75 echo "$first"
76 continue
77 fi
78 prev=$first
79 for curr in $rest
80 do
81 echo "$prev $curr"
82 prev="$curr"
83 done
84 done
85 }
86
87 # read command-line arguments to intercept certain commands
88
89 for arg in "$@"
90 do
91 case "$arg" in
92 -encodeXML )
93 while read line
94 do
95 echo "$line" |
96 sed -e "s/\&/\&amp;/; s/>/\&gt;/; s/</\&lt;/; s/'/\&apos;/g" -e 's/"/\&quot;/g'
97 done
98 exit
99 ;;
100 -decodeXML )
101 while read line
102 do
103 echo "$line" |
104 sed -e "s/\&amp;/\&/; s/\&gt;/>/; s/\&lt;/</; s/\&apos;/'/g" -e 's/\&quot;/"/g'
105 done
106 exit
107 ;;
108 -plainXML )
109 while read line
110 do
111 echo "$line" |
112 sed -e 's/<[^>]*>//g; s/ */ /g'
113 done
114 exit
115 ;;
116 -x2p )
117 binary=$( command -v xmllint )
118 if [ ! -x "$binary" ]
119 then
120 echo "ERROR: required xmllint helper is not present" >&2
121 exit 1
122 fi
123 xmllint --format -
124 exit
125 ;;
126 -j2p )
127 binary=$( command -v python3 )
128 if [ ! -x "$binary" ]
129 then
130 echo "ERROR: required python helper is not present" >&2
131 exit 1
132 fi
133 awk '{ gsub("}{", "}\n{"); print }' |
134 python3 -m json.tool
135 exit
136 ;;
137 -x2j )
138 check_for_perl
139 xml2json
140 exit
141 ;;
142 -word-pairs )
143 word-at-a-time |
144 filter-stop-words -plus |
145 group_phrases |
146 fmt -w 1 |
147 tr ',' ' ' |
148 word_pairs
149 exit
150 ;;
151 -all-pairs )
152 word-at-a-time |
153 group_phrases |
154 fmt -w 1 |
155 tr ',' ' ' |
156 word_pairs
157 exit
158 ;;
159 -missing )
160 shift
161 # find missing subranges in list of ordered numbers
162 awk '($1 != p+1) { print p+1 "-" $1-1} { p=$1 }' "$@"
163 exit
164 ;;
165 -sort-by-length )
166 check_for_perl
167 shift
168 perl -e 'print sort { length($a) <=> length($b) } <>'
169 exit
170 ;;
171 -sort-columns )
172 check_for_perl
173 shift
174 while read line
175 do
176 echo "$line" |
177 tr '\t' '\n' |
178 sort "$@" |
179 tr '\n' '\t' |
180 perl -pe 's/\t$/\n/g'
181 done
182 exit
183 ;;
184 -uniq-columns )
185 check_for_perl
186 shift
187 while read line
188 do
189 echo "$line" |
190 tr '\t' '\n' |
191 uniq "$@" |
192 tr '\n' '\t' |
193 perl -pe 's/\t$/\n/g'
194 done
195 exit
196 ;;
197 -grep-columns )
198 shift
199 while read line
200 do
201 echo "$line" |
202 tr '\t' '\n' |
203 grep "$@" |
204 tr '\n' '\t' |
205 perl -pe 's/\t$/\n/g'
206 done
207 exit
208 ;;
209 -loc2exons )
210 shift
211 tr ',' '\n' |
212 tr -s '.' '\t'
213 exit
214 ;;
215 -loc2introns )
216 shift
217 tr -s '.' '\n' |
218 grep ',' |
219 tr ',' '\t' |
220 print-columns '$1+1, $2-1'
221 exit
222 ;;
223 -loc2range )
224 shift
225 while read loc
226 do
227 echo "$loc" |
228 tr -cs '[^0-9]' '[\n*]' |
229 sort -n |
230 xargs echo |
231 awk '{ print $1 "\t" $NF }'
232 done
233 exit
234 ;;
235 -utf-bom | -bom )
236 # https://unix.stackexchange.com/questions/381230/how-can-i-remove-the-bom-from-a-utf-8-file
237 shift
238 sed $'1s/\xef\xbb\xbf//'
239 exit
240 ;;
241 -mactounix )
242 shift
243 perl -pi -e 's/\r/\n/g' "$1"
244 exit
245 ;;
246 -unixtomac )
247 shift
248 perl -pi -e 's/\n/\r/g' "$1"
249 exit
250 ;;
251 -unixtopc )
252 shift
253 perl -pi -e 's/\n/\r\n/g' "$1"
254 exit
255 ;;
256 -pctounix )
257 shift
258 perl -pi -e 's/\r\n/\n/g' "$1"
259 exit
260 ;;
261 -utf16toutf8 )
262 shift
263 iconv -f UTF-16 -t UTF-8 "$1"
264 exit
265 ;;
266 -utf8toascii )
267 shift
268 iconv -c -t ASCII "$1"
269 exit
270 ;;
271 -jta-clean )
272 sed -e 's/[^A-Za-z0-9]/ /g' |
273 tr 'A-Z' 'a-z' | tr -s ' ' |
274 sed -e 's/^ *//g' -e 's/ *$//g'
275 exit
276 ;;
277 -zapgremlins | -zap-gremlins )
278 shift
279 tr -c '\11\12\15\40-\176' '•'
280 exit
281 ;;
282 -parse-email )
283 shift
284 perl -nle 'print "$1" if /([a-zA-Z0-9\.\_\-]+\@[a-zA-Z0-9\.\_\-]+)(?<!\.)/'
285 exit
286 ;;
287 -parse-url )
288 shift
289 # input: "https://[email protected]:99/path/to/server.fcgi?width=5&height=7"
290 if [ $# -gt 0 ]
291 then
292 orig="$1"
293 else
294 orig=$( cat )
295 fi
296 proto=$( echo "$orig" | sed -ne "s,^\(.*://\).*,\1,p" )
297 url=$( echo "$orig" | sed -e "s,$proto,,g" )
298 user=$( echo "$url" | sed -ne 's/^\([^/@]*\)@.*/\1/p' )
299 hspt=$( echo "$url" | sed -ne "s,^[^/@]*@,,; s,/.*,,p" )
300 host=$( echo "$hspt" | sed -e "s,:.*,," )
301 port=$( echo "$hspt" | sed -ne "s,.*:\([0-9]*\).*,\1,p" )
302 rest=$( echo "$url" | sed -ne "s,^[^/]*/,,p" )
303 path=$( echo "$rest" | sed -e "s/\?.*//" )
304 argu=$( echo "$rest" | sed -ne "s/^[^?]*\?//p" )
305 echo "${proto}|${user}|${host}|${port}|${path}|${argu}"
306 # output: "https://|username|host.org|99|path/to/server.fcgi|width=5&height=7"
307 exit
308 ;;
309 -aa1to3 )
310 fold -w 1 | tr A-Z a-z |
311 while read single
312 do
313 case "$single" in
314 a ) echo "Ala" ;;
315 b ) echo "Asx" ;;
316 c ) echo "Cys" ;;
317 d ) echo "Asp" ;;
318 e ) echo "Glu" ;;
319 f ) echo "Phe" ;;
320 g ) echo "Gly" ;;
321 h ) echo "His" ;;
322 i ) echo "Ile" ;;
323 j ) echo "Xle" ;;
324 k ) echo "Lys" ;;
325 l ) echo "Leu" ;;
326 m ) echo "Met" ;;
327 n ) echo "Asn" ;;
328 o ) echo "Pyl" ;;
329 p ) echo "Pro" ;;
330 q ) echo "Gln" ;;
331 r ) echo "Arg" ;;
332 s ) echo "Ser" ;;
333 t ) echo "Thr" ;;
334 u ) echo "Sec" ;;
335 v ) echo "Val" ;;
336 w ) echo "Trp" ;;
337 x ) echo "Xxx" ;;
338 y ) echo "Tyr" ;;
339 z ) echo "Glx" ;;
340 - ) echo "Gap" ;;
341 "*" ) echo "Ter" ;;
342 esac
343 done
344 exit
345 ;;
346 -aa3to1 )
347 tr -d ' ' | fold -w 3 | tr A-Z a-z |
348 while read triple
349 do
350 case "$triple" in
351 ala ) echo "A" ;;
352 arg ) echo "R" ;;
353 asn ) echo "N" ;;
354 asp ) echo "D" ;;
355 asx ) echo "B" ;;
356 cys ) echo "C" ;;
357 gap ) echo "-" ;;
358 gln ) echo "Q" ;;
359 glu ) echo "E" ;;
360 glx ) echo "Z" ;;
361 gly ) echo "G" ;;
362 his ) echo "H" ;;
363 ile ) echo "I" ;;
364 leu ) echo "L" ;;
365 lys ) echo "K" ;;
366 met ) echo "M" ;;
367 phe ) echo "F" ;;
368 pro ) echo "P" ;;
369 pyl ) echo "O" ;;
370 sec ) echo "U" ;;
371 ser ) echo "S" ;;
372 stp ) echo "*" ;;
373 ter ) echo "*" ;;
374 thr ) echo "T" ;;
375 trp ) echo "W" ;;
376 tyr ) echo "Y" ;;
377 val ) echo "V" ;;
378 xle ) echo "J" ;;
379 xxx ) echo "X" ;;
380 esac
381 done
382 exit
383 ;;
384 -test | -tests )
385 str="the rain & in [spain] stay's <mainly> on \"the\" plain"
386 pln="the rain & in [spain] stay's <mainly> on \\\"the\\\" plain"
387 echo "Plain"
388 echo "$pln"
389 echo "$str" | transmute -plainXML
390 echo "XML"
391 echo "$str" | transmute -encodeXML
392 echo "$str" | transmute -encodeXML | transmute -decodeXML
393 echo "URL"
394 echo "$str" | transmute -encodeURL
395 echo "$str" | transmute -encodeURL | transmute -decodeURL
396 echo "B64"
397 echo "$str" | transmute -encodeB64
398 echo "$str" | transmute -encodeB64 | transmute -decodeB64
399 echo "j2p"
400 nquire -get http://mygene.info/v3 gene 3043 | transmute -j2p
401 echo "-j2x"
402 nquire -get http://mygene.info/v3 gene 3043 | transmute -j2x -set - -rec GeneRec -nest plural
403 echo "-x2p"
404 efetch -db nuccore -id 3043 -format gbc | transmute -format compact | transmute -x2p
405 echo "-g2x"
406 efetch -db nuccore -id 3043 -format gb | transmute -g2x
407 exit
408 ;;
409 * )
410 continue
411 ;;
412 esac
413 done
414
415 # pass remaining commands to precompiled transmute executable
416 PATH=/bin:/usr/bin
417 export PATH
418 osname=`uname -s`
419 cputype=`uname -m`
420 case "$osname-$cputype" in
421 Linux-x86_64 ) platform=Linux ;;
422 Darwin-x86_64 ) platform=Darwin ;;
423 Darwin-*arm* ) platform=Silicon ;;
424 CYGWIN_NT-* | MINGW*-* ) platform=CYGWIN_NT ;;
425 Linux-*arm* ) platform=ARM ;;
426 Linux-aarch64 ) platform=ARM64 ;;
427 * ) platform=UNSUPPORTED ;;
428 esac
429 compiled=$0."$platform"
430 if [ -x "$compiled" ]
431 then
432 exec "$compiled" "$@"
433 else
434 echo ""
435 echo "Unable to locate transmute executable. Please execute the following:"
436 echo ""
437 echo " nquire -dwn ftp.ncbi.nlm.nih.gov entrez/entrezdirect transmute.$platform.gz"
438 echo " gunzip -f transmute.$platform.gz"
439 echo " chmod +x transmute.$platform"
440 echo ""
441 fi