Mercurial > repos > iuc > modify_loom
annotate tsv_to_loompy.py @ 9:0f1aaff9b22d draft default tip
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit 67b3808b56df343798263ff0c905df8cb789edfa
author | iuc |
---|---|
date | Sat, 14 Sep 2024 19:59:12 +0000 |
parents | c8e4d0b9ae8c |
children |
rev | line source |
---|---|
0
c8e4d0b9ae8c
"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 |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
2 """This module converts a tsv file into a binary loom file""" |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
3 |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
4 import argparse |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
5 import os |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
6 |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
7 import loompy |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
8 import numpy as np |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
9 |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
10 parser = argparse.ArgumentParser(description="Loompy file converter flags") |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
11 parser.add_argument('--VERSION', action='version', version='%(prog)s 0.1.0', |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
12 help="Displays tool version") |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
13 parser.add_argument('--rowfile', '-r', help="File of row attributes & values") |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
14 parser.add_argument('--colfile', '-c', |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
15 help="File of column attributes and values") |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
16 parser.add_argument('--output', '-o', help="Output file name") |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
17 parser.add_argument('--files', '-f', nargs='*', |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
18 help="Input tsv files. First file becomes main layer.") |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
19 args = parser.parse_args() |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
20 |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
21 colsfile = args.colfile |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
22 rowsfile = args.rowfile |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
23 if args.output: |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
24 filename = args.output |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
25 else: |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
26 filename = "converted.loom" |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
27 alldata = args.files |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
28 alayers = [] |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
29 layernames = [] |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
30 rowdict = {} |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
31 coldict = {} |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
32 |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
33 # Creates dictionary based on row file |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
34 # For each attribute: |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
35 # Attribute: [attribute values] |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
36 with open(rowsfile, "r") as rows: |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
37 count = 0 |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
38 for line in rows: |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
39 line = line.strip().split("\t") |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
40 if count == 0: # First time through |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
41 row_attributes = line |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
42 for x in row_attributes: |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
43 rowdict[x] = [] |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
44 count += 1 |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
45 else: |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
46 for x in range(0, len(line)): |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
47 rowdict[row_attributes[x]].append(line[x]) |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
48 # Same as above, but for columns |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
49 with open(colsfile, "r") as cols: |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
50 count = 0 |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
51 for line in cols: |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
52 line = line.replace('\"', "") |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
53 line = line.replace(' ', "") |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
54 line = line.strip().split("\t") |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
55 if count == 0: # First time through |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
56 col_attributes = line |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
57 for x in col_attributes: |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
58 coldict[x] = [] |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
59 count += 1 |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
60 else: |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
61 for x in range(0, len(line)): |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
62 coldict[col_attributes[x]].append(line[x]) |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
63 # Finding dimensions for the loom layers |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
64 rowshape = len(rowdict[list(rowdict.keys())[0]]) |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
65 colshape = len(coldict[list(coldict.keys())[0]]) |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
66 |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
67 # Creates a list with each element being entire matrix of |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
68 # each layer file as floats |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
69 for file in range(0, len(alldata)): |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
70 layer = alldata[file][:-4] |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
71 layer = layer.split("/")[-1] |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
72 if layer == "": |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
73 raise Exception("Please only use named files") |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
74 layernames.append(layer) |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
75 cfile = alldata[file] |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
76 with open(cfile, "r") as tsv: |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
77 cmatrix = [] |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
78 for line in tsv: |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
79 line = line.strip().split("\t") |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
80 line = [float(i) for i in line] |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
81 cmatrix += line |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
82 alayers.append(cmatrix) |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
83 |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
84 # Loompy cannot overwright existing files. If somehow it finds |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
85 # a second file with the same name, it must be deleted |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
86 if os.path.isfile(filename): |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
87 os.remove(filename) |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
88 # To create the file properly, the first row and column attributes must be |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
89 # added separately in the form of individual dictionaries |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
90 row_attrs = {row_attributes[0]: np.asarray(rowdict[row_attributes[0]])} |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
91 col_attrs = {col_attributes[0]: np.asarray(coldict[col_attributes[0]])} |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
92 matrix = np.asarray(alayers[0]) |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
93 matrix = matrix.astype(float) |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
94 matrix = matrix.reshape(rowshape, colshape) |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
95 # Creation of initial loom file |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
96 if "loom" not in filename[-5:]: |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
97 filename = filename + ".loom" |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
98 loompy.create(filename, matrix, row_attrs, col_attrs) |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
99 # Adding all row and column attributes, then all layers |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
100 with loompy.connect(filename) as loomfile: |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
101 for x in row_attributes: |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
102 loomfile.ra[x] = rowdict[x] |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
103 for y in col_attributes: |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
104 loomfile.ca[y] = coldict[y] |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
105 for z in range(1, len(alayers)): |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
106 matrix = np.asarray(alayers[z]) |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
107 matrix = matrix.astype(float) |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
108 matrix = matrix.reshape(rowshape, colshape) |
c8e4d0b9ae8c
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/anndata/ commit dc9d19d1f902f3ed54009cd0e68c8518c284b856"
iuc
parents:
diff
changeset
|
109 loomfile[layernames[z]] = matrix |