Renumber Residues in .pdb


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Renumber Residues in .pdb
# 1  
Old 11-05-2010
Renumber Residues in .pdb

Hello scripting Gurus!

I am dealing with a .pdb script which has the following general format:
Code:
ATOM    920  C   GLY B 103     -13.977   7.468 -11.253  1.00  0.00           C  
ATOM    921  O   GLY B 103     -14.817   7.213 -12.116  1.00  0.00           O  
ATOM    922  H   GLY B 103     -11.342   7.827 -11.132  1.00  0.00           H  
ATOM    923  HA2 GLY B 103     -13.483   9.303 -10.207  1.00  0.00           H  
ATOM    924  HA3 GLY B 103     -13.734   9.500 -11.952  1.00  0.00           H  
TER
ATOM    925  N   MET B 104     -13.601   6.588 -10.332  1.00  0.00           N  
ATOM    926  CA  MET B 104     -14.127   5.222 -10.341  1.00  0.00           C  
ATOM    927  C   MET B 104     -15.232   5.094  -9.286  1.00  0.00           C  
ATOM    928  O   MET B 104     -16.396   4.872  -9.620  1.00  0.00           O  
ATOM    929  CB  MET B 104     -12.929   4.298 -10.093  1.00  0.00           C  
ATOM    930  CG  MET B 104     -13.316   2.824 -10.092  1.00  0.00           C

I need to renumber the 6th column which increases each time there is a TER line. I was thinking something like i=1 $5=i if $1='TER' then $5=i+1 but I am struggeling to get any manipulation. Is there a way to preserve the file's spacing without specifying the number of spaces for each column?

I am very grateful for any help. MBNSmilie

Last edited by Scott; 11-07-2010 at 05:44 AM.. Reason: Code tags
# 2  
Old 11-06-2010
Got Perl?
Code:
eturk-linux 20:13:50 (~/temp/pl)> cat filter.pl

#!/usr/bin/perl -w

open(IN,"dat.pdb") || die("Could not open infile!");
open(OUT,">new.pdb") || die("Could not open outfile!");
my $i = 1;
foreach $line (<IN>) {
  if ($line =~ /^TER/) {
    $i++;
    print(OUT $line);
  } else {
    @splitLine = split(/ /, $line);
    $splitLine[5] = $i;
    print(OUT join(' ',@splitLine));
  }
}

eturk-linux 20:13:56 (~/temp/pl)> cat dat.pdb

ATOM 920 C GLY B 103 -13.977 7.468 -11.253 1.00 0.00 C
ATOM 921 O GLY B 103 -14.817 7.213 -12.116 1.00 0.00 O
ATOM 922 H GLY B 103 -11.342 7.827 -11.132 1.00 0.00 H
ATOM 923 HA2 GLY B 103 -13.483 9.303 -10.207 1.00 0.00 H
ATOM 924 HA3 GLY B 103 -13.734 9.500 -11.952 1.00 0.00 H
TER
ATOM 925 N MET B 104 -13.601 6.588 -10.332 1.00 0.00 N
ATOM 926 CA MET B 104 -14.127 5.222 -10.341 1.00 0.00 C
ATOM 927 C MET B 104 -15.232 5.094 -9.286 1.00 0.00 C
ATOM 928 O MET B 104 -16.396 4.872 -9.620 1.00 0.00 O
ATOM 929 CB MET B 104 -12.929 4.298 -10.093 1.00 0.00 C
ATOM 930 CG MET B 104 -13.316 2.824 -10.092 1.00 0.00 C

eturk-linux 20:14:12 (~/temp/pl)> ./filter.pl

eturk-linux 20:14:21 (~/temp/pl)> cat new.pdb

