def setup_maf_header(self): """ Sets up the maf header. """ self.maf_header = MafHeader.from_defaults( version=self.options["version"], annotation=self.options["annotation"], sort_order=BarcodesAndCoordinate(), fasta_index=self.options["reference_fasta_index"], ) header_date = BaseRunner.get_header_date() self.maf_header[header_date.key] = header_date if not self.options["tumor_only"]: normal_aliquot = MafHeaderRecord( key="normal.aliquot", value=self.options["normal_aliquot_uuid"] if not self.options["tumor_only"] else "", ) self.maf_header[normal_aliquot.key] = normal_aliquot tumor_aliquot = MafHeaderRecord( key="tumor.aliquot", value=self.options["tumor_aliquot_uuid"] ) self.maf_header[tumor_aliquot.key] = tumor_aliquot
def test_str(self): version = MafHeaderRecord(MafHeader.VersionKey, TestMafHeader.Version) record1 = MafHeaderRecord("key1", "value1") record2 = MafHeaderRecord("key2", "value2") header = MafHeader() version_line = "%s%s %s" % ( MafHeader.HeaderLineStartSymbol, MafHeader.VersionKey, TestMafHeader.Version, ) record1_line = "%s%s %s" % ( MafHeader.HeaderLineStartSymbol, record1.key, record1.value, ) record2_line = "%s%s %s" % ( MafHeader.HeaderLineStartSymbol, record2.key, record2.value, ) self.assertEqual(str(header), "") header[version.key] = version self.assertEqual(str(header), version_line) header[record1.key] = record1 self.assertEqual(str(header), "%s\n%s" % (version_line, record1_line)) header[record2.key] = record2 self.assertEqual( str(header), "%s\n%s\n%s" % (version_line, record1_line, record2_line))
def test_get_and_set(self): record = MafHeaderRecord("key", "value") self.assertEqual(record.key, "key") self.assertEqual(record.value, "value") record.key = "key2" record.value = "value2" self.assertEqual(record.key, "key2") self.assertEqual(record.value, "value2")
class TestScheme(MafScheme): _Columns = [ MafHeaderVersionRecord("test-scheme"), MafHeaderRecord("key1", "value1") ] @classmethod def version(cls): return "test-scheme" @classmethod def annotation_spec(cls): return "test-annotation" @classmethod def __column_dict__(cls): return OrderedDict([(column.key, column.__class__) for column in TestMafScheme.TestScheme._Columns ]) @classmethod def __column_desc__(cls): return OrderedDict([(column.key, str(column.__class__)) for column in TestMafScheme.TestScheme._Columns ])
def get_header_date(): """ Returns a MafHeaderRecord of the filedate. """ return MafHeaderRecord( key="filedate", value=datetime.date.today().strftime("%Y%m%d") )
def test_dict_methods(self): """ Checks that the header is well-behaved for a Mapping """ version = MafHeaderRecord(MafHeader.VersionKey, TestMafHeader.Version) record1 = MafHeaderRecord("key1", "value1") record2 = MafHeaderRecord("key1", "value2") header = MafHeader() # Set version self.assertNotIn(version.key, header) header[version.key] = version self.assertIn(version.key, header) self.assertTrue(len(header) == 1) self.assertListEqual(list(header.keys()), [MafHeader.VersionKey]) self.assertTrue(header.values(), [TestMafHeader.Version]) self.assertEqual(header.version(), TestMafHeader.Version) expected_scheme = GdcV1_0_0_BasicScheme() self.assertEqual(header.scheme().version(), expected_scheme.version()) self.assertEqual(header.scheme().annotation_spec(), expected_scheme.annotation_spec()) # Set when it is not in the header self.assertNotIn(record1.key, header) header[record1.key] = record1 self.assertIn(record1.key, header) self.assertTrue(len(header) == 2) self.assertListEqual(list(header.keys()), [MafHeader.VersionKey, "key1"]) self.assertTrue(header.values(), [TestMafHeader.Version, "value1"]) # Overwrite self.assertIn(record2.key, header) header[record2.key] = record2 self.assertIn(record2.key, header) self.assertTrue(len(header) == 2) self.assertListEqual(list(header.keys()), [MafHeader.VersionKey, "key1"]) self.assertTrue(header.values(), [TestMafHeader.Version, "value2"]) # Remove it del header[record2.key] self.assertNotIn(record2.key, header) self.assertTrue(len(header) == 1) self.assertListEqual(list(header.keys()), [MafHeader.VersionKey]) self.assertTrue(header.values(), [TestMafHeader.Version])
def test_from_line_trim_whitespace_at_the_end_of_value(self): for line in ["#key value ", "#key value ", "#key value\t"]: record, error = MafHeaderRecord.from_line(line=line) self.assertIsNotNone(record) self.assertIsNone(error) self.assertFalse(isinstance(record, MafHeaderVersionRecord)) self.assertTrue(isinstance(record, MafHeaderRecord)) self.assertEqual(record.key, "key") self.assertEqual(record.value, "value")
def __test_from_line_with_error(self, lines, tpe): """ A helper method for testing when MafHeaderRecord.from_line returns an error. """ for line in lines: record, error = MafHeaderRecord.from_line(line=line) self.assertIsNone(record) self.assertIsNotNone(error) self.assertEqual(error.tpe, tpe)
def test_from_line_version_record(self): """ Tests that a MafHeaderVersionRecord is returned from MafHeaderRecord.from_line only when it encounters MafHeader.VersionKey. """ for line in [ "#%s %s" % (MafHeader.VersionKey, version) for version in MafHeader.SupportedVersions ]: record, error = MafHeaderRecord.from_line(line=line) self.assertIsNotNone(record) self.assertIsNone(error) self.assertTrue(isinstance(record, MafHeaderVersionRecord)) self.assertTrue(isinstance(record, MafHeaderRecord)) for line in ["#garbage value", "#%sz value"]: record, error = MafHeaderRecord.from_line(line=line) self.assertIsNotNone(record) self.assertIsNone(error) self.assertFalse(isinstance(record, MafHeaderVersionRecord)) self.assertTrue(isinstance(record, MafHeaderRecord))
def test_str(self): record = MafHeaderRecord("key", "value") self.assertEqual(str(record), "#key value") record.key = "key2" record.value = "value2" self.assertEqual(str(record), "#key2 value2") record.key = " key2" record.value = " value2" self.assertEqual(str(record), "# key2 value2")