PageRenderTime 24ms CodeModel.GetById 1ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/new_operations/tables_arithmetic_operations.pl

https://bitbucket.org/cistrome/cistrome-harvard/
Perl | 117 lines | 66 code | 24 blank | 27 comment | 14 complexity | c9eb3fdc97ad8aad33177099cb6f992a MD5 | raw file
  1# A program to implement arithmetic operations on tabular files data. The program takes three inputs:
  2# The first input is a TABULAR format file containing numbers only.
  3# The second input is a TABULAR format file containing numbers only.
  4# The two files must have the same number of columns and the same number of rows
  5# The third input is an arithmetic operation: +, -, *, or / for addition, subtraction, multiplication, or division, respectively 
  6# The output file is a TABULAR format file containing the result of implementing the arithmetic operation on both input files.
  7# The output file has the same number of columns and the same number of rows as each of the two input files.
  8# Note: in case of division, none of the values in the second input file could be 0.
  9
 10use strict;
 11use warnings;
 12
 13#variables to handle information of the first input tabular file
 14my $lineData1 = "";
 15my @lineDataArray1 = ();
 16my $lineArraySize = 0;
 17my $lineCounter1 = 0;
 18
 19#variables to handle information of the second input tabular file
 20my $lineData2= "";
 21my @lineDataArray2 = ();
 22my $lineCounter2 = 0;
 23
 24my $result = 0;
 25
 26# check to make sure having the correct number of arguments
 27my $usage = "usage: tables_arithmetic_operations.pl [TABULAR.in] [TABULAR.in] [ArithmeticOperation] [TABULAR.out] \n";
 28die $usage unless @ARGV == 4;
 29
 30#variables to store the names of input and output files
 31my $inputTabularFile1 = $ARGV[0];
 32my $inputTabularFile2 = $ARGV[1];
 33my $arithmeticOperation = $ARGV[2];
 34my $outputTabularFile = $ARGV[3];
 35
 36#open the input and output files
 37open (INPUT1, "<", $inputTabularFile1) || die("Could not open file $inputTabularFile1 \n"); 
 38open (INPUT2, "<", $inputTabularFile2) || die("Could not open file $inputTabularFile2 \n"); 
 39open (OUTPUT, ">", $outputTabularFile) || die("Could not open file $outputTabularFile \n");
 40
 41#store the first input file in the array @motifsFrequencyData1
 42my @tabularData1 = <INPUT1>;
 43	
 44#store the second input file in the array @motifsFrequencyData2
 45my @tabularData2 = <INPUT2>;
 46
 47#reset the $lineCounter1 to 0	
 48$lineCounter1 = 0;
 49
 50#iterated through the lines of the first input file 
 51INDEL1:
 52foreach $lineData1 (@tabularData1){
 53	chomp ($lineData1);
 54	$lineCounter1++;
 55	
 56	#reset the $lineCounter2 to 0
 57	$lineCounter2 = 0;
 58	
 59	#iterated through the lines of the second input file 
 60	foreach $lineData2 (@tabularData2){
 61		chomp ($lineData2);
 62		$lineCounter2++;
 63
 64		#check if the two motifs are the same in the two input files
 65		if ($lineCounter1 == $lineCounter2){
 66			
 67			@lineDataArray1 = split(/\t/, $lineData1);
 68			@lineDataArray2 = split(/\t/, $lineData2);
 69			
 70			$lineArraySize = @lineDataArray1;
 71			
 72			for (my $index = 0; $index < $lineArraySize; $index++){
 73				
 74				if ($arithmeticOperation eq "Addition"){
 75					#compute the additin of both values
 76					$result = $lineDataArray1[$index] + $lineDataArray2[$index];
 77				}
 78				
 79				if ($arithmeticOperation eq "Subtraction"){
 80					#compute the subtraction of both values
 81					$result = $lineDataArray1[$index] - $lineDataArray2[$index];
 82				}	
 83				
 84				if ($arithmeticOperation eq "Multiplication"){
 85					#compute the multiplication of both values
 86					$result = $lineDataArray1[$index] * $lineDataArray2[$index];
 87				}
 88				
 89				if ($arithmeticOperation eq "Division"){
 90					
 91					#check if the denominator is 0
 92					if ($lineDataArray2[$index] != 0){
 93						#compute the division of both values
 94						$result = $lineDataArray1[$index] / $lineDataArray2[$index];
 95					}
 96					else{
 97						die("A denominator could not be zero \n"); 
 98					}
 99				}
100				
101				#store the result in the output file
102				if ($index < $lineArraySize - 1){
103					print OUTPUT $result . "\t";
104				}
105				else{
106					print OUTPUT $result . "\n";
107				}
108			}
109			next INDEL1;
110		}
111	}
112}	 
113
114#close the input and output files
115close(OUTPUT);
116close(INPUT2);
117close(INPUT1);