简体   繁体   中英

Parsing restraints with bash and awk

I have restraints that look like

G6N-D5C-?: (116.663, 177.052, 29.149) K87CD/E85CB/E94CB/H32CB/Q21CB
L12N-T11C-?: (128.977, 175.109, 174.412) K158C/H60C/A152C/N127C/Y159C(notH60C)
K14N-E13C-?: (117.377, 176.474, 29.823) E187CB/V78CB
A75N-Q74C-?: (123.129, 177.253, 23.513) V131CG1/V135CG1/V78CG1

and I need to convert them with output:

assign (resid 5 and name C ) (resid 87 and name CD or resid 85 and name CB or resid 94 and name CB or resid 32 and name CB or resid 21 and name CB ) 3.5 2.5 8.5 ! G6N-D5C-?: (116.663, 177.052, 29.149) K87CD/E85CB/E94CB/H32CB/Q21CB
assign (resid 11 and name C ) (resid 158 and name C or resid 60 and name C or resid 152 and name C or resid 127 and name C or resid 159 and name C ) 3.5 2.5 8.5 ! L12N-T11C-?: (128.977, 175.109, 174.412) K158C/H60C/A152C/N127C/Y159C(notH60C)
assign (resid 13 and name C ) (resid 187 and name CB or resid 78 and name CB ) 3.5 2.5 8.5 ! K14N-E13C-?: (117.377, 176.474, 29.823) E187CB/V78CB
assign (resid 74 and name C ) (resid 131 and name CG1 or resid 135 and name CG2 or resid 78 and name CG1 ) 3.5 2.5 8.5 ! A75N-Q74C-?: (123.129, 177.253, 23.513) V131CG1/V135CG1/V78CG1

I have tried awk, but I couldn't figure out how to break up the array. Please help me convert this, it is brutal to do by hand.

You'll want to split the first word on - and examine the 2nd element.
Then split the last word on / and examine each element.

Assuming GNU awk, read carefully about split() and match() from http://www.gnu.org/software/gawk/manual/html_node/String-Functions.html#String-Functions


Feeling generous:

gawk '
  function extract(str, fmt,      m) {
    if (match(str, /^.([0-9]+)(.+)/, m)) printf fmt, m[1], m[2]
  }
  {
    split($1, a, /-/)
    extract(a[2], "assign (resid %d and name %s ) (")
    n = split($NF, a, /\//)
    sep = ""
    for (i=1; i<=n; i++) {
      extract(a[i], sep "resid %d and name %s ")
      sep = "or "
    }
    print ") 3.5 2.5 8.5 !", $0
  }
'

Here is one way using perl :

#!/usr/bin/perl 

use strict;
use warnings;
use autodie;

open my $fh, '<', 'restraints.file';

while (<$fh>) {
    my @values = map { /.(\d+)(\w+)/; $1, $2 } split '/', (split)[-1];
    my ( $resid, $name ) = /^[^-]+-.(\d+)(\w+)-/;
    print "assign (resid $resid and name $name ) (";
    print join ( " or ", 
        map  { "resid $values[$_] and name $values[$_ + 1]" } 
        grep { not $_ % 2 } 0 .. $#values 
    );
    print " ) 3.5 2.5 8.5 ! $_";
}

Output:

assign (resid 5 and name C ) (resid 87 and name CD or resid 85 and name CB or resid 94 and name CB or resid 32 and name CB or resid 21 and name CB ) 3.5 2.5 8.5 ! G6N-D5C-?: (116.663, 177.052, 29.149) K87CD/E85CB/E94CB/H32CB/Q21CB
assign (resid 11 and name C ) (resid 158 and name C or resid 60 and name C or resid 152 and name C or resid 127 and name C or resid 159 and name C ) 3.5 2.5 8.5 ! L12N-T11C-?: (128.977, 175.109, 174.412) K158C/H60C/A152C/N127C/Y159C(notH60C)
assign (resid 13 and name C ) (resid 187 and name CB or resid 78 and name CB ) 3.5 2.5 8.5 ! K14N-E13C-?: (117.377, 176.474, 29.823) E187CB/V78CB
assign (resid 74 and name C ) (resid 131 and name CG or resid 135 and name CG or resid 78 and name CG ) 3.5 2.5 8.5 ! A75N-Q74C-?: (123.129, 177.253, 23.513) V131CG1/V135CG1/V78CG1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM