#!/bin/bash
# Set the PBS info.
#PBS -N Predict2_DIRECTORY_STRING
#PBS -l nodes=1:ppn=4:long,walltime=24:00:00
#PBS -q batch
#PBS -j oe
cd $PBS_O_WORKDIR
source ../../../../Utilities/Scripts/RunScriptTools || echo 'Internal Server error. Please contact the website administrator.' > errors.txt #include common functions, including sendResultsEmail.
calcStarted 'Predict2' 'Predict a Secondary Structure Common to Two Sequences' 'DIRECTORY_STRING' 'EMAIL_TO'
DIRECTORY=DIRECTORY_STRING
FULLCOMMAND="COMMAND_STRING"
FULLHTML="COMMAND_HTML_STRING"
MODETYPE="MODE_TYPE_STRING"
NUMSEQS=2
OUTPUTTYPE=""
######################################################################
# Do the dynalign calculation.
######################################################################
# Create the unique script variables necessary for this calculation.
COMMAND=`echo $FULLCOMMAND | cut -d ';' -f '1'`
HTMLCOMMAND=`echo $FULLHTML | cut -d ';' -f '1'`
# Run the main calculation command.
runTool dynalign-smp $COMMAND
# For each input sequence, create its results files and individual HTML page in a separate subfolder.
typeset -i i SEQS;
let SEQS=NUMSEQS;
let num=1;
while (( num <= SEQS )); do
# Make the next subfolder and move the relevant files into it.
# Then move to the subfolder.
mkdir seq${num};
mv dynalign${num}.ct seq${num}/dynalign.ct
cd seq${num}
# Create the comprehensive PS and PDF files.
runTool draw dynalign.ct dynalign.ps
if [[ ! -s ../errors.txt ]]; then ps2pdf dynalign.ps dynalign.pdf 1>/dev/null 2>>../errors.txt; fi
# If only one structure is present, create a JPEG and SVG file to be available to the user.
# Otherwise, split the CT file into individual structures and make CT, PS, PDF, and SVG files for each structure.
# Individual JPEG files are not created because they take too long and occupy too much space; they are made only if and when the user requests them.
NUMSTRUCTURES=0
if [[ ! -s ../errors.txt ]]; then
NUMSTRUCTURES=`grep -r showpage dynalign.ps | wc -l`
if (( $NUMSTRUCTURES == 1 )); then
if [[ ! -s ../errors.txt ]]; then gs -dNOPAUSE -dBATCH -q -r300 -dJPEGQ=100 -sDEVICE=jpeg -sOutputFile=dynalign.jpeg dynalign.ps 1>/dev/null 2>>../errors.txt; fi
runTool draw dynalign.ct dynalign.svg -n 1 --svg
else
if [[ ! -s ../errors.txt ]]; then
SEQLENGTH=`head -n 1 dynalign.ct | awk '{print $1}'`
let SEQLENGTH++;
awk '{print > (NR%"'"$SEQLENGTH"'"?i:i++)".ct";}' i=1 dynalign.ct
fi
typeset -i i END;
let END=$NUMSTRUCTURES;
let i=1;
while (( i <= END )); do
runTool draw dynalign.ct ${i}.ps -n $i
if [[ ! -s ../errors.txt ]]; then ps2pdf ${i}.ps ${i}.pdf 1>/dev/null 2>>../errors.txt; fi
runTool draw dynalign.ct ${i}.svg -n $i --svg
let i++;
done
fi
fi
# If no errors occurred in results files generation, create a results page using a templated HTML file.
# If no structures were generated, use a static notification HTML page as the results page.
# If only one structure was generated, place it in a specialized static HTML page.
# If multiple structures were generated, place it in a dynamic, expandable HTML page.
# If one or more structures were made, place the proper interface name, results directory, and command line in the results page.
if [[ ! -s ../errors.txt ]]; then
if grep -Fqs "contains no pairs" dynalign.svg
then
cp ../../../../Utilities/Pages/NoStructures.html Results.html
rm -f dynalign*
else
if (( $NUMSTRUCTURES == 1 )); then
copyTemplate ResultsPageSingleCTSingleStructure Results.html
else
copyTemplate ResultsPageSingleCTManyStructures Results.html
sed -i "s@STRUCTURE_NUMBER@$END@g" Results.html
awk '/download structure 1 links/,/structure 1 links end here -->/{print}' ../../../../../Utilities/Templates/ResultsPageSingleCTManyStructures.html > individualDiv.txt
typeset -i i END;
let END=$NUMSTRUCTURES;
let i=1;
while (( i <= END )); do
sed "s/1/$i/g" individualDiv.txt >> extendedDivs.txt
let i++;
done
sed -i '/download structure 1 links/,/structure 1 links end here -->/d' Results.html
sed -i "N;/Download Individual Structures/r extendedDivs.txt" Results.html
fi
fi
sed -i "s@FULL_RESULTS_DIRECTORY@ResultsPages/$DIRECTORY/seq${num}@g" Results.html
fi
# If an error did occur in results file generation, create an error page using a templated HTML file, delete all generated files, and break out of the loop.
if [[ -s ../errors.txt ]]; then
#copyTemplate Error ../Results.html
#sed -i "N;/errorText/r ../errors.txt" ../Results.html
rm -f ../Results.html
cd ..
rm -rf seq*
break
fi
# Move back to the main directory and increment the loop.
cd ..
let num++;
done
# If an error results page was not already made, put the results pages together into one big one.
if [[ ! -s Results.html ]]; then
copyTemplate ResultsPageManyAlignedStructures Results.html
sed -i '/image box starts/,/command line ends/d' Results.html
let num=1;
while (( num <= SEQS )); do
echo "
Structures derived from sequence $num:
" > combinedTemp.txt
sed -n "/image box starts here/,/download links end here/p" seq$num/Results.html >> combinedTemp.txt
echo "
" >> combinedTemp.txt
sed "s@id=\"@id=\"$num@g" combinedTemp.txt >> combined.txt
sed -i "s@src=\"INTERFACE_NAME.svg\"@src=\"seq${num}/INTERFACE_NAME.svg\"@g" combined.txt
let num++;
done
sed -i "N;/alignment file ends here/r combined.txt" Results.html
rm -f combined*.txt
INTERFACEDESC=`grep -r --after-context=1 dynalign.html ../../../../index.html | tail -n 1 | cut -d '>' -f 2 | cut -d '<' -f 1`
sed -i "s@Results@Results
($INTERFACEDESC)@g" Results.html
sed -i 's@ALIGNMENT_ROOT@dynalign/dynalign@g' Results.html
sed -i 's@INTERFACE_PAGE@dynalign/dynalign.html@g' Results.html
sed -i 's@INTERFACE_NAME@dynalign@g' Results.html
sed -i "s@FULL_RESULTS_DIRECTORY@ResultsPages/$DIRECTORY@g" Results.html
sed -i "s@COMMAND_LINE@dynalign-smp $HTMLCOMMAND@g" Results.html
sed -i 's@dynalign/ResultsPages@Predict2/ResultsPages@g' Results.html
sed -i 's@/seq@/dynalign/seq@g' Results.html
sed -i 's@"/dynalign@"dynalign@g' Results.html
sed -i 's@src="seq@src="dynalign/seq@g' Results.html
sed -i 's@/dynalign.config@/dynalign/dynalign.config@g' Results.html
fi
# Move all files created for this section into the dynalign directory.
mkdir dynalign;
mv seq* dynalign/;
mv dynalign.config dynalign/;
mv dynalign.ali dynalign/
mv Results.html dynalign/;
######################################################################
# Do the PARTS calculation.
######################################################################
# Create the unique script variables necessary for this calculation.
COMMAND=`echo $FULLCOMMAND | cut -d ';' -f '2'`
HTMLCOMMAND=`echo $FULLHTML | cut -d ';' -f '2'`
# Run the main calculation command.
runTool PARTS $COMMAND
# Generate results files and create results pages depending on the calculation mode.
if [[ "$MODETYPE" == "pp" ]]; then
OUTPUTTYPE="Plot"
PLOTBOUNDS="PLOT_BOUNDS_STRING"
# For each input sequence, create its results files and individual HTML page in a separate subfolder.
typeset -i i SEQS;
let SEQS=NUMSEQS;
let num=1;
while (( num <= SEQS )); do
# Make the next subfolder and move the relevant files into it.
# Then move to the subfolder.
mkdir seq${num};
mv PARTS${num}.txt seq${num}/PARTS.txt
cd seq${num}
# Create the plot files.
runTool ProbabilityPlot PARTS.txt PARTS.ps $PLOTBOUNDS --matrix
runTool ps2pdf PARTS.ps PARTS.pdf
runTool gs -dNOPAUSE -dBATCH -q -r300 -dJPEGQ=100 -sDEVICE=jpeg -sOutputFile=PARTS.jpeg PARTS.ps
runTool ProbabilityPlot PARTS.txt PARTS.svg $PLOTBOUNDS --matrix --svg
# If no errors occurred in results files generation, create a results page using a templated HTML file.
if [[ ! -s errors.txt ]]; then
copyTemplate ResultsPagePlots Results.html
sed -i "s@FULL_RESULTS_DIRECTORY@ResultsPages/$DIRECTORY/seq${num}@g" Results.html
sed -i "s@PFS@Text@g" Results.html
sed -i "s@pfs@txt@g" Results.html
fi
# If an error did occur in results file generation, create an error page using a templated HTML file, break out of the loop.
if [[ -s ../errors.txt ]]; then
break
fi
# Move back to the main directory and increment the loop.
cd ..
let num++;
done
# Create the combined structure results page and set the alignment div invisible.
copyTemplate ResultsPageManyAlignedStructures Results.html
sed -i 's@id="alignment" class="centerClass"@id="alignment" class="centerClass" style="display: none"@g' Results.html
else
OUTPUTTYPE="Structures"
# For each input sequence, create its results files and individual HTML page in a separate subfolder.
typeset -i i SEQS;
let SEQS=NUMSEQS;
let num=1;
while (( num <= SEQS )); do
# Make the next subfolder and move the relevant files into it.
# Then move to the subfolder.
mkdir seq${num};
mv PARTS${num}.ct seq${num}/PARTS.ct
cd seq${num}
# Create the comprehensive PS and PDF files.
runTool draw PARTS.ct PARTS.ps
if [[ ! -s ../errors.txt ]]; then ps2pdf PARTS.ps PARTS.pdf 1>/dev/null 2>>../errors.txt; fi
# If only one structure is present, create a JPEG and SVG file to be available to the user.
# Otherwise, split the CT file into individual structures and make CT, PS, PDF, and SVG files for each structure.
# Individual JPEG files are not created because they take too long and occupy too much space; they are made only if and when the user requests them.
NUMSTRUCTURES=0
if [[ ! -s ../errors.txt ]]; then
NUMSTRUCTURES=`grep -r showpage PARTS.ps | wc -l`
if (( $NUMSTRUCTURES == 1 )); then
if [[ ! -s ../errors.txt ]]; then gs -dNOPAUSE -dBATCH -q -r300 -dJPEGQ=100 -sDEVICE=jpeg -sOutputFile=PARTS.jpeg PARTS.ps 1>/dev/null 2>>../errors.txt; fi
runTool draw PARTS.ct PARTS.svg -n 1 --svg
else
if [[ ! -s ../errors.txt ]]; then
SEQLENGTH=`head -n 1 PARTS.ct | awk '{print $1}'`
let SEQLENGTH++;
awk '{print > (NR%"'"$SEQLENGTH"'"?i:i++)".ct";}' i=1 PARTS.ct
fi
typeset -i i END;
let END=$NUMSTRUCTURES;
let i=1;
while (( i <= END )); do
runTool draw PARTS.ct ${i}.ps -n $i
if [[ ! -s ../errors.txt ]]; then ps2pdf ${i}.ps ${i}.pdf 1>/dev/null 2>>../errors.txt; fi
runTool draw PARTS.ct ${i}.svg -n $i --svg
let i++;
done
fi
fi
# If no errors occurred in results files generation, create a results page using a templated HTML file.
# If no structures were generated, use a static notification HTML page as the results page.
# If only one structure was generated, place it in a specialized static HTML page.
# If multiple structures were generated, place it in a dynamic, expandable HTML page.
# If one or more structures were made, place the proper interface name, results directory, and command line in the results page.
if [[ ! -s ../errors.txt ]]; then
if grep -Fqs "contains no pairs" PARTS.svg
then
cp ../../../../Utilities/Pages/NoStructures.html Results.html
rm -f PARTS*
else
if (( $NUMSTRUCTURES == 1 )); then
copyTemplate ResultsPageSingleCTSingleStructure Results.html
else
copyTemplate ResultsPageSingleCTManyStructures Results.html
sed -i "s@STRUCTURE_NUMBER@$END@g" Results.html
awk '/download structure 1 links/,/structure 1 links end here -->/{print}' ../../../../../Utilities/Templates/ResultsPageSingleCTManyStructures.html > individualDiv.txt
typeset -i i END;
let END=$NUMSTRUCTURES;
let i=1;
while (( i <= END )); do
sed "s/1/$i/g" individualDiv.txt >> extendedDivs.txt
let i++;
done
sed -i '/download structure 1 links/,/structure 1 links end here -->/d' Results.html
sed -i "N;/Download Individual Structures/r extendedDivs.txt" Results.html
fi
fi
sed -i "s@FULL_RESULTS_DIRECTORY@ResultsPages/$DIRECTORY/seq${num}@g" Results.html
fi
# If an error did occur in results file generation, create an error page using a templated HTML file, break out of the loop.
if [[ -s ../errors.txt ]]; then
break
fi
# Move back to the main directory and increment the loop.
cd ..
let num++;
done
# Create the combined structure results page and set the alignment root in it.
copyTemplate ResultsPageManyAlignedStructures Results.html
sed -i 's@ALIGNMENT_ROOT@PARTS/PARTS@g' Results.html
fi
# If an error results page was not already made, build the combined results page.
if [[ ! -s ../errors.txt ]]; then
sed -i '/image box starts/,/command line ends/d' Results.html
let num=1;
while (( num <= SEQS )); do
echo "$OUTPUTTYPE derived from sequence $num:
" > combinedTemp.txt
sed -n "/image box starts here/,/download links end here/p" seq$num/Results.html >> combinedTemp.txt
echo "
" >> combinedTemp.txt
sed "s@id=\"@id=\"$num@g" combinedTemp.txt >> combined.txt
sed -i "s@src=\"INTERFACE_NAME.svg\"@src=\"seq${num}/INTERFACE_NAME.svg\"@g" combined.txt
let num++;
done
sed -i "N;/alignment file ends here/r combined.txt" Results.html
rm -f combined*.txt
fi
# Check for errors occurred in results files generation and initial results page generation.
# If none occurred, place the proper interface name and command line in the results page.
if [[ ! -s errors.txt ]]; then
INTERFACEDESC=`grep -r --after-context=1 PARTS.html ../../../../index.html | tail -n 1 | cut -d '>' -f 2 | cut -d '<' -f 1`
sed -i "s@Results@Results
($INTERFACEDESC)@g" Results.html
sed -i 's@INTERFACE_PAGE@PARTS/PARTS.html@g' Results.html
sed -i 's@INTERFACE_NAME@PARTS@g' Results.html
sed -i "s@FULL_RESULTS_DIRECTORY@ResultsPages/$DIRECTORY@g" Results.html
sed -i "s@COMMAND_LINE@PARTS $HTMLCOMMAND@g" Results.html
sed -i 's@PARTS/ResultsPages@Predict2/ResultsPages@g' Results.html
sed -i 's@/seq@/PARTS/seq@g' Results.html
sed -i 's@"/PARTS@"PARTS@g' Results.html
sed -i 's@src="seq@src="PARTS/seq@g' Results.html
sed -i 's@/PARTS.config@/PARTS/PARTS.config@g' Results.html
fi
# Move all files created for this section into the PARTS directory.
mkdir PARTS;
mv seq* PARTS/;
mv PARTS.config PARTS/;
if [[ "$OUTPUTTYPE" == "Structures" ]]; then mv PARTS.ali PARTS/; fi
mv Results.html PARTS/;
######################################################################
# Check for errors, then create the results page.
######################################################################
# If an error results page was not already made, put the results pages together into one big one.
if [[ ! -s Results.html ]]; then
copyTemplate ResultsPageManyAlignedStructures Results.html
sed -i '/results header starts/,/command line ends/d' Results.html
COMBINEDHEADER='
'
echo -e $COMBINEDHEADER > combined.txt
sed -n "/results header starts/,/command line ends/p" dynalign/Results.html > combinedTemp.txt
echo "
" >> combinedTemp.txt
sed "s@id=\"@id=\"1@g" combinedTemp.txt >> combined.txt
sed -n "/results header starts/,/command line ends/p" PARTS/Results.html > combinedTemp.txt
sed "s@id=\"@id=\"2@g" combinedTemp.txt >> combined.txt
sed -i 'N;/id="main"/r combined.txt' Results.html
rm -f combined*.txt
sed -i 's@INTERFACE_PAGE@Predict2/Predict2.html@g' Results.html
sed -i 's@INTERFACE_NAME@Predict a Secondary Structure Common to Two Sequences@g' Results.html
fi
######################################################################
# Perform end-of-calculation tasks
######################################################################
calcCompleted #send results email, notify web client, etc