comparison extract-from-isa @ 0:975585306dc4 draft default tip

"planemo upload commit c2694fb4aa55c4f25eb53db73496eaf5e56d7872"
author prog
date Wed, 08 Jan 2020 07:50:13 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:975585306dc4
1 #!/bin/bash
2 # vi: fdm=marker
3
4 # Constants {{{1
5 ################################################################
6
7 PROG_NAME=$(basename $0)
8 PROG_PATH=$(dirname $0)
9 YES=yes
10 NO=no
11
12 # Global variables {{{1
13 ################################################################
14
15 DEBUG=0
16 EXT=
17 INPUT_DIR=
18 OUTPUT_DIR=
19 SYMLINK=$NO
20
21 # Print help {{{1
22 ################################################################
23
24 function print_help {
25 echo "Usage: $PROG_NAME -i <ISA_DIR> -e <EXT> -o <OUTPUT_DIR>"
26 echo
27 echo "Extract files with a given extension from ISA-Tab archives into a collection."
28 echo
29 echo "Options:"
30 echo " -e, --ext EXT The extension of the files to find."
31 echo " -h, --help Print this help message."
32 echo " -i, --input DIR Input directory containing ISA archive."
33 echo " -o, --output DIR Set the output directory to use."
34 echo " -s, --symlink Create symbolic links instead of copying files."
35 }
36
37 # Error {{{1
38 ################################################################
39
40 function error {
41
42 local msg=$1
43
44 echo "ERROR: $msg" >&2
45
46 exit 1
47 }
48
49 # Print debug msg {{{1
50 ################################################################
51
52 function print_debug_msg {
53
54 local dbglvl=$1
55 local dbgmsg=$2
56
57 [ $DEBUG -ge $dbglvl ] && echo "[DEBUG] $dbgmsg" >&2
58 }
59
60 # Read args {{{1
61 ################################################################
62
63 function read_args {
64
65 local args="$*" # save arguments for debugging purpose
66
67 # Read options
68 while true ; do
69 shift_count=1
70 case $1 in
71 -e|--ext) EXT="$2" ; shift_count=2 ;;
72 -g|--debug) DEBUG=$((DEBUG + 1)) ;;
73 -h|--help) print_help ; exit 0 ;;
74 -i|--input) INPUT_DIR="$2" ; shift_count=2 ;;
75 -o|--output) OUTPUT_DIR="$2" ; shift_count=2 ;;
76 -s|--symlink) SYMLINK=$YES ;;
77 -) error "Illegal option $1." ;;
78 --) error "Illegal option $1." ;;
79 --*) error "Illegal option $1." ;;
80 -?) error "Unknown option $1." ;;
81 -[^-]*) split_opt=$(echo $1 | sed 's/^-//' | sed 's/\([a-zA-Z]\)/ -\1/g') ; set -- $1$split_opt "${@:2}" ;;
82 *) break
83 esac
84 shift $shift_count
85 done
86 shift $((OPTIND - 1))
87
88 # Debug
89 print_debug_msg 1 "Arguments are : $args"
90
91 # Check input params
92 [[ $# -eq 0 ]] || error "No remaining arguments are allowed."
93 [[ -n $INPUT_DIR ]] || error "You must specify an input directory, using -i option."
94 [[ -d $INPUT_DIR ]] || error "\"$INPUT_DIR\" is not a valid directory."
95 [[ -n $OUTPUT_DIR ]] || error "You must specify an output directory, using -o option."
96 [[ ! -e $OUTPUT_DIR ]] || error "\"$OUTPUT_DIR\" already exists."
97 [[ -n $EXT ]] || error "You must specify the extension of the files you are looking for, with the -e option."
98 }
99
100 # MAIN {{{1
101 ################################################################
102
103 read_args "$@"
104
105 # Create output directory
106 print_debug_msg 1 "Create output directory \"$OUTPUT_DIR\"."
107 mkdir -p "$OUTPUT_DIR"
108
109 # Find files to extract
110 print_debug_msg 1 "Find \"$EXT\" files to extract in \"$INPUT_DIR\"."
111 files_to_extract=$(mktemp -t tmp.XXXXXX)
112 find "$(realpath $INPUT_DIR)" -iname "*.$EXT" >files_to_extract
113 print_debug_msg 1 "Files to extract:"
114 if [[ $DEBUG -ge 1 ]] ; then
115 cat files_to_extract >&2
116 fi
117
118 # Extract files
119 if [[ $SYMLINK == $YES ]] ; then
120 print_debug_msg 1 "Create symbolic links of all \"$EXT\" files to extract into \"$OUTPUT_DIR\"."
121 xargs -I % ln -s % "$OUTPUT_DIR" <files_to_extract
122 else
123 print_debug_msg 1 "Copy all \"$EXT\" files to extract to \"$OUTPUT_DIR\"."
124 xargs -I % cp % "$OUTPUT_DIR" <files_to_extract
125 fi
126 rm files_to_extract
127 print_debug_msg 1 "Files extracted:"
128 if [[ $DEBUG -ge 1 ]] ; then
129 ls -1 "$OUTPUT_DIR" >&2
130 fi