comparison loompy_to_tsv.py @ 3:b5c7ba11401d 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:45:13 -0500
parents
children fb9e030ffae4
comparison
equal deleted inserted replaced
2:e175d4067b00 3:b5c7ba11401d
1 #!/usr/bin/env python
2
3 """Converts a loompy file to tsv file(s). Each layer becomes a new file."""
4
5 import argparse
6
7 import loompy
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("-f", "--file", help="loom file to import")
13 args = parser.parse_args()
14
15 file = args.file
16
17 matrices = []
18 allcols = []
19 colstrings = []
20 allrows = []
21
22 # Build background info for all attributes and layers
23 loompyfile = loompy.connect(file)
24 row_attributes = loompyfile.ra.keys() # List of row attributes
25 for row in row_attributes: # Each list represents rownames for row_attributes
26 c_row = loompyfile.ra[row]
27 c_row = [str(r) for r in c_row]
28 allrows.append(c_row)
29 col_attributes = loompyfile.ca.keys() # List of column attributes
30 for col in col_attributes: # each list represents colnames for col_attributes
31 c_col = loompyfile.ca[col]
32 c_col = [str(c) for c in c_col]
33 allcols.append(c_col)
34 layers = loompyfile.layers.keys() # List of layers
35 for layer in layers: # List with each element being a loompy layer
36 c_layer = loompyfile[layer]
37 c_layer = c_layer[:, :]
38 c_layer = c_layer.astype(str)
39 matrices.append(c_layer)
40
41 # Create column attribute output
42 with open("attributes/col_attr.tsv", "w") as colout:
43 col_attributes = "\t".join(col_attributes) + "\n"
44 colout.write(col_attributes)
45 for length in range(0, len(c_col)):
46 attributestring = ""
47 for col in allcols:
48 attributestring = attributestring + col[length] + "\t"
49 while attributestring[-1] == "\t":
50 attributestring = attributestring[:-1]
51 colout.write(attributestring)
52 colout.write("\n")
53 # Create row attribute output
54 with open("attributes/row_attr.tsv", "w") as rowout:
55 row_attributes = "\t".join(row_attributes) + "\n"
56 rowout.write(row_attributes)
57 for length in range(0, len(c_row)):
58 attributestring = ""
59 for row in allrows:
60 attributestring = attributestring + row[length] + "\t"
61 while attributestring[-1] == "\t":
62 attributestring = attributestring[:-1]
63 rowout.write(attributestring)
64 rowout.write("\n")
65
66 # Build output files for each layer
67 for x in range(0, len(layers)):
68 # Output file name generation
69 if layers[x] in layers[0: x]: # Different output names if layers have same names somehow
70 repeats = layers[0, x].count(layer[x])
71 outputname = "output/" + layers[x] + repeats + ".tsv"
72 elif layers[x] == "": # Empty layer name
73 outputname = "output/mainmatrix.tsv"
74 else:
75 outputname = "output/" + str(layers[x]) + ".tsv" # Usual case
76 # Matrix output
77 with open(outputname, "w") as outputmatrix:
78 for line in matrices[x]:
79 line = "\t".join(line)
80 line += "\n"
81 line = line
82 outputmatrix.write(line)