1

I would like to print pattern of Cys residue from each line given in file.tsv. file.tsv has two coloumns as sequenceID and Sequence. from the second column sequence first character "C" should be printed as C, if the next immediate residue is not C then the code should print C#. # should occur only one time for n number of various amino acid occurrence.

So when in Column if "C" is followed by another character I would like to print # after "C". so if sequence column has value DCFRCGHCC then it should print in the third column C#C#CC.

Example input:

c32_g1_i1_ 3GQKAKLKVPVFFLHRRGSICSSFYLMFSFEIKKK*TSKN*CFVCVRVRNRERAGVKCAHVYCPMFNGTQTH*IIISSLNS
c32_g1_i1_ 6AV*TADDDLVRLCSIEHGTIHMCTLYTCCTLTVTHTYTHKTLIFACLFFFNFKGEHQIERAANRTSSM*KKHRNF*LGLLAX

The output should be three columns: sequenceID, Sequence, Cys pattern

c32_g1_i1_3,GQKAKLKVPVFFLHRRGSICSSFYLMFSFEIKKK*TSKN*CFVCVRVRNRERAGVKCAHVYCPMFNGTQTH*IIISSLNS,C#C#C#C#C
c32_g1_i1_6,AV*TADDDLVRLCSIEHGTIHMCTLYTCCTLTVTHTYTHKTLIFACLFFFNFKGEHQIERAANRTSSM*KKHRNF*LGLLAX,C#C#CC#C 
  • 3
    You can't expect most of us to be biologists or to deal smoothly with patterns of Cys, fasta sequences etc. Please rephrase the question so that knowing biology to that extent is not a must to understand it. – kos Oct 7 '15 at 8:02
  • 2
    How you're arriving at C#C#C#C#C and C#C#CC#C is not entirely clear. Are you inserting a # when the next character isn't a C? – Oli Oct 7 '15 at 8:07
  • 1
    As stated this question is a bad fit here, we a linux computer enthusiasts not biologists (well some may be) How ever you can ask at Biology stackexchange ( theres a stack for everything these days :) ) biology.stackexchange.com – Mark Kirby Oct 7 '15 at 8:07
  • 1
    So when in line if "C" is followed by another character I would like to print # after "C". so if line is DCFRCGHCC then it should have C#C#CC. many thanks for replying so quick – Trupti Oct 7 '15 at 8:09
  • So take the 2nd string, print only the Cs, separate them with a # if not immediately followed by another C. Print the output on the same line, after the already occurring 2 fields, comma separating all 3 fields. Is that correct? – Arronical Oct 7 '15 at 8:46
5

The first one-liner / full script parse and convert the file format described in the question; the second full script parses and converts a FASTA file format.


#1

Golfed one-liner:

perl -lane 'my $s;my @m=$F[1]=~/C.?/g;foreach(@m){$_ eq"CC"?$s.="C":$s.="C#"}push(@F,$s);print(join(",",@F))' infile

Expanded full script:

#!/usr/bin/perl

use strict;
use warnings;

@ARGV == 1 || die("Usage: <command> <input_file>\n");

open(my $in, $ARGV[0]) || die("Could not open input file \"$ARGV[0]\": $!\n");

while(<$in>) {
    my $string;
    my @fields = split(" ");
    my @matches = $fields[1] =~ /C.?/g;
    foreach(@matches) {
        $_ eq "CC" ? $string .= "C" : $string .= "C#"
    }
    push(@fields, $string);
    print(join(",", @fields) . "\n")
}

close($in);

exit

Explanation:

  • The input file is processed line by line;
  • Each line is splitted into two strings, the part before the space and the part after the space;
  • Each substring made of a "C" character optionally followed by another character (optionally to catch also a "C" character at the end of the string) in the second string is evaluated, and if the character following the "C" is a "C", "C" is appended to the end of a custom temporary string; otherwise "C#" is appended at the end of the custom temporary string;
  • The first, second and custom temporary string are printed, comma-separated, followed by a newline;

Sample output:

% cat infile
c32_g1_i1_3 GQKAKLKVPVFFLHRRGSICSSFYLMFSFEIKKK*TSKN*CFVCVRVRNRERAGVKCAHVYCPMFNGTQTH*IIISSLNS
c32_g1_i1_6 AV*TADDDLVRLCSIEHGTIHMCTLYTCCTLTVTHTYTHKTLIFACLFFFNFKGEHQIERAANRTSSM*KKHRNF*LGLLAX
% perl -ne 'my $s;my @f=split(" ");my @m=$f[1]=~/C.?/g;foreach(@m){$_ eq"CC"?$s.="C":$s.="C#"}push(@f,$s);print(join(",",@f)."\n")' infile
c32_g1_i1_3,GQKAKLKVPVFFLHRRGSICSSFYLMFSFEIKKK*TSKN*CFVCVRVRNRERAGVKCAHVYCPMFNGTQTH*IIISSLNS,C#C#C#C#C#
c32_g1_i1_6,AV*TADDDLVRLCSIEHGTIHMCTLYTCCTLTVTHTYTHKTLIFACLFFFNFKGEHQIERAANRTSSM*KKHRNF*LGLLAX,C#C#CC#C#

#2

Expanded full version:

#!/usr/bin/perl

use strict;
use warnings;

@ARGV == 1 || die("Usage: <command> <input_file>\n");

open(my $in, $ARGV[0]) || die("Could not open input file \"$ARGV[0]\": $!\n");
open(my $tmp, "+>", "tmpfile") || die("Could not create temporary file \"tmpfile\": $!\n");

select($tmp);

while(<$in>) {
    if(/^>/) {
        s/$/ /
    }
    if(my $next = <$in>) {
        if($next !~ /^>/) {
            chomp
        }
        print;
        seek($in, -length($next), 1)
    }
    else {
        print
    }
}

close($in);

seek($tmp, 0, 0);

select(STDOUT);

while(<$tmp>) {
    my $string;
    my @fields = split(/ (?!.* )|\n/);
    my @matches = $fields[1] =~ /C.?/g;
    foreach(@matches) {
        $_ eq "CC" ? $string .= "C" : $string .= "C#"
    }
    push(@fields, $string);
    print(join(",", @fields) . "\n")
}

close($tmp);

unlink("tmpfile");

exit

Explanation:

  • The input file is processed line by line;
  • If the current line starts with a > character, a space is appended to the line; if a following line exists and doesn't start with a > character, the newline character is stripped from the current line; the current line is printed to a temporary file;
  • The temporary file is processed line by line;
  • Each line is splitted into two strings, the part before the last space and the part after the last space;
  • Each substring made of a "C" character optionally followed by another character (optionally to catch also a "C" character at the end of the string) in the second string is evaluated, and if the character following the "C" is a "C", "C" is appended to the end of a custom temporary string; otherwise "C#" is appended at the end of the custom temporary string;
  • The first, second and custom temporary string are printed, comma-separated, followed by a newline;
  • The temporary file is removed;

Sample output:

% cat infile 
>c32_g1_i1_
3GQKAKLKVPVFFLHRRGSICSSFYLMFSFEIKKK*TSKN*CFVCVRVRNRERAGVKCAHVYCPMFNGTQTH*IIISSLNS
3GQKAKLKVPVFFLHRRGSICSSFYLMFSFEIKKK*TSKN*CFVCVRVRNRERAGVKCAHVYCPMFNGTQTH*IIISSLNS
>c32_g1_i1_
6AV*TADDDLVRLCSIEHGTIHMCTLYTCCTLTVTHTYTHKTLIFACLFFFNFKGEHQIERAANRTSSM*KKHRNF*LGLLAX
6AV*TADDDLVRLCSIEHGTIHMCTLYTCCTLTVTHTYTHKTLIFACLFFFNFKGEHQIERAANRTSSM*KKHRNF*LGLLAX
% perl script.pl infile 
>c32_g1_i1_,3GQKAKLKVPVFFLHRRGSICSSFYLMFSFEIKKK*TSKN*CFVCVRVRNRERAGVKCAHVYCPMFNGTQTH*IIISSLNS3GQKAKLKVPVFFLHRRGSICSSFYLMFSFEIKKK*TSKN*CFVCVRVRNRERAGVKCAHVYCPMFNGTQTH*IIISSLNS,C#C#C#C#C#C#C#C#C#C#
>c32_g1_i1_,6AV*TADDDLVRLCSIEHGTIHMCTLYTCCTLTVTHTYTHKTLIFACLFFFNFKGEHQIERAANRTSSM*KKHRNF*LGLLAX6AV*TADDDLVRLCSIEHGTIHMCTLYTCCTLTVTHTYTHKTLIFACLFFFNFKGEHQIERAANRTSSM*KKHRNF*LGLLAX,C#C#CC#C#C#C#CC#C#
  • This code works perfectly fine. And many thanks for your detailed explaination – Trupti Oct 7 '15 at 8:59
  • @Trupti You're welcome, although I don't think it was actually explained well; I've added an explanation of the script, if you want to double-check whether it actually does exactly what you want. – kos Oct 7 '15 at 9:43
  • but it also allowed me to understand how the while loop worked – Trupti Oct 7 '15 at 10:23
  • @Trupti Apparently the real format of your input file is the one currently shown in your question; can you confirm that? If so I'll have to change my answer. – kos Oct 7 '15 at 14:20
  • @kos Yeah, sorry about that. The Fasta format includes >seqname and Oli, very understandably, assumed the > was meant for quoting. Since the OP mentions Fasta sequences, that should be the right format. – terdon Oct 7 '15 at 14:41
0

Here's an awk version that might work for you

awk '{OFS=","; $3=$2; sub(/[^C]+/,"",$3); gsub(/[^C]+/,"#",$3); print}' file

It just duplicates the second field, then strips any non-empty initial sequence of non-C characters in the duplicate, then replaces each remaining sequence of non-C characters in it with #.

  • This also works great! thank you for explaining another way to work – Trupti Oct 7 '15 at 12:45
  • This was clever, but apparently the real format of the input file is the one currently shown in the question, so apparently this answer has been invalidated as well. – kos Oct 7 '15 at 14:23
  • @kos thanks for the heads-up - baleeted – steeldriver Oct 7 '15 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.