I'm looking to splice out specific amino acid alignment positions as a continuous motif from an alignment (~15 000 sequences). The code is written in Perl (I agree not many developers now) but splicing method is awk which a lot of you know. Please note the hash is "tied" (not shown), this just means the order of the sequences is preserved. I can get the awk code below to return single amino acid positions.
I am struggling to get a loop working in awk.
I agree the Perl wrapper makes it more complicated, but its part of a huge data pipeline (all written in Perl).
#!/usr/local/ActivePerl-5.20/bin/env perl
#use strict;
use warnings;
# I loaded the alignment into a hash, where key = sequence and value = protein
my %dna = (
virus1 => "LGVLVILLMVQEGLKKRMTTKIIISTSMAVLVAMILGGFSMSDLAKLAILMGATFAEMNTGGDVAHLALIAAFKVRPALLVSFIF",
virus2 => "LGVLVILLMVQEGLKKRMTTKIIISTSMAVLVAMILGGFSMSDLAKLAILMGATFAEMNTGGDVXXXXXXAAFKVRPALLVSFIF",
virus3 => "LGVLVILLMVQEGLKKRMTTKIIISTSMAVLVAMILGGFSMSDLAKLAILMGATFAEMNTGGZZZZZZLIAAFKVRPALLVSFIF"
);
# I define the start and end of the alignment positions I am splicing
my $n = 1;
my $z = 5;
# I turn the alignment into a string with separated by carriage returns
my @array = map {"$dna{$_}"} keys %dna;
my $string = join "\n", @array;
# This is the awk loop, note in Perl the "``" means issue a shell command
my $splice = `echo "$string" | awk 'BEGIN {FS=""} {x = $n; while (x < $z); print \$ x; ++x}' 2>1`;
print $splice;
__DATA__
Expected output
LGVLV
LGVLV
LGVLV
Observed output I get the original sequences returned
I am familiar with Python/Perl solutions of line by line processing but awk is more efficient IMO.
while
only encloses the first statement after it, and not the second. You need{ }
for that. If you could explain what that awk is supposed to be doing (I'm pretty sure you've copied it wrong, is there really a space between\$
andx
?), I should be able to post an answer. Do you just want the first 5 characters of each sequence? Don't you want positions$n
to$z
(3-7)? $\endgroup$ – terdon Feb 25 '19 at 0:54