Quantcast
Viewing all articles
Browse latest Browse all 33

For loop of bash script not working, reading lthe next lines within the previous loop [duplicate]

I cannot solve the for loop error in bash script.

The bash script is for extracting data from remote server, copying mapped files locally, and then executing a tcl script.The above process should be iterated for the number of files mentioned originally in the pdb.dat file, and called in each iteration within a for loop.

The code is as follows:

#!/bin/bash#SBATCH --job-name=Convert96#SBATCH -A d4#SBATCH -p ihub#SBATCH -c 2#SBATCH --time=4-00:00:00#SBATCH --mail-user=khabeer.azhar@ihub-data.iiit.ac.in#SBATCH --mail-type=ALL#SBATCH --output=convert.out#SBATCH --error=convert.errVMD_EXEC="/opt/vmd-1.9.3/bin/vmd"# Path to your Tcl script##TCL_SCRIPT="/home2/khabeer.azhar/Charge_Cal/C_try/C1.tcl"dat="pdb.dat"   # Replace with your input data fileabsolutepath="/share6/d4/plas20k_outputs"  # Replace with the correct pathout="charge_protein.dat"out2="charge_ligand.dat"# Initialize output filesecho "index.pdb(for protein) charge" > "$out"echo "index.pdb(for ligand) charge" > "$out2"while IFS= read -r line; do    echo "LOOP"    idx=$(echo "$line" | awk '{print $1}')    pdb=$(echo "$line" | awk '{print $2}')    echo "Copying file: $idx.$pdb.tar from $absolutepath"    idxpdb="$idx.$pdb"    # scp from remote server    scp -r khabeer.azhar@ada:"$absolutepath/$idx.$pdb.tar" .    # Check if the file was successfully copied    if [ -f "$idx.$pdb.tar" ]; then        echo "File $idx.$pdb.tar copied successfully."        tar -xf "$idx.$pdb.tar" --wildcards "$idx.$pdb/complex_solvated.p*"        if [ $? -ne 0 ]; then            echo "Error extracting $idx.$pdb.tar"            continue        fi        prmtop_file="$idx.$pdb/complex_solvated.prmtop"        pdb_file="$idx.$pdb/complex_solvated.pdb"        sed -e "s|set prmtop_file \"index.pdb/complex_solvated.prmtop\"|set prmtop_file \"$prmtop_file\"|" \            -e "s|set pdb_file \"index.pdb/complex_solvated.pdb\"|set pdb_file \"$pdb_file\"|" \            -e "s|puts \$O1 \"index.pdb \$protein_charge\"|puts \$O1 \"$idxpdb \$protein_charge\"|" \            -e "s|puts \$O2 \"index.pdb \$total_ligand_charge\"|puts \$O2 \"$idxpdb \$total_ligand_charge\"|" \            Charge_VMD_tmp.tcl > Charge_cal_tmp.tcl        # Load files into VMD and extract charges        $VMD_EXEC -dispdev text -e Charge_cal_tmp.tcl        if [ $? -ne 0 ]; then            echo "Error executing VMD for $idxpdb"            continue        fi        echo "tcl code finished"    else        echo "Error: File $idx.$pdb.tar not found or could not be copied."    fi    # Clean up extracted files    rm -r "$idx.$pdb" "$idx.$pdb.tar"    rm Charge_cal_tmp.tcl    echo "before going to next loop"done < "$dat"echoecho "Charge Calculation Completed"

The tcl code is as follows:

# Replace "path/to/your/complex.prmtop" with the actual path to your complex PRMTOP fileset O1 [open "charge_protein.dat" a]set O2 [open "charge_ligand.dat" a]    set prmtop_file "index.pdb/complex_solvated.prmtop"    set pdb_file "index.pdb/complex_solvated.pdb"    # Load the PRMTOP and PDB files into VMD    mol new $prmtop_file type parm7    mol addfile $pdb_file    # Select all protein atoms in the complex    set protein [atomselect top "protein"]    set Pnum [$protein num]    set Pname [$protein get index]    set protein_charge 0.0    for {set f 0} {$f < $Pnum} {incr f} {        set N [lindex $Pname $f]        set Nc [atomselect top "protein and index $N"]        set C [$Nc get charge]        set protein_charge [expr {$protein_charge + $C}]        unset N C    }    puts $O1 "index.pdb $protein_charge"    unset protein Pnum Pname protein_charge    # Select all ligand atoms in the complex    set ligands [atomselect top "resname LIG"]    set total_ligand_charge 0.0    set Lnum [$ligands num]    set Lname [$ligands get index]    # Loop over each ligand to calculate its charge    for {set g 0} {$g < $Lnum} {incr g} {        set N [lindex $Lname $g]        set Nc [atomselect top "resname LIG and index $N"]        set C [$Nc get charge]        set total_ligand_charge [expr {$total_ligand_charge + $C}]        unset N C    }    puts $O2 "index.pdb $total_ligand_charge"    unset ligands total_ligand_charge    mol delete top##  exec rm -r $index.$pdb $index.$pdb.tar#   unset index pdb prmtop_file pdb_file    unset prmtop_file pdb_file##close $O1##close $O2

The error is as follows (AFTER ONE ITERATION OF CALCULATION, THE CODE IS CALLING THE NEXT LINES OF pdb.dat AND MENTIONING AS INVALID):

7 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 45290.0atomselect45310.0404530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569Debug: Finished Tcl scriptinvalid command name "18720"invalid command name "8368"Info) VMD for LINUXAMD64, version 1.9.3 (November 30, 2016)Info) Exiting normally.tcl code finishedremoved temporary tcl file, index and pdb idbefore going to next loopCharge Calculation Completed

Any quick suggestions will be extremely appreciated.Thanks in advance.


Viewing all articles
Browse latest Browse all 33

Trending Articles