CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
decoding

 



inidnan
New User

Aug 22, 2014, 7:02 AM

Post #1 of 5 (1694 views)
decoding Can't Post

Hello,
I have a huge file with over 700,00 SNPs with 18 columns. One column is in the format


Code
--+-+ 
---++
?????
-????


Now i have another list which corresponds to this code in a particular order

Code
A-1 
B-7
C-11
D-3
E-100

Now I need to match the expression above to the pattern, where +/- corresponds to A-E and ? corresponds to 0 and as an output I need to get a number.
So the output should be

Code
--+-+ = 122 (1+7+11+3+100) 
---++= 122
?????= 0
-????= 1


This is just a sample, the numbers are much bigger and the pattern runs upto 24 times. Is there any way I can do this with perl ?

Thank you.


FishMonger
Veteran / Moderator

Aug 22, 2014, 7:19 AM

Post #2 of 5 (1689 views)
Re: [inidnan] decoding [In reply to] Can't Post

Yes, this is very easy to do in Perl.

What have you tried?


inidnan
New User

Aug 22, 2014, 7:35 AM

Post #3 of 5 (1683 views)
Re: [FishMonger] decoding [In reply to] Can't Post

I tried a bit of bash scripting, didnt work, and then with perl I tried to make an array storing all the values A to E and then tried to split the coded strings.... its not working for me either...

Code
#!/usr/local/bin/perl 

my $input = 'data.txt'; #file with coded strings
my @values = (1, 7, 11, 3, 100)
my @terms = split //, $input;
my $sum = 0;
for my $i (0 .. @terms-1) {
$sum += $values[$i] if $terms[$i] ne '?';
}
print "$input = $sum\n";



(This post was edited by inidnan on Aug 22, 2014, 7:40 AM)


FishMonger
Veteran / Moderator

Aug 22, 2014, 7:39 AM

Post #4 of 5 (1678 views)
Re: [inidnan] decoding [In reply to] Can't Post

I assume you mean that you tried using the solution that Kenneth gave you on perlmonks, but couldn't get it to work correctly. That's probably because your description of your data is incomplete and saying "its not working for me" is a very poor problem description.

Please post your test script and a few lines of the actual data and the results you expect to get from those lines.


FishMonger
Veteran / Moderator

Aug 22, 2014, 7:46 AM

Post #5 of 5 (1672 views)
Re: [inidnan] decoding [In reply to] Can't Post

You need to open the 'data.txt' file and loop over its contents.


Code
#!/usr/bin/perl 

use strict;
use warnings;

my $input = 'data.txt'; #file with coded strings
my @values = (1, 7, 11, 3, 100);

open my $fh, '<', $input or die "failed to open '$input' $!\n";

while (<$fh>) {
chomp;
my @terms = split //;
my $sum = 0;
for my $i (0 .. @terms-1) {
$sum += $values[$i] if $terms[$i] ne '?';
}
print "$_ = $sum\n";
}


output from c:\test>test.pl

Code
--+-+ = 122 
---++ = 122
????? = 0
-???? = 1



(This post was edited by FishMonger on Aug 22, 2014, 7:55 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives