using perl to calculate frequency and multiply


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using perl to calculate frequency and multiply
# 1  
Old 04-02-2009
using perl to calculate frequency and multiply

Suppose u have two input files FILE A and FILE B

FILE A


AACD
ABBD
ACBC


FILE B

s/ A B C D E
A 1 -2 3 4 2
B 3 2 -1 2 1
C 2 3 1 2 3
D 3 4 -3 2 2
E 1 3 4 2 3

So in FILE A we have calculated frequency at 1st position i.e.
A will be 1
then frequency at 2nd position A is 0.3
B is 0.3 ....C is 0.3
again frequency at 3rd position for C is 0.62
for B is 0.3 ...so on.......


Then at first position is frequency of A multiplied with (second FILE B)
A B C D E
1*1 1*-2 1*3 1*4 1*2


Similarily at 2nd position is frequency of A (0.3) multiplied with second file (A*A) + frequency of B(0.3) multiplied with second file (B*A) + frequency of C(0.3)multiplied with second file (C*A) then again frequency of A (0.3) multiplied with second file (A*B) + frequency of B(0.3) multiplied with second file (B*B) + frequency of C(0.3)multiplied with second file (C*B)...and similarily calculated upto C, D,E

that is
A B C D E
2nd position 0.3*1+ 0.3 *-2 ...
..... 0.3*0.3+ + 0.3*2
..... 0.3*0.2 + 0.3*3

Therefore final output will be like
pos A B C D E
Ist 1 -2 3 4 2
2nd 1.8 0.9 ............
3rd .......so on...
# 2  
Old 04-15-2009
Stuff like this makes our collective heads hurt. What perl code have you written so far? You should start out with something like:
Code:
open(FILEA, $ARGV[0]) || die "File A: $!"
open(FILEB, $ARGV[1]) || die "File B: $!"

while (<FILEA>) { 
   $rows_in_a[$.] = [ split('') ];
}
# $rows_in_a is now a 2D array of the elements in FileA

while (<FILEB>) { 
   $rows_in_b[$.] = [ split ]; 
}
# $rows_in_b is now a 2D array of the elements in FileB.

Thing is, in FileB, the first row and first column are labels... do they correspond to the elements in FileA? Are they actually these letter or are they some other lablel? It's not clear. But at least at this point, you can do:
Code:
$freq_a[ $i ] = $rows_in_a[ $i ]->[ $j ] * $rows_in_b[ $i+1 ]-> [ $j+1 ];

But I have no idea what you really want.
# 3  
Old 04-15-2009
resimplify

Actually real input is File A

AACD
ABBD
ACBC

where first column is all A so frequency will be 1
second column is A B C so frequency for A B C will be 0.3 0.3 0.3
third column is frequency of C = 0.3 and B =0.6....so on



In the first column will be

frequency of A * [A] [A] = 1
frequency of A * [A] [B] = -2
frequency of A * [A] [C] = 3
frequency of A * [A] [D] =4
frequency of A * [A] [E] =2

Now as in the second column there is A,B ,C
frequency of A * [A] [A] + frequency of B * [B] [A] + frequency of C* [C] [A]
frequency of A * [A] [B] + frequency of B * [B] [B] + frequency of C* [C] [B]
frequency of A * [A] [C] + frequency of B * [B] [C] + frequency of C* [C] [C]
frequency of A * [A] [D] + frequency of B * [B] [D] + frequency of C* [C] [D]
frequency of A * [A] [E] + frequency of B * [B] [E] + frequency of C* [C] [E]

in the third column
frequency of B * [B] [A] + frequency of C* [C] [A]
frequency of B * [B] [B] + frequency of C* [C] [B]
frequency of B * [B] [C] + frequency of C* [C] [C]
frequency of B * [B] [D] + frequency of C* [C] [D]
frequency of B * [B] [E] + frequency of C* [C] [E]


For calculations of [A][A] , [A][B] , [B][B],[C][C]........

We have file B
s/ A B C D E
A 1 -2 3 4 2
B 3 2 -1 2 1
C 2 3 1 2 3
D 3 4 -3 2 2
E 1 3 4 2 3

where
[A][A]=1
[A][B]=-2 ..
[C] [A] =2
so on


