File: add-chain-IDs.pl

package info (click to toggle)
macromoleculebuilder 4.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 122,488 kB
  • sloc: cpp: 23,632; python: 5,047; ansic: 2,101; awk: 145; perl: 144; makefile: 40; sh: 21
file content (57 lines) | stat: -rwxr-xr-x 1,250 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/perl -w

# (C) Daniel Larsson 2012

@id = ('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');

$nr = -1;
$prev_id = "";
$previous_res = -1;
$first = 1;

while($line = <>) {

   if( $line =~ /^TER/ ){
       $nr++;

       if( $nr >= $#id ){
           $nr = 0;
       }
       printf $line;
       $previous_res = -1;
   }
   elsif( $line =~ m/(ATOM.{17})(.)(.{4})(.*)/ ){

       $pre = $1;
       $current_id = $2;
       $current_res = $3;
       $post = $4;

       $current_res =~ s/\s*(\d+)/$1/;

       if( $prev_id ne $current_id || $current_res < $previous_res || $current_res > $previous_res + 1 ) {
           $nr++;

           if( $nr >= $#id ){
               $nr = 0;
           }

           if( $first != 1 ){
               printf "TER\n";
           }
       }

       printf "%s%1s%4d%s\n", $pre, $id[$nr], $current_res, $post;

       if( $first == 1 ){
           $first = 0;
       }

       $prev_id = $current_id;
       $previous_res = $current_res;
   }
   elsif ( $line !~ /^TER/ ) {
       print $line;
   }

}