def ConstructSubreferences(datasets, reference_file, output_dir, index = None, threads = 1, log = None): bwa_command = "bin/spades-bwa" if log == None: log = logging.getLogger('reference_construction') log.setLevel(logging.INFO) console = logging.StreamHandler(sys.stderr) console.setFormatter(logging.Formatter('%(message)s')) console.setLevel(logging.INFO) log.addHandler(console) support.ensure_dir_existence(output_dir) if not os.path.exists(output_dir): os.makedirs(output_dir) if index == None: log.info("Constructing index\n") index = alignment.index_bwa(bwa_command, log, reference_file, os.path.join(output_dir, "bwa_index"), "bwtsw") sam_dir = os.path.join(output_dir, "alignments") log.info("Aligning barcodes\n") sam_files = AlignToReference(datasets, sam_dir, bwa_command, log, index, threads) subreference_dir = os.path.join(output_dir, "subreferences") filtered_dir = os.path.join(output_dir, "filtered") support.recreate_dir(subreference_dir) support.recreate_dir(filtered_dir) log.info("Constructing subreferences") subreferences_list = [(barcode_id, ConstructSubreferenceFromSam(barcode_sam)) for barcode_id, barcode_sam in sam_files] log.info("Reading reference") reference = ReadReference(reference_file) log.info("Printing output") PrintAll([(barcode, filtered) for barcode, (filtered, subreference) in subreferences_list], reference, filtered_dir) PrintAll([(barcode, subreference) for barcode, (filtered, subreference) in subreferences_list], reference, subreference_dir) log.info("Subreference construction finished. See results in " + output_dir)
def main(argv): options = launch_options.Options(argv, spades_home, truspades_home, spades_version) support.ensure_dir_existence(options.output_dir) if options.test and not options.continue_launch: support.recreate_dir(options.output_dir) log = create_log(options) dataset_file = os.path.join(options.output_dir, "dataset.info") if options.continue_launch: dataset = barcode_extraction.ReadDataset(dataset_file, log) elif options.input_dirs is not None: dataset = generate_dataset(options.input_dirs, log) if dataset is None: log.info("Error: could not parse dataset from input directories\n") sys.exit(1) barcode_extraction.print_dataset(dataset, dataset_file, log) log.info("Dataset generated. See result in " + dataset_file) else: dataset = barcode_extraction.ReadDataset(options.dataset_file, log) barcode_extraction.print_dataset(dataset, dataset_file, log) log_dir = os.path.join(options.output_dir, "logs") support.ensure_dir_existence(log_dir) # if options.print_commands: # verify_exists(options.output_dir) # print_commands(commands, options) if options.mode == "run_truspades": RunTruSPAdes(dataset, log_dir, options, log) elif options.mode == "construct_subreferences": reference_construction.ConstructSubreferences(dataset, options.reference, options.output_dir, options.index, options.threads, log = None) log.info("TruSPAdes launch successfully finished") if options.test: CheckTestSuccess(options, log)
def ConstructSubreferences(datasets, reference_file, output_dir, index = None, threads = 1, log = None): bwa_command = "bin/bwa-spades" if log == None: log = logging.getLogger('reference_construction') log.setLevel(logging.INFO) console = logging.StreamHandler(sys.stderr) console.setFormatter(logging.Formatter('%(message)s')) console.setLevel(logging.INFO) log.addHandler(console) support.ensure_dir_existence(output_dir) if not os.path.exists(output_dir): os.makedirs(output_dir) if index == None: log.info("Constructing index\n") index = alignment.index_bwa(bwa_command, log, reference_file, os.path.join(output_dir, "bwa_index"), "bwtsw") sam_dir = os.path.join(output_dir, "alignments") log.info("Aligning barcodes\n") sam_files = AlignToReference(datasets, sam_dir, bwa_command, log, index, threads) subreference_dir = os.path.join(output_dir, "subreferences") filtered_dir = os.path.join(output_dir, "filtered") support.recreate_dir(subreference_dir) support.recreate_dir(filtered_dir) log.info("Constructing subreferences") subreferences_list = [(barcode_id, ConstructSubreferenceFromSam(barcode_sam)) for barcode_id, barcode_sam in sam_files] log.info("Reading reference") reference = ReadReference(reference_file) log.info("Printing output") PrintAll([(barcode, filtered) for barcode, (filtered, subreference) in subreferences_list], reference, filtered_dir) PrintAll([(barcode, subreference) for barcode, (filtered, subreference) in subreferences_list], reference, subreference_dir) log.info("Subreference construction finished. See results in " + output_dir)
def RunBarcodeQuast(barcodes, barcode_quast_dir, reference_dir, threads): quast_format = " ".join([ "quast", "--min-contig", "1000", "--contig-thresholds", "5000,8000,12000", "-e", "-R", os.path.join(reference_dir, "{0}.fasta"), "{1}", "-o", os.path.join(barcode_quast_dir, "{0}") ]) commands = [(barcode_id, quast_format.format(barcode_id, file_name)) for (barcode_id, file_name) in barcodes] support.recreate_dir(barcode_quast_dir) task = parallel_launcher.ExternalCallTask("", "") errors = parallel_launcher.run_in_parallel(task, commands, threads) sys.stderr.write(str(errors) + " barcodes failed")