annotate tables_arithmetic_operations.pl @ 1:2e9f3b6ebfbc draft default tip

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