0

I'm trying to undergo pdbqt-flexible files merge into one pdb using following script:

http://prosciens.com/prosciens/oldproscienssarl/files/flexrigidpdbqt2pdb_template.sh

Problematic fragment:

Let's merge the files

  1. First we clean up the model PDB

    grep ATOM ${FLEXPDBQT}_${MODEL}.pdb > ${FLEXPDBQT}_${MODEL}.pdb.tmp
    
  2. Next we create a list of residues

    cut -c 18-27 ${FLEXPDBQT}_${MODEL}.pdb.tmp > residuelistraw.tmp`
    cat residuelistraw.tmp | uniq > residuelist.tmp
    
  3. Then we split the model file into residues

    while read r
    do
    rns= echo $r | sed 's/ //g'
    egrep "[ \t]$r[ \t]" ${FLEXPDBQT}_${MODEL}.pdb.tmp > $rns.pdb.tmp
    sed -i 's/'$FLEXPDBQT'_'$MODEL'.pdb.tmp://' $rns.pdb.tmp
    

Currently it fails at #3 step yielding following error:

/flexrigidpdbqt2pdb_template.sh: line 133: $ rns.pdb.tmp: ambiguous redirect
sed: -e expression # 1, character 9: unknown option for the `s' command

I tried fix the error using some sed substitution:

rns=`echo "${r/ /}"`
echo $rns
egrep "[ \t]$r[ \t]" ${FLEXPDBQT}_${MODEL}.pdb.tmp > $rns.pdb.tmp
sed -i 's/'$FLEXPDBQT'_'$MODEL'.pdb.tmp://' $rns.pdb.tmp

But so far nothing changed.

My sed version is 4.4

Graeme
  • 2,971
  • 21
  • 26
MadEye
  • 19
  • 3
  • 2
    Could you please post the code here instead. We are not willing to take any security risk by downloading a `sh` file. – Mihir Luthra Oct 07 '19 at 11:58
  • Or at least edit your Q to show what is on line 133, and any sed commends you find in that script. Good luck. – shellter Oct 07 '19 at 12:23
  • sed -i 's/\'$FLEXPDBQT\'_\'$MODEL\'.pdb.tmp://' $rns.pdb.tmp – Šerg Oct 07 '19 at 13:14
  • Code is unfortunately quite long - I added the most problematic part now. – MadEye Oct 07 '19 at 19:32
  • 1
    Always quote your variable expansions. And what is `rns= echo $r | sed 's/ //g'` supposed to do? It surely won't set `rns` to `r` without spaces. – AlexP Oct 07 '19 at 20:04
  • If you want that, `rns=${r//[[:space:]]/}` is the more efficient and correct approach. – Charles Duffy Oct 07 '19 at 20:09
  • ```rns=`echo "${r/ /}"` ``` deletes only *one* space, and it does that very inefficiently (spinning up a whole new copy of your shell to run `echo` in). – Charles Duffy Oct 07 '19 at 20:12
  • 1
    Could you please try to build a [mre]? Right now, nobody else will have `.pdb` files that let them reproduce the problem, so nobody can test whether their answers will actually work. Of course, if you can simplify or isolate the problem to not *require* those files, that's all the better. – Charles Duffy Oct 07 '19 at 20:14

2 Answers2

2

On the "ambiguous redirection" error

An "ambiguous redirection" error doesn't come from sed at all, it comes from your shell.

An "ambiguous redirection" error means your shell can't start the command you gave it at all, because it wasn't able to perform the redirections requested as part of that command's environment.

In that case, the variable rns is empty.

That's because rns= echo $r | sed 's/ //g' doesn't assign rns to be the output of sed at all. Instead, it assigns a transient environment variable named rns to be an empty string, only for the duration of execution of echo $r (the output of which is sent to sed, and from there to the script's stdout).

Instead, use:

rns=${r//[[:space:]]/}

...or, less efficiently:

rns=$(sed -e 's/ //g' <<<"$r")

To avoid the same error in cases where the variable isn't empty, be sure you quote!

That is, instead of running ... >$file, always run ... >"$file", to ensure that unwanted string-splitting or glob expansion can't make an otherwise-valid redirection unworkable. (This doesn't happen with all shell versions, but that means that failing to quote causes your code's behavior to be unpredictable unless you know which shell release it's going to be run with!).


On the "unknown option" error

If you use / as the sigil separating parts of your sed command, then you must not have any /s in the data being substituted. If you cannot guarantee this, use a different sigil instead of /; for example, s@/foo/bar@/baz/qux@ works properly.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
0

It looks like that the essential part to repair that script was changing rns as you proposed to:

rns=${r//[[:space:]]/}

and subsequent quoting of the output:

sed -i 's/'$FLEXPDBQT'_'$MODEL'.pdb.tmp://' "$rns.pdb.tmp"

However, further problems come out with next step, not previously shown:

sed '/'"HN ${r}"'/ r '${rns}'.pdb.tmp' ${RIGIDPDBQT}_${LIGNAME}_${MODEL}_apo.pdb > "${RIGIDPDBQT}_${LIGNAME}_${MODEL}_apo.pdb.tmp"

It yields no output (and no error), propably due to another problem with sed (which I really can't understand).

It is quite hard to give here a working example - a lot of preformatted txt files. Despite that, the problem about which I asked is solved, thank you!

MadEye
  • 19
  • 3