Skip to content

Instantly share code, notes, and snippets.

@jasonsahl
Created July 17, 2017 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonsahl/ac1b2b64daecb83925e562b454a1a301 to your computer and use it in GitHub Desktop.
Save jasonsahl/ac1b2b64daecb83925e562b454a1a301 to your computer and use it in GitHub Desktop.
Transfrom content of Kallisto matrix, used for in silico ribotyping of C. difficile in this case
#!/usr/bin/env python
"""Transform ribotype data. Input matrix
is a transposed output from bac_seq"""
from __future__ import print_function
from __future__ import division
import sys
import os
import optparse
def test_file(option, opt_str, value, parser):
try:
with open(value): setattr(parser.values, option.dest, value)
except IOError:
print('%s file cannot be opened' % option)
sys.exit()
def transpose_matrix(matrix):
out_matrix = open("transposed_matrix.matrix", "w")
reduced = [ ]
for line in open(matrix, "rU"):
newline=line.strip("\n")
fields = newline.split("\t")
reduced.append(fields)
test=map(list, zip(*reduced))
for x in test:
out_matrix.write("\t".join(x)+"\n")
out_matrix.close()
def main(matrix, min_percent):
transpose_matrix(matrix)
print("Genome hits:")
for line in open("transposed_matrix.matrix", "rU"):
newline = line.strip()
fields = newline.split()
if fields[0] == "target":
pass
else:
values = []
to_write = []
for x in fields[1:]:
values.append(int(x))
max_value = max(values)
for value in values:
try:
if float(value)/max_value > float(min_percent):
to_write.append("1")
else:
to_write.append("0")
except:
to_write.append("0")
if "0" in to_write[1:]:
pass
else:
print(fields[0])
os.system("rm transposed_matrix.matrix")
if __name__ == "__main__":
usage="usage: %prog [options]"
parser = optparse.OptionParser(usage=usage)
parser.add_option("-m", "--matrix", dest="matrix",
help="/path/to/kallisto_merged_matrix [REQUIRED]",
type="string", action="callback", callback=test_file)
parser.add_option("-p", "--min_percent", dest="min_percent",
help="minimum percent of maximum to be called present, defaults to 0.2",
type="float", action="store", default="0.2")
options, args = parser.parse_args()
mandatories = ["matrix"]
for m in mandatories:
if not getattr(options, m, None):
print("\nMust provide %s.\n" %m)
parser.print_help()
exit(-1)
main(options.matrix,options.min_percent)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment