comparison modify_loom.py @ 3:8623710d083c draft

"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
author iuc
date Mon, 06 Jan 2020 13:44:46 -0500
parents
children
comparison
equal deleted inserted replaced
2:e0395cca2c57 3:8623710d083c
1 #!/usr/bin/env python
2 """This program adds layers, row attributes or column attributes for loom files"""
3
4 import argparse
5
6 import loompy
7 import numpy as np
8
9 parser = argparse.ArgumentParser(description="Loompy file converter flags")
10 parser.add_argument('--VERSION', action='version', version='%(prog)s 0.1.0',
11 help="Displays tool version")
12 parser.add_argument('--file', '-f',
13 help="Loom file to which data will be added")
14 parser.add_argument('--rowfile', '-r', help="File of row attributes & values")
15 parser.add_argument('--colfile', '-c',
16 help="File of column attributes and values")
17 parser.add_argument('--layers', '-l', nargs='*',
18 help="Input tsv files. First file becomes main layer.")
19 parser.add_argument('--add', '-a', choices=["rows", "cols", "layers"],
20 help="Selects rows, columns or layers to be added to file")
21 args = parser.parse_args()
22
23 lfile = args.file
24 if args.rowfile:
25 rowfile = args.rowfile
26 if args.colfile:
27 colfile = args.colfile
28 if args.layers:
29 alllayers = args.layers
30 addselect = args.add
31 # Check proper flags for chosen attributes are being added
32 if addselect == "cols" and not args.colfile:
33 raise Exception("To add column attributes, column flag and file must be provided")
34 if addselect == "rows" and not args.rowfile:
35 raise Exception("To add row attributes, row flag and file must be provided")
36 if addselect == "layers" and not args.layers:
37 raise Exception("To add layers, a layer flag and file(s) must be provided")
38
39 layernames = []
40 rowdict = {}
41 coldict = {}
42
43 with loompy.connect(lfile) as loomfile:
44 # Loom file dimensions
45 nrow = loomfile.shape[0]
46 ncol = loomfile.shape[1]
47 if addselect == "layers":
48 layernames = []
49 # Generate layer names based on file names
50 for x in range(0, len(alllayers)):
51 layer = alllayers[x]
52 layer = layer.split("/")[-1].split(".")[-2] # Takes away path, takes off extension
53 layernames.append(layer)
54 # Add in the layers themselves
55 for layer in range(0, len(alllayers)):
56 matrix = ""
57 with open(alllayers[layer], "r") as infile:
58 rows = 0
59 count = 0
60 for line in infile:
61 if count == 0:
62 cols = len(line.split("\t"))
63 if cols != ncol:
64 raise Exception("Dimensions of new matrix incorrect for this loom file. New matrices must be %d by %d" % (nrow, ncol))
65 matrix = matrix + line + "\t"
66 rows += 1
67 if rows != nrow:
68 raise Exception("Dimensions of new matrix incorrect for this loom file. New matrices must be %d by %d")
69 matrix = matrix.split("\t")
70 matrix = [float(n) for n in matrix[:-1]]
71 matrix = np.asarray(matrix).reshape(nrow, ncol)
72 loomfile[layernames[layer]] = matrix
73 elif addselect == "rows":
74 with open(rowfile, "r") as rows:
75 count = 0
76 for line in rows:
77 line = line.strip().split("\t")
78 if count == 0: # First time through
79 row_attributes = line
80 for x in row_attributes:
81 rowdict[x] = []
82 count += 1
83 else:
84 for x in range(0, len(line)):
85 rowdict[row_attributes[x]].append(line[x])
86 for x in row_attributes:
87 if len(rowdict[x]) != nrow:
88 raise Exception("Incorrect length of row. Row length must be: %d" % nrow)
89 loomfile.ra[x] = rowdict[x]
90 elif addselect == "cols":
91 with open(colfile, "r") as cols:
92 count = 0
93 for line in cols:
94 line = line.replace('\"', "")
95 line = line.replace(' ', "")
96 line = line.strip().split("\t")
97 if count == 0: # First time through
98 col_attributes = line
99 for x in col_attributes:
100 coldict[x] = []
101 count += 1
102 else:
103 for x in range(0, len(line)):
104 coldict[col_attributes[x]].append(line[x])
105 for y in col_attributes:
106 if len(coldict[y]) != ncol:
107 raise Exception("Incorrect length of column. Column length must be: %d" % ncol)
108 loomfile.ca[y] = coldict[y]