Mercurial > repos > iuc > anndata_import
annotate 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 |
rev | line source |
---|---|
3
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
1 #!/usr/bin/env python |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
2 |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
3 """Converts a loompy file to tsv file(s). Each layer becomes a new file.""" |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
4 |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
5 import argparse |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
6 |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
7 import loompy |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
8 |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
9 parser = argparse.ArgumentParser(description="Loompy file converter flags") |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
10 parser.add_argument('--version', action='version', version='%(prog)s 0.1.0', |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
11 help="Displays tool version") |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
12 parser.add_argument("-f", "--file", help="loom file to import") |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
13 args = parser.parse_args() |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
14 |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
15 file = args.file |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
16 |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
17 matrices = [] |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
18 allcols = [] |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
19 colstrings = [] |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
20 allrows = [] |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
21 |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
22 # Build background info for all attributes and layers |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
23 loompyfile = loompy.connect(file) |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
24 row_attributes = loompyfile.ra.keys() # List of row attributes |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
25 for row in row_attributes: # Each list represents rownames for row_attributes |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
26 c_row = loompyfile.ra[row] |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
27 c_row = [str(r) for r in c_row] |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
28 allrows.append(c_row) |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
29 col_attributes = loompyfile.ca.keys() # List of column attributes |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
30 for col in col_attributes: # each list represents colnames for col_attributes |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
31 c_col = loompyfile.ca[col] |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
32 c_col = [str(c) for c in c_col] |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
33 allcols.append(c_col) |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
34 layers = loompyfile.layers.keys() # List of layers |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
35 for layer in layers: # List with each element being a loompy layer |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
36 c_layer = loompyfile[layer] |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
37 c_layer = c_layer[:, :] |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
38 c_layer = c_layer.astype(str) |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
39 matrices.append(c_layer) |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
40 |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
41 # Create column attribute output |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
42 with open("attributes/col_attr.tsv", "w") as colout: |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
43 col_attributes = "\t".join(col_attributes) + "\n" |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
44 colout.write(col_attributes) |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
45 for length in range(0, len(c_col)): |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
46 attributestring = "" |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
47 for col in allcols: |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
48 attributestring = attributestring + col[length] + "\t" |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
49 while attributestring[-1] == "\t": |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
50 attributestring = attributestring[:-1] |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
51 colout.write(attributestring) |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
52 colout.write("\n") |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
53 # Create row attribute output |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
54 with open("attributes/row_attr.tsv", "w") as rowout: |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
55 row_attributes = "\t".join(row_attributes) + "\n" |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
56 rowout.write(row_attributes) |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
57 for length in range(0, len(c_row)): |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
58 attributestring = "" |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
59 for row in allrows: |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
60 attributestring = attributestring + row[length] + "\t" |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
61 while attributestring[-1] == "\t": |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
62 attributestring = attributestring[:-1] |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
63 rowout.write(attributestring) |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
64 rowout.write("\n") |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
65 |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
66 # Build output files for each layer |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
67 for x in range(0, len(layers)): |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
68 # Output file name generation |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
69 if layers[x] in layers[0: x]: # Different output names if layers have same names somehow |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
70 repeats = layers[0, x].count(layer[x]) |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
71 outputname = "output/" + layers[x] + repeats + ".tsv" |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
72 elif layers[x] == "": # Empty layer name |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
73 outputname = "output/mainmatrix.tsv" |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
74 else: |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
75 outputname = "output/" + str(layers[x]) + ".tsv" # Usual case |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
76 # Matrix output |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
77 with open(outputname, "w") as outputmatrix: |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
78 for line in matrices[x]: |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
79 line = "\t".join(line) |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
80 line += "\n" |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
81 line = line |
b5c7ba11401d
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
82 outputmatrix.write(line) |