def test_write_lines(self): with TempDirectory() as output_file: writer = FileWriter(os.path.join(output_file.path, "A.tmp")) writer.open() writer.write("1\n2\n") writer.write("3") writer.close() actual_file = open(os.path.join(output_file.path, "A.tmp")) actual_output = actual_file.readlines() actual_file.close() self.assertEquals(["1\n", "2\n", "3"], actual_output)
def execute(args, execution_context): validate_args(args) output_dir = os.path.abspath(args.output) unclaimed_readers, trans_vcf_readers = _claim_readers(args) _log_unclaimed_readers(unclaimed_readers) logger.info("Processing [{}] VCF file(s) from [{}]", len(trans_vcf_readers), args.input) new_tags = [ _ExcludeMalformedRef(), _ExcludeMalformedAlt(), _ExcludeMissingAlt() ] for i, trans_vcf_reader in enumerate(trans_vcf_readers): logger.info("Translating file {}/{} [{}]", i + 1, len(trans_vcf_readers), trans_vcf_reader.file_name) new_filename = _mangle_output_filename(trans_vcf_reader.file_name) output_filepath = os.path.join(output_dir, new_filename) file_writer = FileWriter(output_filepath) _translate_files( trans_vcf_reader, new_tags, execution_context, file_writer, ) logger.info("Wrote [{}] VCF file(s)", len(trans_vcf_readers))
def test_hashable(self): s = set([FileWriter("foo")]) s.add(FileWriter("foo")) self.assertEquals(1, len(s))
def test_file_name(self): writer = FileWriter("foo/bar/baz.tmp") self.assertEquals("baz.tmp", writer.file_name)
def test_equality(self): self.assertEquals(FileWriter("foo"), FileWriter("foo")) self.assertNotEquals(FileWriter("foo"), FileWriter("bar")) self.assertNotEquals(FileWriter("foo"), 1)
def test_write(self): with TempDirectory() as output_file: file_path = os.path.join(output_file.path, "test.tmp") writer = FileWriter(file_path) writer.open() writer.write("A") writer.write("B\n") writer.write("CD\n") writer.close() actual_output = output_file.read('test.tmp', encoding='utf8') expected_output = "AB|CD|".replace('|', os.linesep) self.assertEquals(expected_output, actual_output)
def test_write(self): with TempDirectory() as output_file: file_path = os.path.join(output_file.path, "test.tmp") writer = FileWriter(file_path) writer.open() writer.write("A") writer.write("B\n") writer.write("CD\n") writer.close() actual_output = output_file.read("test.tmp", encoding="utf8") expected_output = "AB|CD|".replace("|", os.linesep) self.assertEquals(expected_output, actual_output)
def _sort_vcf(reader, sorted_dir): vcf_records = [] reader.open() for vcf_record in reader.vcf_records(): vcf_records.append(vcf_record) reader.close() vcf_records.sort() writer = FileWriter(os.path.join(sorted_dir, reader.file_name)) writer.open() writer.write("\n".join(reader.metaheaders) + "\n") writer.write(reader.column_header + "\n") for vcf_record in vcf_records: writer.write(vcf_record.text()) writer.close() reader = MergeVcfReader(vcf.FileReader(writer.output_filepath)) return reader