Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

#!/usr/bin/env python3 

 

import sys, re, yaml, logging, fcsparser, subprocess 

from pathlib import Path 

from natsort import natsorted 

from pprint import pprint 

 

def parse_well_label(label): 

    fields = label.rsplit('/', 1) 

    if len(fields) == 1: 

        return None, fields[0] 

    else: 

        return fields 

 

def load_experiments(config_path, well_glob='**/*_{}_*.fcs'): 

    config_path = Path(config_path) 

 

    if config_path.suffix == '.py': 

        py_command = 'python', str(config_path) 

        yml_config = subprocess.check_output(py_command) 

        documents = list(yaml.load_all(yml_config)) 

 

    else: 

        with config_path.open() as file: 

            documents = list(yaml.load_all(file)) 

 

    if not documents: 

        raise UsageError("'{}' is empty.".format(config_path)) 

 

    # Find the *.fcs data files relevant to this experiment.  If there is a  

    # document with a mapping called "plates:", treat the values as paths to  

    # data directories and the keys as names that can refer to the directories  

    # in the rest of the file.  If there is a document with an assignment  

    # called "plate:", treat it as the path to the only data directory that  

    # will be used in the rest of the file.  If no data directory is specified  

    # by either of these two mechanisms, try to infer a path from the name of  

    # the YAML file itself. 

 

    inferred_path = config_path.parent / config_path.stem 

 

    def str_to_path(s): 

        return Path(s) if Path(s).is_absolute() else config_path.parent/s 

 

    def clean_up_plate_document(): 

        if len(documents[0]) > 1: 

            raise UsageError("Too many fields in 'plates' header.") 

        del documents[0] 

 

 

    if 'plates' in documents[0]: 

        plates = {k: str_to_path(v) for k,v in documents[0]['plates'].items()} 

        clean_up_plate_document() 

    elif 'plate' in documents[0]: 

        plates = {None: str_to_path(documents[0]['plate'])} 

        clean_up_plate_document() 

    elif inferred_path.is_dir(): 

        plates = {None: inferred_path} 

    else: 

        plates = {} 

 

    # Construct and fill in a list of experiments.  Well names are converted  

    # into paths based on the user-given glob pattern, then parsed and stored  

    # as pandas data frames.  Note that if a well is referenced more than once,  

    # it will also be parsed more than once.  This guarantees that each well  

    # can be processed independently, which is important for many workflows. 

 

    experiments = [] 

    includes = {} 

 

    def load_well(label): 

        # Short-circuit the case where the well has already been loaded, which  

        # is triggered by the "from" external reference machinery. 

 

        if isinstance(label, Well): 

            return label 

 

        # Parse well and plate names from the given label.  The plate name is  

        # optional, because often there is only one. 

 

        plate, well = parse_well_label(label) 

 

        # Find the *.fcs file referenced by the given label. 

 

        if plate not in plates: 

            raise UsageError( 

                    "Plate '{}' not defined.".format(plate) 

                    if plate is not None else 

                    "No default plate defined.") 

 

        plate_path = plates[plate] 

        well_paths = list(plate_path.glob(well_glob.format(well))) 

        if len(well_paths) == 0: 

            raise UsageError("No *.fcs files found for well '{}'".format(label)) 

        if len(well_paths) > 1: 

            raise UsageError("Multiple *.fcs files found for well '{}'".format(label)) 

        well_path = well_paths[0] 

 

        # Load the cell data for the given well. 

 

        logging.info('Loading {}'.format(well_path.name)) 

        meta, data = fcsparser.parse(str(well_path)) 

        return Well(label, meta, data) 

 

 

    for experiment in documents: 

        if not experiment: 

            raise UsageError("An empty experiment was found.\nDid you accidentally leave '---' at the end of the file?") 

 

        # Reference experiments from other files if the special "from" keyword  

        # is present. 

 

        if 'from' in experiment: 

            experiment = load_experiment( 

                    config_path.parent / experiment['from'], 

                    experiment['label']) 

 

        # Make sure each document has a label and a list of wells.  Other key-  

        # value pairs can be present but are not required. 

 

        if 'label' not in experiment: 

            raise UsageError("The following experiment is missing a label:\n\n{}".format(yaml.dump(experiment))) 

        if 'wells' not in experiment: 

            raise UsageError("The following experiment doesn't have any wells:\n\n{}".format(yaml.dump(experiment))) 

 

        # Set the well data for the comparison.  This requires converting the  

        # well names we were given into paths and parsing those files. 

 

        for well_type, well_names in experiment['wells'].items(): 

            experiment['wells'][well_type] = [load_well(x) for x in well_names] 

 

        experiments.append(experiment) 

 

    return experiments 

 

def load_experiment(config_path, experiment_label, well_glob='**/*_{}_*.fcs'): 

    experiments = load_experiments(config_path, well_glob=well_glob) 

    for experiment in experiments: 

        if experiment['label'] == experiment_label: 

            return experiment 

    raise UsageError("No experiment named '{}'".format(experiment_label)) 

 

 

class Well: 

 

    def __init__(self, label, meta, data): 

        self.label = label 

        self.meta = meta 

        self.data = data 

 

    def __repr__(self): 

        return 'Well({})'.format(self.label) 

 

 

 