so final output will be like

column A B C D E
1st 1 -2 3 4 2

2nd 1.8 0.9 .. so on

Last edited by cdfd123; 04-15-2009 at 04:00 PM.. Reason: in the third column
# 4  
Old 04-16-2009
There's no real need to post the clarification of the assignment at this point -- you now have the basic code idea in my first post. The rest is up to you. Good luck.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl script to calculate failure ratio

Hello geeks, Find below a Perl script am writing to calculate some failure rate in our GPRS network, am just starting to use perl for scripting. #!/usr/bin/perl #Script written to calculate the following: #PDP activation failure rate for every 15 minutes interval #Number of Active PDP... (1 Reply)
Discussion started by: infinitydon
1 Replies

2. Shell Programming and Scripting

Calculate time difference between pst and pdt dates in perl

Hi, how to calculate the time difference between PST date and PDT date in perl scripting. date1: Mon Dec 31 16:00:01 PST 2015 date2: Tue Mar 19 06:09:30 PDT 2013 and also difference between PST-PST and PDT-PDT need difference in months or days (months prefereble). (3 Replies)
Discussion started by: praveen265
3 Replies

3. Shell Programming and Scripting

Letter Frequency Decryption Program in Perl

Hello, :/ (0 Replies)
Discussion started by: jvr42
0 Replies

4. Shell Programming and Scripting

Calculate average from CSV file using PERL script

Hi All I have this csv file and I need to calculate the average of FPS. FPS:27.7420, Interval:1314184238772 FPS:25.9798, Interval:1314184242646 FPS:27.4772, Interval:1314184246311 FPS:26.1623, Interval:1314184250159 FPS:26.4515, Interval:1314184253972 FPS:31.5896, Interval:1314184257163... (24 Replies)
Discussion started by: sayachop
24 Replies

5. Shell Programming and Scripting

Perl- Finding average "frequency" of occurrence of duplicate lines

Hello, I am working with a perl script that tries to find the average "frequency" in which lines are duplicated. So far I've only managed to find the way to count how many times the lines are repeated, the code is as follows: perl -ae' my $filename= $ENV{'i'}; open (FILE, "$filename") or... (10 Replies)
Discussion started by: acsg
10 Replies

6. Programming

perl - calculate the number of lines from particular files

Hi, plz see the below code. here my aim is to calculate the number of lines in unprocessedData.out if this file contains 40 lines then lastly $linenum should print 40.(except blank lines) i have tried below code but it giving me the output only one. can anyone help me how to do ? ... (9 Replies)
Discussion started by: pspriyanka
9 Replies

7. Shell Programming and Scripting

Calculate age of a file | calculate time difference

Hello, I'm trying to create a shell script (#!/bin/sh) which should tell me the age of a file in minutes... I have a process, which delivers me all 15 minutes a new file and I want to have a monitoring script, which sends me an email, if the present file is older than 20 minutes. To do... (10 Replies)
Discussion started by: worm
10 Replies

8. UNIX for Dummies Questions & Answers

How to calculate frequency distributions?

Hello, I'm trying to get lists of the frequency distributions for each of two variables (vars C and N in the examples). I'd like the distribution for each variable to range from the min of the two variables to the max of the two variables. I can work out the max value beforehand by ordering the... (2 Replies)
Discussion started by: auburn
2 Replies

9. Shell Programming and Scripting

All I want to do is Multiply...

All I want to do is find 5!. read num num={1..5} while do f= done echo f Error Msg. line 5: ${1..5} bad substitution line 6: integer expression expected Line 5 is the num=... Line 6 is the "while" statement I am new at this, and I am really, really trying. Please... (14 Replies)
Discussion started by: Ccccc
14 Replies

10. Shell Programming and Scripting

How can use the perl or other command line to calculate the length of the character?

For example, if I have the file whose content are: >HWI-EAS382_30FC7AAXX:7:1:927:1368 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA >HWI-EAS382_30FC7AAXX:7:1:924:1373 ACGAACTTTAAAGCACCTCTTGGCTCGTATGCCGTC I want my output calculate the total length of nucleotide. So my output should look like this:... (1 Reply)
Discussion started by: patrick chia
1 Replies
Login or Register to Ask a Question