| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
# Script for Biomaj PostProcess |
| 4 |
# author : ofilangi, osallou |
| 5 |
# date : 19/06/2007 |
| 6 |
# update : 22/10/2010 fix bug in generated alias file + a few cleanups |
| 7 |
# 23/12/2015 use makeblastdb for ncbi blast+ |
| 8 |
# |
| 9 |
# -title Title for database file [String] Optional |
| 10 |
# -in Input file(s) for formatting [File In] Optional |
| 11 |
# -logfile Logfile name: [File Out] Optional |
| 12 |
# default = formatdb.log |
| 13 |
# -dbtype nucl |
| 14 |
# -parse_seqids |
| 15 |
# |
| 16 |
|
| 17 |
#---------- |
| 18 |
#GLOBAL DEF |
| 19 |
#---------- |
| 20 |
BLASTDB_DIR="$datadir/index-blast"; # Path where aliases files should be generated |
| 21 |
mkdir -p $BLASTDB_DIR |
| 22 |
FORMATDB="makeblastdb"; # Path to formatdb executable |
| 23 |
|
| 24 |
|
| 25 |
#---------- |
| 26 |
# FUNCTIONS |
| 27 |
#---------- |
| 28 |
# createAlias: builds an alias file |
| 29 |
# arg1: file to write to |
| 30 |
# arg2: bank name |
| 31 |
# arg3: db file list |
| 32 |
createAlias() { |
| 33 |
local file=$1; |
| 34 |
local nomBanque=$2; |
| 35 |
local lFiles=$3; |
| 36 |
|
| 37 |
rm -f $file; |
| 38 |
echo "#" > $file |
| 39 |
echo "# Alias file created "`date` >>$file |
| 40 |
echo "#" >>$file ; |
| 41 |
echo "#">> $file ; |
| 42 |
echo "TITLE "$nomBanque >> $file; |
| 43 |
echo "#" >> $file; |
| 44 |
echo "DBLIST "$lFiles >>$file; |
| 45 |
echo "#" >> $file; |
| 46 |
echo "#GILIST" >> $file; |
| 47 |
echo "#" >> $file; |
| 48 |
echo "#OIDLIST" >> $file; |
| 49 |
echo "#" >> $file; |
| 50 |
} |
| 51 |
|
| 52 |
#----- |
| 53 |
# MAIN |
| 54 |
#----- |
| 55 |
|
| 56 |
if (test $# -ne 4) then |
| 57 |
echo "arguments:" 1>&2 |
| 58 |
echo "1: input files" |
| 59 |
echo "2: working directory" 1>&2 |
| 60 |
echo "3: formatdb options (without -in for input file)" 1>&2 |
| 61 |
echo "4: bank name" 1>&2 |
| 62 |
echo `formatdb --help`; |
| 63 |
exit -1 |
| 64 |
fi |
| 65 |
|
| 66 |
relWorkDir=`echo "$2" | sed "s/\/*$//"` # remove useless trailing slash |
| 67 |
|
| 68 |
workdir=$datadir/$dirversion/future_release |
| 69 |
workdir=$workdir/$relWorkDir; |
| 70 |
|
| 71 |
rm -rf $workdir; |
| 72 |
mkdir -p $workdir ; |
| 73 |
|
| 74 |
if ( test $? -ne 0 ) then |
| 75 |
echo "Cannot create $workdir." 1>&2 ; |
| 76 |
exit 1; |
| 77 |
fi |
| 78 |
|
| 79 |
cd $workdir |
| 80 |
|
| 81 |
# Some vars for links creation |
| 82 |
back=""; |
| 83 |
dir=$relWorkDir; |
| 84 |
OLDIFS=$IFS; |
| 85 |
IFS="/"; |
| 86 |
for i in $dir |
| 87 |
do |
| 88 |
back="../"$back; |
| 89 |
done |
| 90 |
IFS=$OLDIFS; |
| 91 |
|
| 92 |
# Create links to input files into the working dir |
| 93 |
listFile=""; |
| 94 |
|
| 95 |
for expression in $1 |
| 96 |
do |
| 97 |
# the basename can be a regex |
| 98 |
lsFile=`ls $datadir/$dirversion/future_release/$expression`; |
| 99 |
if ( test $? -ne 0 ) then |
| 100 |
echo "No input file found in dir `pwd`." 1>&2 ; |
| 101 |
exit 1 |
| 102 |
fi |
| 103 |
baseFile=`dirname $expression`; |
| 104 |
for f in $lsFile |
| 105 |
do |
| 106 |
name=`basename $f`; |
| 107 |
rm -f $4.p*; |
| 108 |
rm -f $4.n*; |
| 109 |
nameLink=`echo $name | cut -d"." -f1`; |
| 110 |
ln -s $back/$baseFile/$name $nameLink; |
| 111 |
if ( test $? -ne 0 ) then |
| 112 |
echo "Cannot create link. [ln -s $back$f $name]" 1>&2 ; |
| 113 |
exit 1 |
| 114 |
fi |
| 115 |
if (test -z "$listFile") then |
| 116 |
listFile=$nameLink; |
| 117 |
else |
| 118 |
listFile=$nameLink" "$listFile; |
| 119 |
fi |
| 120 |
done |
| 121 |
done |
| 122 |
|
| 123 |
echo "Input sequence file list: $listFile"; |
| 124 |
|
| 125 |
if (test -z "$listFile") then |
| 126 |
echo "No input file found." 1>&2 ; |
| 127 |
exit 1 |
| 128 |
fi |
| 129 |
|
| 130 |
nameB=$4; |
| 131 |
echo "Database name: $nameB"; |
| 132 |
|
| 133 |
echo "Working in "`pwd`; |
| 134 |
echo "Launching formatdb [formatdb -in $listFile $3 -out $nameB]"; |
| 135 |
|
| 136 |
# Execute formatdb |
| 137 |
$FORMATDB -in "$listFile" $3 -out $nameB; |
| 138 |
|
| 139 |
formatdbResult=$? |
| 140 |
if ( test $formatdbResult -ne 0 ) then |
| 141 |
echo "Formatdb failed with status $formatdbResult" 1>&2 ; |
| 142 |
exit 1 |
| 143 |
fi |
| 144 |
|
| 145 |
echo "##BIOMAJ#blast###$2$nameB" |
| 146 |
|
| 147 |
# Delete temp files and links |
| 148 |
#------------------------------------------------------------- |
| 149 |
rm -f $listFile; |
| 150 |
rm -f formatdb.log |
| 151 |
|
| 152 |
# Add generated files to biomaj postprocess dependance |
| 153 |
echo "Generated files:"; |
| 154 |
for ff in `ls *` |
| 155 |
do |
| 156 |
echo $PP_DEPENDENCE$PWD/$ff; |
| 157 |
done |
| 158 |
|
| 159 |
goodPath=`readlink $datadir/$dirversion/future_release -s -n`; |
| 160 |
if ( test $? -ne 0 ) then |
| 161 |
echo "Failed to get version path: readlink returned with an error [$goodPath]" 1>&2 ; |
| 162 |
exit 1 |
| 163 |
fi |
| 164 |
|
| 165 |
# Search for nal files which are sometimes generated by formatdb. |
| 166 |
lsAl=`ls *.?al 2> /dev/null`; |
| 167 |
|
| 168 |
if ( test $? -ne 0 ) then |
| 169 |
echo "No alias file found."; |
| 170 |
lsAl=""; |
| 171 |
else |
| 172 |
echo "Generated alias files:" |
| 173 |
echo "$lsAl"; |
| 174 |
fi |
| 175 |
|
| 176 |
# If nal files were generated, use them to generate nal files in $BLASTDB_DIR |
| 177 |
for fileIndexVirtuel in $lsAl |
| 178 |
do |
| 179 |
echo "Found alias file: [$fileIndexVirtuel]"; |
| 180 |
listIndex=`more $fileIndexVirtuel | grep DBLIST`; |
| 181 |
listFile2=""; |
| 182 |
for f in $listIndex |
| 183 |
do |
| 184 |
if (test $f != "DBLIST") then |
| 185 |
listFile2=$goodPath/$relWorkDir/$f" "$listFile2; |
| 186 |
fi |
| 187 |
done |
| 188 |
echo "Creating alias in [$BLASTDB_DIR/$fileIndexVirtuel]"; |
| 189 |
createAlias $BLASTDB_DIR/$fileIndexVirtuel $nameB "$listFile2" |
| 190 |
done |
| 191 |
|
| 192 |
# Else, if no nal file was generated by formatdb, create them |
| 193 |
if (test -z "$lsAl") then |
| 194 |
ext=`ls | grep .*hr$ | tail -c5 | head -c2`al; |
| 195 |
echo "Creating alias file [$PWD/$4$ext]"; |
| 196 |
|
| 197 |
listNhr=`ls *.*hr | sed 's/\..hr$//g'`; |
| 198 |
listFileNalRel=""; # List of blast db files, relative path |
| 199 |
listFileNalAbs=""; # List of blast db files, absolute path |
| 200 |
for f in $listNhr |
| 201 |
do |
| 202 |
listFileNalRel=$f" "$listFileNalRel; |
| 203 |
listFileNalAbs=$goodPath/$relWorkDir/$f" "$listFileNalAbs; |
| 204 |
done |
| 205 |
|
| 206 |
createAlias $4$ext $nameB "$listFileNalRel"; |
| 207 |
echo $PP_DEPENDENCE$PWD/$4$ext; |
| 208 |
|
| 209 |
echo "Creating alias in [$BLASTDB_DIR/$4$ext]"; |
| 210 |
createAlias $BLASTDB_DIR/$4$ext $nameB "$listFileNalAbs" ; |
| 211 |
fi |
| 212 |
|