Skip to content

Instantly share code, notes, and snippets.

@mkf
Last active Feb 11, 2020
Embed
What would you like to do?
samy1.sh
#!/usr/bin/env bash
if [[ $2 != *.sam ]]
then
>&2 echo "The '$2' does not end with .sam"
exit 20
elif [[ ! -f $2 ]]
then
>&2 echo "The .sam is not a file"
exit 21
fi
sambasename="${2##*/}"
resultdir="${sambasename%%.sam}"
unset tsvprefix
if [[ -d $1 ]]
then
tsvprefix="$1/"
files=($1/*.tsv)
for i in "${!files[@]}"
do
x="${files[$i]}"
x="${x##$tsvprefix}"
x="${x%%.tsv}"
files[$i]="$x"
done
unset x
# files=("${${$1/*.tsv##$tsvprefix}%%.tsv}")
elif [[ -f $1 ]]
then
if [[ $1 != *.tsv ]]
then
>&2 echo "The '$1' does not end with .tsv"
exit 22
fi
tsvprefix="${$1%%/*}/"
x="${1##*/}"
x="${x%%.tsv}"
files=("$x")
unset x
else
>&2 echo "The '$1' is neither a file nor a directory."
exit 23
fi
declare -A store
# declare -p store
for i in "${files[@]}"
do
>&2 echo "Loading $i ..."
while read p
do
p="${p%%$'\t'*}"
if [[ ${store[$p]} == *":$i:"* ]]
then :
# echo "$p Already $i"
elif [[ ${store[$p]} == *":" ]]
then
store[$p]="${store[$p]}$i:"
# echo "$p Adding $i"
else
store[$p]=":$i:"
# echo "$p Init $i"
fi
done < "${tsvprefix}${i}.tsv"
done
# declare -p store
mkdir -v "$resultdir"
while read p
do
key="${p%%$'\t'*}"
if [[ -z ${store[$key]} ]]
then
# echo "Pusty '$p' '$key'"
continue
fi
IFS=':' read -r -a arr <<< "${store[$key]}"
# echo "Hej $p $key"
# echo "Dla '$p' '$key': " "el:${arr[@]} "
for i in "${arr[@]}"
do
# echo "Normalnie '$p' do '$i'"
echo "$p" >> "${resultdir}/${i}.sam"
done
done < "$2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment