#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    15
    Rep Power
    0

    How to find and replace multiple words and matching them with perl


    Hi to all PERL programmers,
    I have data like this
    DATA HAVE
    pop A B C D E

    P1 T/T C/C C/C T/T C/C
    P2 A/A G/G C/C T/T C/C
    1 A/A G/G C/C T/T C/C
    2 A/A G/G C/C T/T C/C
    3 A/T A/C A/G A/T A/C
    4 T/A T/G T/C T/A T/G
    5 G/A G/T G/C G/A G/T
    6 C/A C/T C/G C/A C/T
    1.first I want to convert all A/A to A, T/T to T, C/C to C, G/G to G, Z/Z to - and -/- to -
    2. Now I want to know status from A to E by comparing P1 with P2, if P1=P2 then status from A to E is mono or any one of P1 or P2 contains Z/Z or -/- then status from A to E is mono else status from A to E is poly
    3. I want to match 1 in pop column with p2 in pop column for A to E
    4. if 1 in pop column matches to p2 in pop column and its status is poly only then I would like to give 1 other wise as such, if it is mono I do not want to do anything.
    5. Now I will calculate # 1's and # H's
    6. finally I will calculate %sim with this formula =((#1*2+#H)/((#1+#H)*2))*100.

    I was successfull in achiving first step with this code
    Code:
    #!/usr/bin/perl -w
    use strict;
    open(FILE, "<input.txt") || die "File not found";
    my @lines = <FILE>;
    my @newlines;
    foreach(@lines) {
       $_ =~ s/AA/A/g;
       $_ =~ s/TT/T/g;
       $_ =~ s/GG/G/g;
       $_ =~ s/CC/C/g;
       $_ =~ s/AT/H/g;
       $_ =~ s/AG/H/g;
       $_ =~ s/AC/H/g;
       $_ =~ s/TA/H/g;
       $_ =~ s/TG/H/g;
       $_ =~ s/TC/H/g;
       $_ =~ s/GA/H/g;
       $_ =~ s/GT/H/g;
       $_ =~ s/GC/H/g;
       $_ =~ s/CA/H/g;
       $_ =~ s/CT/H/g;
       $_ =~ s/CG/H/g;
       $_ =~ s/ZZ/-/g;
         
       push(@newlines,$_);
    }
    open(FILE, ">input1.txt") || die "File not found";
    print FILE @newlines;
    close(FILE);
    Now i donot have any idea on how to proceed for next steps can any one help me to solve this problem?
    any help would be highly appreciated. I want to attach my sample data here but i do not know how to attach file here and sorry for that.
    Thanks in Advance
    Regards.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    828
    Rep Power
    496
    It is quite difficult for me to understand what you need.

    For requirement # 2, something like this:

    Somewhere in the loop that reads all the data:

    Perl Code:
     
    my $p1 = (split / /, $_, 2)[1] if /^P1/;
    my $p2 = (split / /, $_, 2)[1] if /^P2/;


    After the end of the loop:

    Perl Code:
    if ($p1 eq $p2) {
         # do something
    } else {
         # do something else
    }


    I do not understand the rest of your questions and don't have time right now to try to figure out.

IMN logo majestic logo threadwatch logo seochat tools logo