def partial_digests_analysis(self, data, clones_observations): analysis = clones_observations.partial_digests_analysis() best = max(analysis, key=lambda a: analysis[a]["valid_clones"]) if analysis[best]["valid_clones"] == analysis[()]["valid_clones"]: return { "message": "The partial digest analysis did not find any " "significant results", "success": "yeah!", } ax = clones_observations.plot_partial_digests_analysis(analysis) validations = analysis[best]["validations"] observations = ClonesObservations( clones_observations.clones, clones_observations.constructs_records, partial_cutters=best, ) pdf_data = observations.plot_all_validations_patterns(validations) return { "pdf_file": { "data": data_to_html_data(pdf_data, "pdf"), "name": "digest_validation_assuming_partial_%s.pdf" % "_".join(best), "mimetype": "application/pdf", }, "message": ("The analysis shows that the following enzyme(s) were" " possibly only partially cutting: %s. <br/>The report below" " shows the validation under this hypothesis.") % (", ".join(["<b>%s</b>" % b for b in best])), "figure_data": matplotlib_figure_to_svg_base64_data(ax.figure, bbox_inches="tight"), "success": "yeah!", }
def partial_digests_analysis(self, data, clones_observations): analysis = clones_observations.partial_digests_analysis() best = max(analysis, key=lambda a: analysis[a]['valid_clones']) if analysis[best]['valid_clones'] == analysis[()]['valid_clones']: return { 'message': 'The partial digest analysis did not find any ' 'significant results', 'success': 'yeah!' } ax = clones_observations.plot_partial_digests_analysis(analysis) validations = analysis[best]['validations'] observations = ClonesObservations( clones_observations.clones, clones_observations.constructs_records, partial_cutters=best) pdf_data = observations.plot_all_validations_patterns(validations) return { 'pdf_file': { 'data': data_to_html_data(pdf_data, 'pdf'), 'name': 'digest_validation_assuming_partial_%s.pdf' % "_".join(best), 'mimetype': 'application/pdf' }, 'message': ("The analysis shows that the following enzyme(s) were" " possibly only partially cutting: %s. <br/>The report below" " shows the validation under this hypothesis.") % (", ".join(["<b>%s</b>" % b for b in best])), 'figure_data': matplotlib_figure_to_svg_base64_data(ax.figure, bbox_inches='tight'), 'success': 'yeah!' }
def test_complex_validation(tmpdir): data_dir = os.path.join('tests', 'test_data', 'complex_validation_data') records_path = os.path.join(data_dir, 'constructs_sequences.zip') constructs_map_path = os.path.join(data_dir, 'constructs_map.xls') digestions_map_path = os.path.join(data_dir, 'digestions_map.xls') aati_zip_path = os.path.join(data_dir, 'digestion_results.zip') clones = ClonesObservations.from_files( records_path=records_path, constructs_map_path=constructs_map_path, aati_zip_path=aati_zip_path, digestions_map_path=digestions_map_path ) validations = clones.validate_all_clones(relative_tolerance=0.03) clones.plot_validations_plate_map(validations) clones.plot_all_validations_patterns(validations) partial_digest_analysis = clones.partial_digests_analysis() clones.plot_partial_digests_analysis(partial_digest_analysis) summary_table = clones.validations_summary_table( validations, target=os.path.join(str(tmpdir), 'table.csv'))
def work(self): self.logger(message="Reading the files...") data = self.data # PARSE ALL FILES constructs_records = records_from_data_files(data.constructsSequences) if data.circularSequences: for record in constructs_records: record.linear = False constructs_records = {r.id: r for r in constructs_records} constructs_records = OrderedDict(sorted(constructs_records.items())) constructs_plate = plate_from_platemap_spreadsheet( file_to_filelike_object(data.constructsMap), file_type=file_type(data.constructsMap), data_field='construct', headers=True) constructs_map = OrderedDict([ (well.name, well.data.construct) for well in constructs_plate.iter_wells(direction='row') if 'construct' in well.data and str(well.data.construct) != 'nan' ]) unknown_constructs = {} for well, construct in constructs_map.items(): if construct not in constructs_records: unknown_constructs[construct] = { "well": well, "suggestions": did_you_mean(construct, constructs_records) } if len(unknown_constructs): return {"success": False, "unknown_constructs": unknown_constructs} if data.uniqueDigestion: digestion = tuple(data.digestion) digestions_map = OrderedDict([(wellname, digestion) for wellname in constructs_map]) else: digestions_plate = plate_from_platemap_spreadsheet( file_to_filelike_object(data.digestionsMap), file_type=file_type(data.digestionsMap), data_field='digestion', headers=True) digestions_map = OrderedDict([ (well.name, tuple(well.data.digestion.split(', '))) for well in digestions_plate.iter_wells(direction='row') if 'digestion' in well.data and str(well.data.digestion) != 'nan' ]) archive = file_to_filelike_object(data.fragmentAnalysisArchive) # ANALYZE ALL FILES AND VALIDATE BANDS self.logger(message="Analyzing the data...") observations = BandsObservation.from_aati_fa_archive( archive, ignore_bands_under=data.ignoreBandsUnder) clones = Clone.from_bands_observations(observations, constructs_map, digestions_map) clones_observations = ClonesObservations(clones, constructs_records) if data.subanalysis == 'partial_digests': return self.partial_digests_analysis(data, clones_observations) else: return self.validation_analysis(data, clones_observations)
def work(self): self.logger(message="Reading the files...") data = self.data # PARSE ALL FILES constructs_records = records_from_data_files(data.constructsSequences) for record in constructs_records: set_record_topology(record, data.topology) constructs_records = {r.id: r for r in constructs_records} constructs_records = OrderedDict(sorted(constructs_records.items())) constructs_plate = plate_from_platemap_spreadsheet( file_to_filelike_object(data.constructsMap), file_type=file_type(data.constructsMap), data_field="construct", headers=True, ) constructs_map = OrderedDict([ (well.name, well.data.construct) for well in constructs_plate.iter_wells(direction="row") if "construct" in well.data and str(well.data.construct) != "nan" ]) unknown_constructs = {} for well, construct in constructs_map.items(): if construct not in constructs_records: unknown_constructs[construct] = { "well": well, "suggestions": did_you_mean(construct, constructs_records), } if len(unknown_constructs): return {"success": False, "unknown_constructs": unknown_constructs} if data.uniqueDigestion: digestion = tuple(data.digestion) digestions_map = OrderedDict([(wellname, digestion) for wellname in constructs_map]) else: digestions_plate = plate_from_platemap_spreadsheet( file_to_filelike_object(data.digestionsMap), file_type=file_type(data.digestionsMap), data_field="digestion", headers=True, ) digestions_map = OrderedDict([ (well.name, tuple(well.data.digestion.split(", "))) for well in digestions_plate.iter_wells(direction="row") if "digestion" in well.data and str(well.data.digestion) != "nan" ]) archive = file_to_filelike_object(data.fragmentAnalysisArchive) # ANALYZE ALL FILES AND VALIDATE BANDS self.logger(message="Analyzing the data...") observations = BandsObservation.from_aati_fa_archive( archive, ignore_bands_under=data.ignoreBandsUnder, min_rfu_size_ratio=data.rfuSizeRatio, ) clones = Clone.from_bands_observations(observations, constructs_map, digestions_map) clones_observations = ClonesObservations(clones, constructs_records) if data.subanalysis == "partial_digests": return self.partial_digests_analysis(data, clones_observations) else: return self.validation_analysis(data, clones_observations)
clones_map = { well.name: well.data.construct for well in clones_plate.iter_wells() if 'construct' in well.data and str(well.data.construct) != 'nan' } bands_observations = BandsObservation.from_aati_fa_archive( data_dir.aati_files_zip._path) clones = { well_name: Clone(name=well_name, construct_id=clones_map[well_name], digestions={("NcoI",): bands_observations[well_name]}) for well_name, construct_id in clones_map.items() } # VALIDATE ALL CLONES WITH BANDWITCH clones_observations = ClonesObservations(clones, constructs_dict) validations = clones_observations.validate_all_clones(relative_tolerance=0.03) validations_summary = clones_observations.validations_summary(validations) # CREATE A FOLDER WITH VALIDATION REPORTS report_root = flametree.file_tree('.').examples_output._dir('band_validation') report_root._file('validations.pdf').write( clones_observations.plot_all_validations_patterns(validations) ) ax = clones_observations.plot_validations_plate_map(validations) ax.figure.savefig(report_root._file('success_map.pdf').open('wb'), format='pdf', bbox_inches='tight')