#!/bin/sh ### ### cluster3.sh ### ### Clustering 3' tag sequences. ### ### usage : % cluster3.sh -dir(in dir) -date(timestamp) ### ### exit code: ### 0 succeed ### 1 command line error ### 11 previous process is executing ### 12 input sequence does not exist ### ### ver 0.00 Jul 25 1997 T.Shin'i of NIG ### ver 0.10 Jun 17 1998 T.Shin'i of NIG ### Remove previous sequences when new sequences of ### clones which already exist in DB are processed. ### ver 0.11 Jun 22 2000 T.Shin'i of NIG ### Add "no merge" mode. ### ## ## initial settings ## LANG=""; export LANG if [ $BASELINE ]; then baseline=$BASELINE else baseline=/usr/local/yk fi PATH=`cat $baseline/etc/extbindir.rc`:$PATH; export PATH projhome=$baseline/clusterf bindir=$projhome/bin logdir=$projhome/log logabb=$logdir/cluster3.log workdir=$projhome/work lockfile=$workdir/.lockfile.cluster3 tmpfile=$workdir/cluster3.$$ archivedir=$projhome/Arc fastadirabb=$projhome/fasta ## ## data files and parameters ## clstlistname=celk-cog.list clstlist=$projhome/data/$clstlistname clstseq=$projhome/data/r.seq score_threshold=150 identity_threshold=89 seqperexec=1000 mergemode=0 # 1 : yes, 0 : no ## ## parse of command line parameters ## indir="" datestr="" for param do case $param in -[Dd][Ii][Rr]*) ## input directory name indir=`echo $param | sed -e 's/^-[Dd][Ii][Rr]//'`;; -[Dd][Aa][Tt][Ee]*) ## time stamp of this process datestr=`echo $param | sed -e 's/^-[Dd][Aa][Tt][Ee]//'`;; esac done if [ -z "$indir" ]; then echo "usage : $0 -dir(in-dir) -date(timestamp)" exit 1 fi if [ -z "$datestr" ]; then datestr=`date '+%Y%m%d'` fi logfile=$logabb.$datestr fastadir=$fastadirabb/result3/$datestr ## ## parameter or system error check ## if [ ! -d $indir ]; then echo "directory $indir does not exist" exit 12 fi if [ -f $lockfile -o -f $logfile -o -d $fastadir ]; then echo "Previous proceess is running." echo "Try again after the process ending." exit 11 fi echo $datestr > $lockfile ## make lockfile ## ## execute clustering ## tmpclstlist=$clstlist.tmp ls $indir > $tmpfile.master if [ `cat $tmpfile.master | wc -l` -lt 1 ]; then echo "sequence does not exist in $indir" rm $lockfile exit 12 fi ## ## remove old sequences ## (sed -e 's/\./ /' $tmpfile.master | gawk '{print $1}' ; gawk '{print $2}' $clstlist) | sort | uniq -d > $tmpfile.duplist if [ `cat $tmpfile.duplist | wc -l` -gt 0 ]; then cp $clstseq $clstseq.org split -$seqperexec $tmpfile.duplist $tmpfile.split. for list in $tmpfile.split.* do for clone in `cat $list` do grep -v " $clone"'$' $clstlist > $clstlist.new grep " $clone"'$' $clstlist | \ gawk '{printf("remove %s from %s\n", $2, $1)}' \ >> $logfile mv $clstlist.new $clstlist done findgetfs -s$clstseq `cat $list` >> $tmpfile.rmseq findgetfs -v -s$clstseq `cat $list` > $clstseq.new mv $clstseq.new $clstseq done rm $tmpfile.split.* fi mkdir $fastadir cd $workdir indirname=`basename $indir` if [ ! -d $indirname ]; then ln -s $indir $indirname fi newcelk=`gawk '{print $1}' $clstlist | sort -r -u | head -1 | sed -e 's/^CELK//'` newcelk=`expr $newcelk + 1` ## ## the beginning of loop ## for infile in `cat $tmpfile.master` do abbrev=`echo $infile | sed -e 's/\./ /' | gawk '{print $1}'` fasta $indirname/$infile $clstseq > $fastadir/$abbrev $bindir/postfasta -s$score_threshold -i$identity_threshold \ $fastadir/$abbrev > $tmpfile.fsout if [ `cat $tmpfile.fsout | wc -l` -lt 1 ]; then ## ## This is a new type -- create a new CELK group ## echo "$newcelk $abbrev" | \ gawk '{printf("CELK%05d\t%s\n", $1, $2)}' >> $clstlist cat $indirname/$infile >> $clstseq echo "$abbrev $newcelk" | \ gawk '{printf("add %s to new (CELK%05d)\n", $1, $2)}' \ >> $logfile newcelk=`expr $newcelk + 1` else ## ## find cluster ID for each hit ## for hit in `gawk '{print $2}' $tmpfile.fsout` do grep " $hit"'$' $clstlist >> $tmpfile.clst done ## ## find cluster ID for primary hit ## priclst=`head -1 $tmpfile.clst | gawk '{print $1}'` if [ $mergemode = 1 ]; then echo $priclst > $tmpfile.merge ## ## find cluster ID to be merged to the primary cluster ## tail +2 $tmpfile.clst | grep -v "^$priclst " | \ gawk '{print $1}' | sort -u > $tmpfile.clstlist for clst in `cat $tmpfile.clstlist` do grep "^$clst " $clstlist \ | gawk '{print $2}' | \ sort -u > $tmpfile.mchk grep "^$clst " $tmpfile.clst \ | gawk '{print $2}' \ | sort -u >> $tmpfile.mchk if [ `sort $tmpfile.mchk | uniq -u | wc -l` -lt 1 ]; then echo $clst >> $tmpfile.merge fi done ## ## merge one or more CELK clusters ## priclst=`sort $tmpfile.merge | head -1` for clst in `cat $tmpfile.merge` do if [ $clst != $priclst ]; then echo "s/$clst /$priclst /" \ >> $tmpfile.sed echo "merge cluster $clst to $priclst" \ >> $logfile fi done if [ -f $tmpfile.sed ]; then mv $clstlist $tmpclstlist sed -f $tmpfile.sed $tmpclstlist > $clstlist rm $tmpclstlist $tmpfile.sed fi fi ## ## add new clone ## echo "$priclst $abbrev" >> $clstlist cat $indirname/$infile >> $clstseq echo "add $abbrev to $priclst" >> $logfile rm $tmpfile.clst fi done ## ## The end of the loop ## ## ## make backup file ## cp $clstlist $archivedir/$clstlistname.$datestr gzip $archivedir/$clstlistname.$datestr if [ -f $tmpfile.rmseq ]; then cp $tmpfile.rmseq $archivedir/rrmseq.$datestr mv $clstseq.org $archivedir/rbackup.seq.$datestr gzip $archivedir/rrmseq.$datestr $archivedir/rbackup.seq.$datestr fi rm $tmpfile.* $lockfile if [ ! -d $indirname ]; then rm $indirname fi exit 0