Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
TOuCAN/bin/aggregate_matrix.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
104 lines (78 sloc)
2.51 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python | |
import sys | |
import getopt | |
def matrix_iterator(instream=sys.stdin): | |
"""Iterate over the rows of the matrix.""" | |
for line in instream: | |
# remove the endline | |
line = line.rstrip() | |
# skip empty line | |
if len(line) == 0: | |
continue | |
# return the fields | |
yield line.split("\t") | |
def main(instream=sys.stdin, outstream=sys.stdout): | |
"""The main program loop.""" | |
base = None | |
counts = [] | |
# foreach entry in the array | |
for columns in matrix_iterator(instream): | |
# first entry | |
if base is None: | |
base = columns[:7] | |
# if we are changing fragment | |
if base[:7] != columns[:7]: | |
outstream.write("%s\t%d\n" % ( | |
"\t".join(base), sum(counts))) | |
base = columns[:7] | |
counts = [] | |
# add the score | |
try: | |
counts.append(int(columns[7])) | |
except ValueError: | |
counts.append(0) | |
# write the output entry | |
outstream.write("%s\t%d\n" % ("\t".join(base), sum(counts))) | |
if __name__ == "__main__": | |
def usage(message="", error=None): | |
"""Print the usage information.""" | |
sys.stdout.write(""" | |
Message %(message)s | |
Called as %(commandline)s | |
Usage: | |
python %(tool)s -i [input file] -o [output file] | |
Options: | |
-i/--input [file] the matrix file, default stdin | |
-o/--output [file] the output matrix file, default stdout | |
-h/--help [] print this message | |
""" % { | |
"message": message, | |
"tool": sys.argv[0], | |
"commandline": " ".join(sys.argv) | |
}) | |
if error is not None: | |
sys.exit(error) | |
# end of usage | |
# main variables | |
fstream_input = sys.stdin | |
fstream_output = sys.stdout | |
# option parsing | |
shortopt = "i:o:h" | |
longopt = ["input=", "output=", "help"] | |
try: | |
opts, args = getopt.getopt(sys.argv[1:], shortopt, longopt) | |
for o, a in opts: | |
if o in ("-i", "--input"): | |
fstream_input = open(a, "r") | |
elif o in ("-o", "--output"): | |
fstream_output = open(a, "w") | |
elif o in ('-h', '--help'): | |
usage("Help was asked", error=0) | |
except getopt.GetoptError as err: | |
usage(str(err), error=2) | |
# run the program loop | |
main(fstream_input, fstream_output) | |
if not fstream_input.closed and fstream_input is not sys.stdin: | |
fstream_input.close() | |
if not fstream_output.closed and fstream_output is not sys.stdout: | |
fstream_output.close() |