Difference between revisions of "Split NMR-style multiple model pdb files into individual models"

From CCP4 wiki
Jump to navigationJump to search
Line 8: Line 8:
 
This one-liner splits the file models.pdb into individual pdb files named model_###.pdb.
 
This one-liner splits the file models.pdb into individual pdb files named model_###.pdb.
  
'''grep -n 'MODEL\|ENDMDL' models.pdb | '''
+
  grep -n 'MODEL\|ENDMDL' models.pdb | cut -d: -f 1 | \
'''cut -d: -f 1 | '''
+
  awk '{if(NR%2) printf "sed -n %d,",$1+1; else printf "%dp > model_%03d.pdb\n", $1-1,NR/2;}' | bash -sf
'''awk '{if(NR%2) printf "sed -n %d,",$1+1; else printf "%dp > model_%03d.pdb\n", $1-1,NR/2;}' | '''
 
'''bash -sf'''
 
  
 
== Bash script ==
 
== Bash script ==
  
'''i=1'''
+
  i=1
 
+
  while read -a line; do
'''while read -a line; do'''
+
    echo "${line[@]}" >> model_${i}.pdb
 
+
    [[ ${line[0]} == ENDMDL ]] && ((i++))
'''    echo "${line[@]}" >> model_${i}.pdb'''
+
  done < /path/to/file.pdb
 
 
'''    [[ ${line[0]} == ENDMDL ]] && ((i++))'''
 
 
 
'''done < /path/to/file.pdb'''
 
  
  
Line 30: Line 24:
 
Should be called as  
 
Should be called as  
  
awk -f script.awk < models.pdb
+
  awk -f script.awk < models.pdb
  
'''BEGIN {file = 0; filename = "model_"  file ".pdb"}'''
+
  BEGIN {file = 0; filename = "model_"  file ".pdb"}
 
+
  /ENDMDL/ {getline; file ++; filename = "model_" file ".pdb"}
'''/ENDMDL/ {getline; file ++; filename = "model_" file ".pdb"}'''
+
  {print $0 > filename}
 
 
'''{print $0 > filename}'''
 
  
  
 
== Perl script ==
 
== Perl script ==
  
'''$base='1g9e';open(IN,"<$base.pdb");@indata = <IN>;$i=0;'''
+
  $base='1g9e';open(IN,"<$base.pdb");@indata = <IN>;$i=0;
 
+
  foreach $line(@indata) {
'''foreach $line(@indata) {'''
+
  if($line =~ /^MODEL/) {++$i;$file="${base}_$i.pdb";open(OUT,">$file");next}
 
+
  if($line =~ /^ENDMDL/) {next}
'''if($line =~ /^MODEL/) {++$i;$file="${base}_$i.pdb";open(OUT,">$file");next}'''
+
  if($line =~ /^ATOM/ || $line =~ /^HETATM/) {print OUT "$line"}
 
+
  }
'''if($line =~ /^ENDMDL/) {next}'''
 
 
 
'''if($line =~ /^ATOM/ || $line =~ /^HETATM/) {print OUT "$line"}'''
 
 
 
'''}'''
 
  
  
 
Back to [[Useful scripts (aka smart piece of code)]]
 
Back to [[Useful scripts (aka smart piece of code)]]

Revision as of 15:08, 16 December 2010

This assumes that you have a correctly formatted pdb file that contains both MODEL and ENDMDL records.


Bash/awk one-liner

This one-liner splits the file models.pdb into individual pdb files named model_###.pdb.

 grep -n 'MODEL\|ENDMDL' models.pdb | cut -d: -f 1 | \
 awk '{if(NR%2) printf "sed -n %d,",$1+1; else printf "%dp > model_%03d.pdb\n", $1-1,NR/2;}' |  bash -sf

Bash script

 i=1
 while read -a line; do
   echo "${line[@]}" >> model_${i}.pdb
   [[ ${line[0]} == ENDMDL ]] && ((i++))
 done < /path/to/file.pdb


Awk script

Should be called as

 awk -f script.awk < models.pdb
 BEGIN {file = 0; filename = "model_"  file ".pdb"}
 /ENDMDL/ {getline; file ++; filename = "model_" file ".pdb"}
 {print $0 > filename}


Perl script

 $base='1g9e';open(IN,"<$base.pdb");@indata = <IN>;$i=0;
 foreach $line(@indata) {
 if($line =~ /^MODEL/) {++$i;$file="${base}_$i.pdb";open(OUT,">$file");next}
 if($line =~ /^ENDMDL/) {next}
 if($line =~ /^ATOM/ || $line =~ /^HETATM/) {print OUT "$line"}
 }


Back to Useful scripts (aka smart piece of code)