#!/bin/bash
#run_asd.bash - a helper script that discovers how your stru file is formatted and then runs asd
# Copyright (C) 2011 Zachary A Szpiech (szpiechz@umich.edu)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
EXTRAARGS="--ibs-all --full"
CHECK=1
for i
do
case "$i" in
--help)
echo 'Usage: -f -s -t -o';
echo 'When -o is present the script will not check if any files will be overwriten';
exit 1;;
esac
done
args=`getopt f:s:t:o $*`
if test $? != 0
then
echo 'Usage: -f -s -t -o'
echo 'When -o is present the script will not check if any files will be overwriten'
exit 1
fi
set -- $args
for i
do
case "$i" in
-f) shift;FILE=$1;shift;;
-s) shift;SORT=$1;shift;;
-t) shift;THREADS=$1;shift;;
-o) shift;CHECK=0;;
esac
done
if [ -z $FILE ]
then
echo "ERROR: Need to provide a data file on argument -f."
exit
fi
if [ ! -e $FILE ]
then
echo "ERROR: Could not find $FILE."
exit
fi
if [ -z $SORT ]
then
echo "ERROR: Need to provide a non-data column where individual IDs on argument -s."
exit
fi
if [ -z $THREADS ]
then
echo "ERROR: Need to provide a number of threads to spawn on argument -t."
exit
fi
if [[ $SORT != [0-9]* ]]
then
echo "ERROR: $SORT is not a positive integer. Please pass a positive integer to -s."
exit
fi
if [ $SORT -le 0 ]
then
echo "ERROR: $SORT is not a positive integer. Please pass a positive integer to -s."
exit
fi
if [[ $THREADS != [0-9]* ]]
then
echo "ERROR: $THREADS is not a positive integer. Please pass a positive integer to -t."
exit
fi
if [ $THREADS -le 0 ]
then
echo "ERROR: $THREADS is not a positive integer. Please pass a positive integer to -t."
exit
fi
BIN=$(which asd 2> /dev/null)
if [ -z $BIN ]
then
if [ -e asd ]
then
BIN=./asd
else
echo "ERROR: Could not find asd in PATH or current directory."
exit
fi
fi
for i in head awk wc tail
do
if [ -z $(which $i 2> /dev/null) ]
then
echo "ERROR: Could not find $i in PATH. You need $i to run this script, and will have to run asd manually."
fi
done
LOCI=$(head -1 $FILE | awk '{print NF}')
if [[ $LOCI != [0-9]* ]]
then
echo "ERROR: Found $LOCI loci, which is not a positive integer. Is $FILE empty?"
exit
else
echo "Found $LOCI loci."
fi
CURNF=0
NDR=0
NDC=0
until [ $LOCI -lt $CURNF ]
do
let NDR=$NDR+1
CURNF=$(head -$NDR $FILE | tail -1 | awk '{print NF}')
#echo $CURNF
done
let NDR=$NDR-1
let NDC=$CURNF-$LOCI
#let LINES=$(cut -d " " -f1 $FILE | awk 'END{print NR}')-$NDR
let LINES=$(wc -l $FILE | awk '{print $1}')-$NDR
echo "Found $LINES data lines."
echo "Found $NDR header rows."
echo "Found $NDC header columns."
if [ $CHECK -eq 1 ]
then
for i in .ibs0 .ibs1 .ibs2 .dist
do
if [ -e ${FILE}$i ]
then
while [ "$answer" != "Y" ] && [ "$answer" != "n" ]
do
echo -n "WARNING: ${FILE}$i exists already. Overwrite? (Y/n): "
read answer
done
if [ "$answer" == "n" ]
then
echo -n "Rename? (Y/n): "
while [ "$answer2" != "Y" ] && [ "$answer2" != "n" ]
do
read answer2
done
if [ "$answer2" == "n" ]
then
echo "Exiting..."
exit
else
echo -n "Enter a new name for ${FILE}$i: "
read NEWFILENAME
mv ${FILE}$i $NEWFILENAME
echo "${FILE}$i renamed $NEWFILENAME"
fi
fi
fi
answer=""
answer2=""
done
fi
echo "Running asd with the following parameters:"
echo "$BIN -f $FILE -o $FILE $EXTRAARGS --ndc $NDC --ndr $NDR --threads $THREADS -c $LOCI -r $LINES -s $SORT"
$BIN -f $FILE -o $FILE $EXTRAARGS --ndc $NDC --ndr $NDR --threads $THREADS -c $LOCI -r $LINES -s $SORT