|
0
|
1 #!/usr/bin/env python
|
|
|
2
|
|
|
3 import sys
|
|
|
4 import argparse
|
|
|
5
|
|
|
6 def main():
|
|
|
7
|
|
|
8 VERSION = 0.1
|
|
|
9
|
|
|
10 parser = argparse.ArgumentParser(description="Calculates the means of rows of a numeric tab delimited table.")
|
|
|
11 parser.add_argument("-i", "--infile", help="Input tab delimted numeric file")
|
|
|
12 parser.add_argument("-o", "--outfile", help="Output list of means")
|
|
|
13 parser.add_argument("-s", "--skip", default=0, help="Number of header rows to skip", type=int)
|
|
|
14 parser.add_argument("--version", action='store_true')
|
|
|
15
|
|
|
16 args = parser.parse_args()
|
|
|
17
|
|
|
18 if args.version:
|
|
|
19 print("means_by_row.py version: %.1f" % VERSION)
|
|
|
20 return
|
|
|
21
|
|
|
22 infile = str(args.infile)
|
|
|
23 outfile = str(args.outfile)
|
|
|
24 skip = int(args.skip)
|
|
|
25
|
|
|
26 lines = open( infile, 'r').readlines()
|
|
|
27
|
|
|
28 averages = []
|
|
|
29 linenum = 0
|
|
|
30
|
|
|
31 for line in lines:
|
|
|
32 if int(skip) > 0:
|
|
|
33 skip -= 1
|
|
|
34 continue
|
|
|
35 line.rstrip()
|
|
|
36 sum = 0.0
|
|
|
37 count = 0.0
|
|
|
38 units = line.split('\t')
|
|
|
39 for unit in units:
|
|
|
40 try:
|
|
|
41 unit = float(unit)
|
|
|
42 sum += unit
|
|
|
43 count += 1
|
|
|
44 except:
|
|
|
45 print("WARNING: Non numeric found at line %f.1" % linenum)
|
|
|
46 continue
|
|
|
47 avg = 0
|
|
|
48 if count > 0:
|
|
|
49 avg = sum/count
|
|
|
50 averages.append(avg)
|
|
|
51 linenum += 1
|
|
|
52
|
|
|
53 fout = open(outfile, 'w')
|
|
|
54 fout.write("Sample_Average\n")
|
|
|
55
|
|
|
56 for avg in averages:
|
|
|
57 fout.write(str('%.3f' % avg)+ '\n')
|
|
|
58
|
|
|
59 return
|
|
|
60
|
|
|
61 if __name__ == "__main__": main()
|