Debian Codegraph @

tools/process/formatdb.sh

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