# HG changeset patch # User devteam # Date 1396357913 14400 # Node ID 6ff325b363829cbe5bb28beac9b5d95a6b02d889 Imported from capsule None diff -r 000000000000 -r 6ff325b36382 tables_arithmetic_operations.pl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tables_arithmetic_operations.pl Tue Apr 01 09:11:53 2014 -0400 @@ -0,0 +1,117 @@ +# A program to implement arithmetic operations on tabular files data. The program takes three inputs: +# The first input is a TABULAR format file containing numbers only. +# The second input is a TABULAR format file containing numbers only. +# The two files must have the same number of columns and the same number of rows +# The third input is an arithmetic operation: +, -, *, or / for addition, subtraction, multiplication, or division, respectively +# The output file is a TABULAR format file containing the result of implementing the arithmetic operation on both input files. +# The output file has the same number of columns and the same number of rows as each of the two input files. +# Note: in case of division, none of the values in the second input file could be 0. + +use strict; +use warnings; + +#variables to handle information of the first input tabular file +my $lineData1 = ""; +my @lineDataArray1 = (); +my $lineArraySize = 0; +my $lineCounter1 = 0; + +#variables to handle information of the second input tabular file +my $lineData2= ""; +my @lineDataArray2 = (); +my $lineCounter2 = 0; + +my $result = 0; + +# check to make sure having the correct number of arguments +my $usage = "usage: tables_arithmetic_operations.pl [TABULAR.in] [TABULAR.in] [ArithmeticOperation] [TABULAR.out] \n"; +die $usage unless @ARGV == 4; + +#variables to store the names of input and output files +my $inputTabularFile1 = $ARGV[0]; +my $inputTabularFile2 = $ARGV[1]; +my $arithmeticOperation = $ARGV[2]; +my $outputTabularFile = $ARGV[3]; + +#open the input and output files +open (INPUT1, "<", $inputTabularFile1) || die("Could not open file $inputTabularFile1 \n"); +open (INPUT2, "<", $inputTabularFile2) || die("Could not open file $inputTabularFile2 \n"); +open (OUTPUT, ">", $outputTabularFile) || die("Could not open file $outputTabularFile \n"); + +#store the first input file in the array @motifsFrequencyData1 +my @tabularData1 = ; + +#store the second input file in the array @motifsFrequencyData2 +my @tabularData2 = ; + +#reset the $lineCounter1 to 0 +$lineCounter1 = 0; + +#iterated through the lines of the first input file +INDEL1: +foreach $lineData1 (@tabularData1){ + chomp ($lineData1); + $lineCounter1++; + + #reset the $lineCounter2 to 0 + $lineCounter2 = 0; + + #iterated through the lines of the second input file + foreach $lineData2 (@tabularData2){ + chomp ($lineData2); + $lineCounter2++; + + #check if the two motifs are the same in the two input files + if ($lineCounter1 == $lineCounter2){ + + @lineDataArray1 = split(/\t/, $lineData1); + @lineDataArray2 = split(/\t/, $lineData2); + + $lineArraySize = @lineDataArray1; + + for (my $index = 0; $index < $lineArraySize; $index++){ + + if ($arithmeticOperation eq "Addition"){ + #compute the additin of both values + $result = $lineDataArray1[$index] + $lineDataArray2[$index]; + } + + if ($arithmeticOperation eq "Subtraction"){ + #compute the subtraction of both values + $result = $lineDataArray1[$index] - $lineDataArray2[$index]; + } + + if ($arithmeticOperation eq "Multiplication"){ + #compute the multiplication of both values + $result = $lineDataArray1[$index] * $lineDataArray2[$index]; + } + + if ($arithmeticOperation eq "Division"){ + + #check if the denominator is 0 + if ($lineDataArray2[$index] != 0){ + #compute the division of both values + $result = $lineDataArray1[$index] / $lineDataArray2[$index]; + } + else{ + die("A denominator could not be zero \n"); + } + } + + #store the result in the output file + if ($index < $lineArraySize - 1){ + print OUTPUT $result . "\t"; + } + else{ + print OUTPUT $result . "\n"; + } + } + next INDEL1; + } + } +} + +#close the input and output files +close(OUTPUT); +close(INPUT2); +close(INPUT1); \ No newline at end of file diff -r 000000000000 -r 6ff325b36382 tables_arithmetic_operations.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tables_arithmetic_operations.xml Tue Apr 01 09:11:53 2014 -0400 @@ -0,0 +1,105 @@ + + on tables + + + tables_arithmetic_operations.pl $inputFile1 $inputFile2 $inputArithmeticOperation3 $outputFile1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +.. class:: infomark + +**What it does** + +This program implements arithmetic operations on tabular files data. The program takes three inputs: + +- The first input is a TABULAR format file containing numbers only. +- The second input is a TABULAR format file containing numbers only. +- The third input is an arithmetic operation: +, -, x, or / for addition, subtraction, multiplication, or division, respectively. +- The output file is a TABULAR format file containing the result of implementing the arithmetic operation on both input files. + + +Notes: + +- The two files must have the same number of columns and the same number of rows. +- The output file has the same number of columns and the same number of rows as each of the two input files. +- In case of division, none of the values in the second input file could be 0, otherwise the program will stop and report an error. + +**Example** + +Let us have the first input file as follows:: + + 5 4 0 + 10 11 12 + 1 3 1 + 1 2 1 + 2 0 4 + +And the second input file as follows:: + + 5 4 4 + 2 5 8 + 1 2 1 + 3 2 5 + 2 4 4 + +Running the program and choosing "Addition" as an arithmetic operation will give the following output:: + + 10 8 4 + 12 16 20 + 2 5 2 + 4 4 6 + 4 4 8 + + + + + diff -r 000000000000 -r 6ff325b36382 test-data/numericalTable1.tabular --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/numericalTable1.tabular Tue Apr 01 09:11:53 2014 -0400 @@ -0,0 +1,8 @@ +5 4 4 5 4 5 6 6 +10 11 12 14 15 14 19 23 +1 1 1 1 1 1 1 1 +1 2 1 2 2 1 4 3 +2 4 4 5 4 4 5 5 +3 1 2 1 1 2 3 3 +6 7 5 1 1 2 2 2 +4 4 4 5 8 7 6 6 diff -r 000000000000 -r 6ff325b36382 test-data/table_addition_result.tabular --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/table_addition_result.tabular Tue Apr 01 09:11:53 2014 -0400 @@ -0,0 +1,8 @@ +10 8 8 10 8 10 12 12 +20 22 24 28 30 28 38 46 +2 2 2 2 2 2 2 2 +2 4 2 4 4 2 8 6 +4 8 8 10 8 8 10 10 +6 2 4 2 2 4 6 6 +12 14 10 2 2 4 4 4 +8 8 8 10 16 14 12 12 diff -r 000000000000 -r 6ff325b36382 test-data/table_division_result.tabular --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/table_division_result.tabular Tue Apr 01 09:11:53 2014 -0400 @@ -0,0 +1,8 @@ +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 diff -r 000000000000 -r 6ff325b36382 test-data/table_multiplication_result.tabular --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/table_multiplication_result.tabular Tue Apr 01 09:11:53 2014 -0400 @@ -0,0 +1,8 @@ +25 16 16 25 16 25 36 36 +100 121 144 196 225 196 361 529 +1 1 1 1 1 1 1 1 +1 4 1 4 4 1 16 9 +4 16 16 25 16 16 25 25 +9 1 4 1 1 4 9 9 +36 49 25 1 1 4 4 4 +16 16 16 25 64 49 36 36 diff -r 000000000000 -r 6ff325b36382 test-data/table_subtraction_result.tabular --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/table_subtraction_result.tabular Tue Apr 01 09:11:53 2014 -0400 @@ -0,0 +1,8 @@ +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0