-
Notifications
You must be signed in to change notification settings - Fork 2
/
.bash-myutils
174 lines (153 loc) · 4.1 KB
/
.bash-myutils
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
alias u='cd ../'
alias u2='cd ../../'
alias u3='cd ../../../'
alias u4='cd ../../../../'
alias m='cd -'
alias hf=hissyfit
alias fixpaste='printf "\e[?2004l"'
alias bwork="bjobs | tail -n +2 | perl -ane '"'print "$F[2]\n";'"' | sort | uniq -c"
alias duck1='du -ch -d 1 | sort -h'
alias duck2='du -ch -d 2 | sort -h'
alias cD='cd $(pwd -P)'
# This one not used/tested yet. Had issues with shell quoting.
#
function set_farm_mem {
echo $1
MEM1="-M $1 -R ,select[mem>$1] rusage[mem=$1],"
export MEM=${MEM1//,/'"'}
}
# Last full file name, or batch of N last file names
#
function ffnl()
{ local num=1
if [[ ! -z "$1" ]]; then
num=$1
fi
tail -n $num $HOME/.ffn.mycache
}
function ffncp
{ local dest=${1:?Need destination to copy last file to}
cp $(tail -n 1 $HOME/.ffn.mycache) $dest
}
# not great. Use themax
function bash_max {
local max_value
read max_value
while read value; do
if [[ $value =~ ^[0-9]+$ ]] && [[ $value -gt $max_value ]]; then
max_value=$value
fi
done
echo $max_value
}
# not great. Use themin
function bash_min {
local min_value
read min_value
while read value; do
if [[ $value =~ ^[0-9]+$ ]] && [[ $value -lt $min_value ]]; then
min_value=$value
fi
done
echo $min_value
}
function themax {
perl -ne 'chomp; $m = $_ if $_ > $m; END { print "$m\n"; } BEGIN { $m = <>; chomp $m; }'
}
function themax2 {
export _tmp_idx_=${1:?Need index (0-based) for column containing number}
perl -ane 'chomp; ($m, $line) = ($F[$I], $_) if $F[$I] > $m; END { print "$line\n"; } BEGIN { $m = 0-"inf"; $line = ""; $I=$ENV{_tmp_idx_}}'
}
function themin {
perl -ne 'chomp; $m = $_ if $_ < $m; END { print "$m\n"; } BEGIN { $m = <>; chomp $m; }'
}
function themin2 {
export _tmp_idx_=${1:?Need index (0-based) for column containing number}
perl -ane 'chomp; ($m, $line) = ($F[$I], $_) if $F[$I] < $m; END { print "$line\n"; } BEGIN { $m = 0+"inf"; $line = ""; $I=$ENV{_tmp_idx_}}'
}
function theminmax {
perl -ne 'chomp;$h=$_ if$_>$h;$l=$_ if$_<$l; END { print "$l $h\n"; } BEGIN { $h=<>; chomp $h; $l=$h; }'
}
function decolon {
local sep=${2:-:}
local string=${1:?Need something to decolonise}
echo $string | tr "$sep" '\n'
}
function debug_bash {
foobar=${1:?Gimme something to run}
( bash --debugger <<EOC
PS4='+ ${BASH_SOURCE[0]} '
set -x
$*
set +x
exit
EOC
)
}
function countcram {
samtools view --input-fmt-option required_fields=2 -c ${1:?Need cram/bam argument}
}
function cpuhours {
cpuh=${1:?Need cpu hours}
numr=${2:?Need number of reads}
sc=$(sc "$cpuh / (150 * ($numr / 2**30))")
printf "%.2f CPUh per Gbase\n" $sc
}
function nflogcmd {
name=${1:?Need nextflow run name (nextflow log)}
nextflow log $name -f script > nf.$name.cmd.txt
}
function helpme {
echo 'declare -F ¯\\_(ツ)_/¯'
}
function fqfa {
gzip -dcf ${1:?Need file} | perl -ne '$i=($.+2)/4; print ">$i\n$_" if $. % 4 == 2'
}
function ie { # immediate edit
name=${1?Need program name}
path=$(which $name)
if [[ $? != 0 ]]; then
echo "Not found: [$name]"
return 1
elif [[ $(file --mime $path) =~ charset=binary ]]; then
echo "Binary file [$path] left alone"
return 1
elif [[ ! -x $path ]]; then
echo "File $path not executable (surprisingly)"
return 1
fi
$EDITOR $path
}
complete -c ie
function length {
# This one is for streams, and optionally takes a column name
# nchar is for cline arguments.
num=${1-1}
pick -k ::$num,len
}
function twofasta {
pick -k id::^'>':1 seq::2 | tr '\t' '\n'
}
# Given argument 'tag', glog searches the file named in variable GLOGME
# for a section defined by
# <tag
# ... content ...
# >
# If tag is not provided, lists all tags found in the file.
#
function glog() {
tag=${1:-__null__}
local glogme=${GLOGME:?Define GLOGME variable}
if [[ $tag == '__null__' ]]; then
echo "---"
grep -i "^<[a-z]" "$glogme" | cut -b 2-
echo "---"
return
fi
par=$(perl -ne 'print if /^<'$tag'/../^>/' "$glogme")
if [[ -n $par ]]; then
echo -e "---\n$par\n---"
else
echo "--- Not found ($tag)"
fi
}