Question regarding Bash program

Hello All,

I am trying to write a small bash script to make my life easier for an analysis. I have multiple folders and inside them are 10 output files with an extension .pdbqt What I am trying to do is to read the folder content and then make a PyMol (.pml) file to load the molecules and then display it. When I close the PyMol, the script should go to the next folder and do the same thing again. Below I am pasting the code, any help is greatly appreciated :o

#!/bin/bash                                                                                                                                                                                   
for f in Frog-mol-ryan_*; do
    if [ -d "${f}" ]; then
        echo Loading structures in Pymol: $f
        
cat <<EOF >log.pml                                                                                                                                                                           
                                                                                                                                                                                              
load /server/John/Docking-Studies/docking-FC996-analog/MR1004/test/Frog-mol-ryan_*/out_Frog-ryan-mol_ligand_*.pdbqt                                                            
                                                                                                                                                                                              
reset                                                                                                                                                                                         
load /server/John/Docking-Studies/Insilico-mod-FC996/docking-FC996-analog/PDBs/Holo-with-Bpore.pdb                                                                                
cmd.hide("everything","holo-with-Bpore")                                                                                                                                                 
cmd.show("cartoon"   ,"holo-with-Bpore")                                                                                                                                                 
cmd.show("sticks", "out_Frog-mol-ryan_ligand_*.pdbqt")                                                                                                                                                   
EOF                                                                                                                                                                                           

/usr/bin/pymol log.pml
    fi
done

The output log.pml is:

load /server/John/Docking-Studies/Insilico-mod-FC996/docking-FC996-analog/MR1004/test/Frog-mol-ryan_*/out__ligand_*.pdbqt

reset
load /server/John/Docking-Studies/Insilico-mod-FC996/docking-FC996-analog/PDBs/Holo-with-Bpore.pdb
cmd.hide("everything","holo-with-Bpore")
cmd.show("cartoon"   ,"holo-with-Bpore")
cmd.show("sticks", "out__ligand_*.pdbqt")

I am not able to pass the argument in cat EOF section.

Kindly help
Thanks

You've passed hardcoded text, rather than the variable $f ..

load /server/John/Docking-Studies/docking-FC996-analog/MR1004/test/Frog-mol-ryan_*/out_Frog-ryan-mol_ligand_*.pdbqt

becomes

load /server/John/Docking-Studies/Insilico-mod-FC996/docking-FC996-analog/MR1004/test/$f/out_$f*.pdbqt

Assuming the "out_Frog_ryan_mol" was a typo and should have been "out_Frog_mol_ryan"...

Hope this helps

Hello sea,
Thank you for your help. One small problem, the output files are named out_Frog-mol-ryan_ligand_05.pdbqt so if I set as "$f/out_$f*.pdbqt" its not able to find the files.
Kindly suggest.

Thanks

Oh the assumption was wrong :wink:

This should do the trick then:
load /server/John/Docking-Studies/Insilico-mod-FC996/docking-FC996-analog/MR1004/test/$f/out_Frog-mol-ryan_ligand_*.pdbq

Thanks sea,
Sorry to disturb you so much.
"out_Frog-mol-ryan_ligand_.pdbq" is not getting translated in the log.pml file so the visualizer is not able to understand the "" explicitly.

Let me explain the structure of directories. I have 100 folders named "Frog-mol-ryan_1, Frog-mol-ryan_2, Frog-mol-ryan_3 ... and so on till out_Frog-ryan-mol_ligand_100. Now each folder has 20 output files named "out_Frog-ryan-mol_ligand_1.pdbqt, out_Frog-ryan-mol_ligand_2.pdbqt .... and so on till out_Frog-ryan-mol_ligand_20.pdbqt". What I am trying to do is to make a bash script file which will go inside each folder (Frog-ryan-mol_1), grab all the "out_*.pdbqt" files and write a log.pml file with instructions to load all the molecules in the PyMol. Now after loading the molecules in PyMol, when I close the PyMol window the script will go to the next folder (Frog-ryan-mol_2) and do the same thing as the previous one.

Thank you so much for your time and patience :slight_smile:

Can you show us how should a single log.pml look like for a single directory?

cat <<EOF >log.pml

load /server/John/Docking-Studies/docking-FC996-analog/MR1004/test/Frog-mol-ryan_*/out_Frog-ryan-mol_ligand_*.pdbqt

reset
load /server/John/Docking-Studies/Insilico-mod-FC996/docking-FC996-analog/PDBs/Holo-with-Bpore.pdb
cmd.hide("everything","holo-with-Bpore")
cmd.show("cartoon" ,"holo-with-Bpore")
cmd.show("sticks", "out_Frog-mol-ryan_ligand_*.pdbqt")
EOF

/usr/bin/pymol log.pml

I mean actual file. Not the code that you are trying to use to generate it. Can you manually prepare one log.pml file that contains all the data for a single directory, that when run with "/usr/bin/pymol log.pml" will produce exepcted output for that directory? Test it by running "/usr/bin/pymol log.pml" and see if it is really working for that single directory.

Hello bartus11,

Thanks for your reply. I am attaching a zip files containing the folders and respective bash script and a dummy Pymol log file.

Thanks once again for the help :slight_smile:

Try this:

#!/bin/bash
for dir in Frog-mol-ryan_*; do
  if [ -d "${dir}" ]; then
    echo Loading structures in Pymol: $dir
    cat /dev/null > log.pml
    for file in $dir/*.pdbqt; do
      echo "load /server/John/Docking-Studies/Insilico-mod-FC996/docking-FC996-analog/MR1004/test/$file" >> log.pml
    done
    cat <<EOF>>log.pml
reset
load /server/John/Docking-Studies/Insilico-mod-FC996/docking-FC996-analog/PDBs/Holo-with-Bpore.pdb
cmd.hide("everything","holo-with-Bpore")
cmd.show("cartoon"   ,"holo-with-Bpore")
EOF
    for file in $dir/*.pdbqt; do
      echo "cmd.show(\"sticks\", \"`basename $file`\")" >> log.pml
    done
    /usr/bin/pymol log.pml
  fi
done

Hello bartus11,
Thank you so much for the help. The script is working like charm. With so little knowledge about the scripting I would have never figured that out.
I am trying to learn some scripting so that I could use if for my small analysis work, mostly copying, transferring and sorting type works. I have a linux machine at my lab with bash shell.
Could you suggest me some good reference to learn bash scripting (books or online) will be greatly appreciated :slight_smile:

Thanks once again for the help :slight_smile:

This site is quite comprehensive guide to BASH (with many examples too): Advanced Bash-Scripting Guide