ATOM 920 C GLY B 1 -13.977 7.468 -11.253 1.00 0.00 C
ATOM 921 O GLY B 1 -14.817 7.213 -12.116 1.00 0.00 O
ATOM 922 H GLY B 1 -11.342 7.827 -11.132 1.00 0.00 H
ATOM 923 HA2 GLY B 1 -13.483 9.303 -10.207 1.00 0.00 H
ATOM 924 HA3 GLY B 1 -13.734 9.500 -11.952 1.00 0.00 H
TER
ATOM 925 N MET B 2 -13.601 6.588 -10.332 1.00 0.00 N
ATOM 926 CA MET B 2 -14.127 5.222 -10.341 1.00 0.00 C
ATOM 927 C MET B 2 -15.232 5.094 -9.286 1.00 0.00 C
ATOM 928 O MET B 2 -16.396 4.872 -9.620 1.00 0.00 O
ATOM 929 CB MET B 2 -12.929 4.298 -10.093 1.00 0.00 C
ATOM 930 CG MET B 2 -13.316 2.824 -10.092 1.00 0.00 C

This User Gave Thanks to turk451 For This Post:
# 3  
Old 11-06-2010
Renumbers any number starting with 1

Awesome script, I just installed perl and ran the script. It works but, if one of the columns before the 6th column includes the number 1 then it too is renumbered. I only need $i=splitline [5] =1. I tried manipulating the script and my ignorance is definately a limiting factor. Is it possible to move the splitLine designators outside of the foreach?

Thank you for all of your help.
# 4  
Old 11-06-2010
Quote:
It works but, if one of the columns before the 6th column includes the number 1 then it too is renumbered. I only need $i=splitline [5] =1 ... Is it possible to move the splitLine designators outside of the foreach?
There is actually no check for the number "1" anywhere in the script. The "foreach" just iterates over all of the lines of the file, in order. The "if" statement checks to see if the line begins with "TER". If so, then the value of i is incremented, and the line is left unchanged and printed to the output. If not, then the line is split into an array of strings using the space character. The 6th element in the array is replaced with the current value of i. Then the array of strings is joined together with a space character into a single string, which is what is printed to the output.

Maybe there is some extra space on some lines which is moving the position of the column over by one? If so, maybe you could split on multiple spaces "/ +/" instead of just the single space in the current split call, but keep in mind when when you rejoin the line, if there were multiple spaces in the original data line, they would be replaced by a single space in the output.
This User Gave Thanks to turk451 For This Post:
# 5  
Old 11-07-2010
Code:
awk -v n=1 '/^TER/ {n++;print;next} {$6=n}1' infile

This User Gave Thanks to rdcwayx For This Post:
# 6  
Old 11-07-2010
Code:
awk '{if(/^TER/)i++;else $6=i+1}1' infile

This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 11-07-2010
Is there a way to preserve the tab spacing when using awk without specifying the column spacing with %?
Login or Register to Ask a Question

Previous Thread | Next Thread

4 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove line containing string and renumber

Hello, I have some files in a directory and a short list of strings. I want to loop through the files and remove lines containing the string and renumber. There are some issues. The first is the strings that can contain troublesome characters like single quotes and parenthesis. Here is one... (12 Replies)
Discussion started by: LMHmedchem
12 Replies

2. Shell Programming and Scripting

awk script to renumber

I have input file like MODEL 1 ATOM 1 N GLY 121 27.310 57.180 59.640 1.00 0.00 ATOM 2 H1 GLY 121 27.320 57.560 60.610 1.00 0.00 MODEL 2 ATOM 1 N GLY 121 28.430 56.080 59.750 1.00 0.00 ATOM 2 H1 GLY 121 ... (2 Replies)
Discussion started by: dsp80
2 Replies

3. Shell Programming and Scripting

Renumber position 88-94 inside all files matching criteria inside folder

There are 4 files inside one folder matching criteria i.e. File name = ABCJmdmfbsjopXXXXXXX_mm-dd-yyyy_XXX.data Here is the Code which find the files matching criteria:- TS=`date +"%m-%d-%Y"`| for fname in `find . -name "ABCJmdmfbsjop???????_${TS}*.data"` do # Matching File Processing Code.... (1 Reply)
Discussion started by: lancesunny
1 Replies

4. UNIX Desktop Questions & Answers

Renumber column using Awk

I have a data set similar to that below and I need to add a column $2 (which numbers the lines from 1- end) Text Input Output x y z Text Input Output x y z Text Input Output x y z Text Input Output x ... (1 Reply)
Discussion started by: marybnewcomb
1 Replies
Login or Register to Ask a Question