|
a |
|
b/singlecellmultiomics/libraryProcessing/sample_sheet.py |
|
|
1 |
import json |
|
|
2 |
|
|
|
3 |
def mark_autodetect(libname): |
|
|
4 |
lib = libname.lower() |
|
|
5 |
if 'k9me3' in lib: |
|
|
6 |
return 'H3K9me3' |
|
|
7 |
if 'k4me1' in lib: |
|
|
8 |
return 'H3K4me1' |
|
|
9 |
if 'k4me3' in lib: |
|
|
10 |
return 'H3K4me3' |
|
|
11 |
if 'k27me3' in lib: |
|
|
12 |
return 'H3K27me3' |
|
|
13 |
if 'k36me3' in lib: |
|
|
14 |
return 'H3K36me3' |
|
|
15 |
|
|
|
16 |
return 'NA' |
|
|
17 |
|
|
|
18 |
|
|
|
19 |
class SampleSheet: |
|
|
20 |
""" |
|
|
21 |
Class for reading and writing sample sheet information |
|
|
22 |
""" |
|
|
23 |
def __init__(self, path=None): |
|
|
24 |
self.data = {} |
|
|
25 |
if path is not None: |
|
|
26 |
self.load_json(path) |
|
|
27 |
|
|
|
28 |
def load_json(self,sample_sheet_location,merge=False): |
|
|
29 |
""" |
|
|
30 |
Load the data from the specified json file. |
|
|
31 |
When merge=True the data will be added to the current data (used for merging sheets) |
|
|
32 |
Duplicates will be overwritten by the last load file |
|
|
33 |
""" |
|
|
34 |
with open(sample_sheet_location) as f: |
|
|
35 |
if merge: |
|
|
36 |
for k,v in json.load(f).items(): |
|
|
37 |
if k not in self.data: |
|
|
38 |
self.data[k] = v |
|
|
39 |
else: |
|
|
40 |
self.data[k].update(v) |
|
|
41 |
else: |
|
|
42 |
self.data = json.load(f) |
|
|
43 |
def get(self,key,default): |
|
|
44 |
return self.data.get(key,default) |
|
|
45 |
|
|
|
46 |
def __getitem__(self,k): |
|
|
47 |
return self.data[k] |
|
|
48 |
|
|
|
49 |
def layout_well2index(self, layout_name): |
|
|
50 |
""" Get the well to index mapping for the supplied layout """ |
|
|
51 |
return {k:tuple(v) for k,v in self['well2coord'][self['layout_format'][layout_name]].items()} |
|
|
52 |
|
|
|
53 |
def index_to_condition(self, layout_name): |
|
|
54 |
well_to_condition = self['layouts'][layout_name] |
|
|
55 |
return {self['well2index'][self['layout_format'][layout_name]][k]:v for k,v in well_to_condition.items()} |
|
|
56 |
|
|
|
57 |
|
|
|
58 |
def index2well(self, layout_name: str) -> dict: |
|
|
59 |
""" |
|
|
60 |
Returns a dictionary index->well |
|
|
61 |
{1: 'A1', |
|
|
62 |
2: 'A2', |
|
|
63 |
3: 'A3', |
|
|
64 |
4: 'A4', |
|
|
65 |
5: 'A5', |
|
|
66 |
""" |
|
|
67 |
return {v:k for k,v in self['well2index'][self['layout_format'][layout_name]].items() } |
|
|
68 |
|
|
|
69 |
@property |
|
|
70 |
def libraries(self): |
|
|
71 |
|
|
|
72 |
libraries = set() |
|
|
73 |
for k in ['libtype','marks']: |
|
|
74 |
libraries.update( set(self.get(k,dict()).keys())) |
|
|
75 |
return libraries |
|
|
76 |
|
|
|
77 |
|
|
|
78 |
def drop_library(self, library): |
|
|
79 |
if type(library) is list: |
|
|
80 |
for l in library: |
|
|
81 |
self.drop_library(l) |
|
|
82 |
return |
|
|
83 |
for main_key in ['marks','condition','libtype','library_layout']: |
|
|
84 |
if main_key not in self.data: |
|
|
85 |
continue |
|
|
86 |
if library in self[main_key]: |
|
|
87 |
del self[main_key][library] |
|
|
88 |
|
|
|
89 |
def drop_mark(self, mark): |
|
|
90 |
""" Remove all libraries with the given mark """ |
|
|
91 |
if type(mark) is list: |
|
|
92 |
for m in mark: |
|
|
93 |
self.drop_mark(m) |
|
|
94 |
return |
|
|
95 |
to_prune = [] |
|
|
96 |
for sample, _mark in self['marks'].items(): |
|
|
97 |
if mark==_mark: |
|
|
98 |
to_prune.append(sample) |
|
|
99 |
|
|
|
100 |
self.drop_library(to_prune) |