#!/bin/bash

# Copyright 2003 Sashidhar Gadiraju, Peter K. Rogan
# 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 2 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#


#Program : mklnchr
#Version : 0.3
#Description : Create links from a directory containing both the annotation databases
#					and the genome as a chromosome delimited fasta file.

LIBSRC=""	#source dir where the chrN_mrna.txt and chrN.fa files are present
SLNK="" #empty string implies hard link , "-s" imples sym link
PFX="chr"
TMRNA="mrna.txt"	#target mrna file name
TFA="fsequ"			#target fasta file name
PRNAME=`basename $0`	

function usage
{
	if [ "$1" != "" ]; then
		echo "******************************************"
		echo "$1"
		echo "******************************************"
	fi
	echo "usage: $PRNAME [-hs]  LIBSRCDIR"
	echo "Create links to the chrN_mrna.txt and chrN.fa present in LIBSRCDIR"
	echo "The links are named mrna.txt and fsequ and are created in the directory chrm_N"
	echo "N takes all the values specified in the 'LIBSRCDIR/chrmlist' file"
	echo "-h : this help message"
	echo "-s : create symbolic links instead of hard links"
	exit 0
}

function abspath
{
	[ $# -eq 1 ] || return -1
	#GET THE ABSOLUTE PATH OF THE RESULTS DIRECTORY
	D=`dirname "$1"`
	B=`basename "$1"`
	ABSPATH="`cd \"$D\" 2>/dev/null && pwd || echo \"$D\"`/$B"	
}

function isdigit
{
	[ $# -eq 1 ] || return -1

	case $1 in
		*[!0-9]*|"") return -1;;
		*) return 0;;
	esac
}
	
while getopts ":hs" Option
do
	case $Option in
		h ) usage ;;
		s ) SLNK="-s";;
		* ) usage "Option not implemented";;
	esac
done
shift $(($OPTIND - 1))

if [ $# -le 0 ]; then
	usage "LIBSRCDIR not given"
fi
LIBSRC="$1"
abspath $LIBSRC ; LIBSRC=$ABSPATH
if [ ! -f "$LIBSRC/chrmlist" ]; then
	echo "'$LIBSRC/chrmlist' not found"
	exit 1
fi
						
# you cannot have entries as follows in chrmlist file
# 1 2
# 2 1 
function findCHRM
{
	CMD=`echo "grep '\<$1\>' $LIBSRC/chrmlist | grep -v '^ *\*' | tail -1"`
	#echo $CMD
	#eval $CMD
	#set the CHRMNUM value to the actual directory name
	#set the CHRMNAME value to the correspoding chromosome name
	CHRMNUM=`eval $CMD | awk '{print $1}'`
	CHRMNAME=`eval $CMD | awk '{print $2}'`
	#return the value of isdigit function
	isdigit $CHRMNUM
}

CHRSTCNT=`sort +0n $LIBSRC/chrmlist | grep -v '^ *$\|^ *\*' | head -1 | awk '{print $1}'`
CHRENDCNT=`sort +0n $LIBSRC/chrmlist | grep -v '^ *$\|^ *\*' | tail -1 | awk '{print $1}'`

for count in `sort +0n $LIBSRC/chrmlist | grep -v '^ *$\|^ *\*' | awk '{print $1}'`
do
	if [ "$count" = "" ]; then
		echo "Empty chrmlist"
		exit 1
	fi
	curdir="chrm_$count"
	findCHRM $count
	chrname=$CHRMNAME
	MRNA="${PFX}${chrname}_mrna.txt"
	FA="${PFX}${chrname}.fa"
	
	if [ ! -d "$curdir"	]; then
		echo "creating directory $curdir"
		mkdir "$curdir"
	fi
	
	#echo "remove previously existing mrna and fasta files"
	rm $curdir/$TMRNA
	rm $curdir/$TFA
		if [ ! -f $LIBSRC/$MRNA ]; then
			echo "$LIBSRV/$MRNA not found"
		else
			ln $SLNK $LIBSRC/$MRNA $curdir/$TMRNA
		fi
		if [ ! $LIBSRC/$FA ]; then
			echo "$LIBSRC/$FA not found"
		else
			ln $SLNK $LIBSRC/$FA $curdir/$TFA
		fi
done