def define_96_well_plates(define_well, define_experiment=lambda expt: None, 

        plate=None, plates=None, plate_order=None, layout='col/row/plate', 

        **extra_params): 

 

    header = [] 

    script_name = Path(sys.argv[0]).stem 

 

    # If the user didn't specify any plates: 

    if plate is None and plates is None: 

        plates = {None: ''} 

 

    # If the user specified a single plate: 

    elif plate is not None and plates is None: 

        header.append({'plate': plate.replace('$', script_name)}) 

        plates = {None: plate} 

 

    # If the user specifies multiple plates: 

    elif plate is None and plates is not None: 

        header.append({'plates': { 

            k: v.replace('$', script_name) for k, v in plates.items()}}) 

 

    else: 

        raise UsageError("cannot specify both 'plate' and 'plates'") 

 

    # Set the order in which the plates should be considered.  The default  

    # order is alphabetical. 

 

    if plate_order is None: 

        plate_order = natsorted(plates) 

 

    # Understand how the plates are indexed, i.e. do the indices increment by  

    # column then row then plate, or by column then plate then row, etc. 

 

    steps = layout.split('/') 

 

    if len(steps) == 2: 

        steps.append('plate') 

    if set(steps) != {'row', 'col', 'plate'}: 

        raise UsageError("invalid layout: '{}'".format(layout)) 

 

    strides = { 

            'col': 12, 

            'row': 8, 

            'plate': len(plates), 

    } 

    divisors = { 

            steps[0]: 1, 

            steps[1]: strides[steps[0]], 

            steps[2]: strides[steps[0]] * strides[steps[1]], 

    } 

 

    # Create experiments by iterating through each well and associating a  

    # labels and a condition with each one. 

 

    experiments = [] 

 

    for i in range(96 * len(plates)): 

 

        # Figure out which row, column, and plate this index refers to. 

 

        row = (i // divisors['row']) % strides['row'] 

        col = (i // divisors['col']) % strides['col'] 

        plate = plate_order[(i // divisors['plate']) % strides['plate']] 

 

        # Get the experiment and condition to associate with this well from the  

        # user.  Skip this well if define_well() returns None. 

 

        well = WellCursor96(i, row, col, plate) 

        definition = define_well(well) 

 

        if definition is None: 

            continue 

 

        label, condition = definition 

 

        # If an experiment with this label already exists, find it.   

        # Otherwise create an empty experiment data structure and add  

        # it to the list of experiments. 

 

        try: 

            experiment = next( 

                    expt for expt in experiments 

                    if expt['label'] == label) 

 

        except StopIteration: 

            experiment = extra_params.copy() 

            experiment['label'] = label 

            experiment['wells'] = {} 

            experiments.append(experiment) 

 

        # Associate this well with the given condition. 

 

        experiment['wells'].setdefault(condition, []).append(str(well)) 

 

    # Allow the user to add custom parameters to each experiment. 

 

    for experiment in experiments: 

        define_experiment(experiment) 

 

    # Export the experiments to YAML and either print them to stdout or save  

    # them to a file, depending on the command-line arguments. 

 

    output_path = script_name + '.yml' 

 

    import docopt 

    args = docopt.docopt("""\ 

Usage: 

    {script_name}.py [-o] 

 

Options: 

    -o --output 

        Save the experimental layout to ``{output_path}`` 

""".format(**locals())) 

 

    # I wanted to use yaml.dump_all() here, but yaml.dump() has a better  

    # default indentation algorithm. 

 

    dump_config = lambda **kwargs: '---\n'.join( 

            yaml.dump(x, **kwargs) for x in header + experiments) 

 

    if args['--output']: 

        with open(output_path, 'w') as file: 

            file.write(dump_config()) 

    else: 

        print(dump_config()) 

 

class WellCursor96: 

 

    def __init__(self, index, row, col, plate): 

        self._index = index 

        self._row = row 

        self._col = col 

        self._plate = plate 

 

    def __repr__(self): 

        return self.label 

 

    def __eq__(self, other): 

        try: 

            return (self.index, self.row, self.col, self.plate) == \ 

                   (other.index, other.row, other.col, other.plate) 

 

        except AttributeError: 

            other_plate, other_well = parse_well_label(str(other)) 

            other_well_match = re.match('([A-H])([0-9]{1,2})', other_well) 

 

            if not other_well_match: 

                raise UsageError("can't compare {} to {}".format(other, self)) 

 

            other_row = list('ABCDEFGH').index(other_well_match.group(1)) 

            other_col = int(other_well_match.group(2)) - 1 

 

            return (self.row, self.col, self.plate) == \ 

                   (other_row, other_col, other_plate) 

 

    @property 

    def index(self): 

        return self._index 

 

    @property 

    def row(self): 

        return self._row 

 

    @property 

    def col(self): 

        return self._col 

 

    @property 

    def plate(self): 

        return self._plate 

 

    @property 

    def label(self): 

        row = 'ABCDEFGH'[self.row] 

        col = self.col + 1 

        label = '{}{:02d}'.format(row, col) 

 

        if self.plate: 

            label = '{}/{}'.format(self.plate, label) 

 

        return label 

 

 

 

class UsageError (Exception): 

    """ 

    Indicate errors caused by invalid user input. 

    """ 

    def __init__(self, message): 

        super().__init__(message)