diff --git a/extras/migrations/010-db-chrname-add-organelle.migration b/extras/migrations/010-db-chrname-add-organelle.migration new file mode 100755 index 0000000000000000000000000000000000000000..c7e541af02cb8bc03b021697fa4696b474a9abf6 --- /dev/null +++ b/extras/migrations/010-db-chrname-add-organelle.migration @@ -0,0 +1,66 @@ +#!/usr/bin/env python +""" +Add a column 'organelle_type' to the 'ChrName' tables. + +Usage: + ./010-db-chrname-add-organelle.migration [migrate] +""" + + +import migration + + +def check(): + """ + Check if migration is needed. + """ + connection = migration.db_connect('hg18') + cursor = connection.cursor() + cursor.execute('SHOW COLUMNS FROM ChrName WHERE field = "organelle_type";') + has_column_hg18 = len(cursor.fetchall()) > 0 + connection.close() + + connection = migration.db_connect('hg19') + cursor = connection.cursor() + cursor.execute('SHOW COLUMNS FROM ChrName WHERE field = "organelle_type";') + has_column_hg19 = len(cursor.fetchall()) > 0 + connection.close() + + if has_column_hg19 != has_column_hg19: + migration.fatal('Installation is not in a recognizable state. Fix manually.') + return not has_column_hg19 + + +def migrate(): + """ + Perform migration. + """ + connection = migration.db_connect('hg18') + cursor = connection.cursor() + cursor.execute(""" + ALTER TABLE ChrName + ADD COLUMN organelle_type enum('chromosome','mitochondrion') NOT NULL DEFAULT 'chromosome' AFTER name;""") + connection.commit() + cursor = connection.cursor() + cursor.execute(""" + UPDATE ChrName SET organelle_type = 'mitochondrion' WHERE name = 'chrM';""") + connection.commit() + connection.close() + migration.info('Added column hg18.ChrName.organelle_type') + + connection = migration.db_connect('hg19') + cursor = connection.cursor() + cursor.execute(""" + ALTER TABLE ChrName + ADD COLUMN organelle_type enum('chromosome','mitochondrion') NOT NULL DEFAULT 'chromosome' AFTER name;""") + connection.commit() + cursor = connection.cursor() + cursor.execute(""" + UPDATE ChrName SET organelle_type = 'mitochondrion' WHERE name = 'chrM';""") + connection.commit() + connection.close() + migration.info('Added column hg19.ChrName.organelle_type') + + +if __name__ == '__main__': + migration.main(check, migrate) diff --git a/extras/post-install.sh b/extras/post-install.sh index 0103866511708e77460e27ef9d78ec3a3eba3fad..b32c9c8d2ce0188fc50c391dd6d66ee98310b1e0 100644 --- a/extras/post-install.sh +++ b/extras/post-install.sh @@ -98,6 +98,7 @@ cat << EOF | mysql -u mutalyzer -D hg18 CREATE TABLE ChrName ( AccNo char(20) NOT NULL, name char(20) NOT NULL, + organelle_type enum('chromosome','mitochondrion') NOT NULL DEFAULT 'chromosome', PRIMARY KEY (AccNo) ) ENGINE = MYISAM; CREATE TABLE Mapping ( @@ -118,33 +119,33 @@ CREATE TABLE Mapping ( source varchar(20) DEFAULT NULL, INDEX (transcript) ) ENGINE = MYISAM; -INSERT INTO ChrName (AccNo, name) VALUES -('NC_000001.9', 'chr1'), -('NC_000002.10', 'chr2'), -('NC_000003.10', 'chr3'), -('NC_000004.10', 'chr4'), -('NC_000005.8', 'chr5'), -('NC_000006.10', 'chr6'), -('NC_000007.12', 'chr7'), -('NC_000008.9', 'chr8'), -('NC_000009.10', 'chr9'), -('NC_000010.9', 'chr10'), -('NC_000011.8', 'chr11'), -('NC_000012.10', 'chr12'), -('NC_000013.9', 'chr13'), -('NC_000014.7', 'chr14'), -('NC_000015.8', 'chr15'), -('NC_000016.8', 'chr16'), -('NC_000017.9', 'chr17'), -('NC_000018.8', 'chr18'), -('NC_000019.8', 'chr19'), -('NC_000020.9', 'chr20'), -('NC_000021.7', 'chr21'), -('NC_000022.9', 'chr22'), -('NC_000023.9', 'chrX'), -('NC_000024.8', 'chrY'), -('NC_001807.4', 'chrM'), -('NT_113891.1', 'chr6_cox_hap1'), +INSERT INTO ChrName (AccNo, name, organelle_type) VALUES +('NC_000001.9', 'chr1', 'chromosome'), +('NC_000002.10', 'chr2', 'chromosome'), +('NC_000003.10', 'chr3', 'chromosome'), +('NC_000004.10', 'chr4', 'chromosome'), +('NC_000005.8', 'chr5', 'chromosome'), +('NC_000006.10', 'chr6', 'chromosome'), +('NC_000007.12', 'chr7', 'chromosome'), +('NC_000008.9', 'chr8', 'chromosome'), +('NC_000009.10', 'chr9', 'chromosome'), +('NC_000010.9', 'chr10', 'chromosome'), +('NC_000011.8', 'chr11', 'chromosome'), +('NC_000012.10', 'chr12', 'chromosome'), +('NC_000013.9', 'chr13', 'chromosome'), +('NC_000014.7', 'chr14', 'chromosome'), +('NC_000015.8', 'chr15', 'chromosome'), +('NC_000016.8', 'chr16', 'chromosome'), +('NC_000017.9', 'chr17', 'chromosome'), +('NC_000018.8', 'chr18', 'chromosome'), +('NC_000019.8', 'chr19', 'chromosome'), +('NC_000020.9', 'chr20', 'chromosome'), +('NC_000021.7', 'chr21', 'chromosome'), +('NC_000022.9', 'chr22', 'chromosome'), +('NC_000023.9', 'chrX', 'chromosome'), +('NC_000024.8', 'chrY', 'chromosome'), +('NC_001807.4', 'chrM', 'mitochondrion'), +('NT_113891.1', 'chr6_cox_hap1', 'chromosome'), ('NT_113959.1', 'chr22_h2_hap1'); EOF @@ -164,6 +165,7 @@ cat << EOF | mysql -u mutalyzer -D hg19 CREATE TABLE ChrName ( AccNo char(20) NOT NULL, name char(20) NOT NULL, + organelle_type enum('chromosome','mitochondrion') NOT NULL DEFAULT 'chromosome', PRIMARY KEY (AccNo) ) ENGINE = MYISAM; CREATE TABLE Mapping ( @@ -184,41 +186,41 @@ CREATE TABLE Mapping ( source varchar(20) DEFAULT NULL, INDEX (transcript) ) ENGINE = MYISAM; -INSERT INTO ChrName (AccNo, name) VALUES -('NC_000001.10', 'chr1'), -('NC_000002.11', 'chr2'), -('NC_000003.11', 'chr3'), -('NC_000004.11', 'chr4'), -('NC_000005.9', 'chr5'), -('NC_000006.11', 'chr6'), -('NC_000007.13', 'chr7'), -('NC_000008.10', 'chr8'), -('NC_000009.11', 'chr9'), -('NC_000010.10', 'chr10'), -('NC_000011.9', 'chr11'), -('NC_000012.11', 'chr12'), -('NC_000013.10', 'chr13'), -('NC_000014.8', 'chr14'), -('NC_000015.9', 'chr15'), -('NC_000016.9', 'chr16'), -('NC_000017.10', 'chr17'), -('NC_000018.9', 'chr18'), -('NC_000019.9', 'chr19'), -('NC_000020.10', 'chr20'), -('NC_000021.8', 'chr21'), -('NC_000022.10', 'chr22'), -('NC_000023.10', 'chrX'), -('NC_000024.9', 'chrY'), -('NC_012920.1', 'chrM'), -('NT_167244.1', 'chr6_apd_hap1'), -('NT_113891.2', 'chr6_cox_hap2'), -('NT_167245.1', 'chr6_dbb_hap3'), -('NT_167246.1', 'chr6_mann_hap4'), -('NT_167247.1', 'chr6_mcf_hap5'), -('NT_167248.1', 'chr6_qbl_hap6'), -('NT_167249.1', 'chr6_ssto_hap7'), -('NT_167250.1', 'chr4_ctg9_hap1'), -('NT_167251.1', 'chr17_ctg5_hap1'); +INSERT INTO ChrName (AccNo, name, organelle_type) VALUES +('NC_000001.10', 'chr1', 'chromosome'), +('NC_000002.11', 'chr2', 'chromosome'), +('NC_000003.11', 'chr3', 'chromosome'), +('NC_000004.11', 'chr4', 'chromosome'), +('NC_000005.9', 'chr5', 'chromosome'), +('NC_000006.11', 'chr6', 'chromosome'), +('NC_000007.13', 'chr7', 'chromosome'), +('NC_000008.10', 'chr8', 'chromosome'), +('NC_000009.11', 'chr9', 'chromosome'), +('NC_000010.10', 'chr10', 'chromosome'), +('NC_000011.9', 'chr11', 'chromosome'), +('NC_000012.11', 'chr12', 'chromosome'), +('NC_000013.10', 'chr13', 'chromosome'), +('NC_000014.8', 'chr14', 'chromosome'), +('NC_000015.9', 'chr15', 'chromosome'), +('NC_000016.9', 'chr16', 'chromosome'), +('NC_000017.10', 'chr17', 'chromosome'), +('NC_000018.9', 'chr18', 'chromosome'), +('NC_000019.9', 'chr19', 'chromosome'), +('NC_000020.10', 'chr20', 'chromosome'), +('NC_000021.8', 'chr21', 'chromosome'), +('NC_000022.10', 'chr22', 'chromosome'), +('NC_000023.10', 'chrX', 'chromosome'), +('NC_000024.9', 'chrY', 'chromosome'), +('NC_012920.1', 'chrM', 'mitochondrion'), +('NT_167244.1', 'chr6_apd_hap1', 'chromosome'), +('NT_113891.2', 'chr6_cox_hap2', 'chromosome'), +('NT_167245.1', 'chr6_dbb_hap3', 'chromosome'), +('NT_167246.1', 'chr6_mann_hap4', 'chromosome'), +('NT_167247.1', 'chr6_mcf_hap5', 'chromosome'), +('NT_167248.1', 'chr6_qbl_hap6', 'chromosome'), +('NT_167249.1', 'chr6_ssto_hap7', 'chromosome'), +('NT_167250.1', 'chr4_ctg9_hap1', 'chromosome'), +('NT_167251.1', 'chr17_ctg5_hap1', 'chromosome'); EOF echo -e "${COLOR_INFO}Populating Mapping table with NCBI data (hg19)${COLOR_END}" diff --git a/mutalyzer/Db.py b/mutalyzer/Db.py index ae295b3cf0601854323bafc6ed7bd609019c797a..42386156f539cf0f583124c9d6a5d870da4d8f83 100644 --- a/mutalyzer/Db.py +++ b/mutalyzer/Db.py @@ -480,14 +480,14 @@ class Mapping(Db) : """ statement = """ - SELECT AccNo + SELECT AccNo, organelle_type FROM ChrName WHERE name = %s; """, name ret = self.query(statement) if ret : - return ret[0][0] + return ret[0] return None #chromAcc diff --git a/mutalyzer/mapping.py b/mutalyzer/mapping.py index 38c0d48e0fdc4baa45b2af78b1a88a4094c74ab7..e005a895bd243f192beee1db4710a2d59b117bc3 100644 --- a/mutalyzer/mapping.py +++ b/mutalyzer/mapping.py @@ -524,7 +524,10 @@ class Converter(object) : else: variants = [self.parseTree.RawVar] - chromAcc = self.__database.chromAcc(self.dbFields["chromosome"]) + try: + chromAcc, organelle_type = self.__database.chromAcc(self.dbFields["chromosome"]) + except TypeError: + return None # Construct the variant descriptions descriptions = [] @@ -555,7 +558,10 @@ class Converter(object) : else: description = '[' + ';'.join(descriptions) + ']' - return "%s:g.%s" % (chromAcc, description) + if organelle_type == 'mitochondrion': + return "%s:m.%s" % (chromAcc, description) + else: + return "%s:g.%s" % (chromAcc, description) #c2chrom def chromosomal_positions(self, positions, reference, version=None): @@ -622,8 +628,9 @@ class Converter(object) : if variant.startswith('chr') and ':' in variant: preco, postco = variant.split(':', 1) - chrom = self.__database.chromAcc(preco) - if chrom is None : + try: + chrom, _ = self.__database.chromAcc(preco) + except TypeError: self.__output.addMessage(__file__, 4, "ENOTINDB", "The accession number %s could not be found in our database (or is not a chromosome)." % preco) @@ -650,7 +657,7 @@ class Converter(object) : """ if not self._parseInput(variant) : - return None + return None acc = self.parseTree.RefSeqAcc version = self.parseTree.Version diff --git a/mutalyzer/services/rpc.py b/mutalyzer/services/rpc.py index 48b3ee1896214608519c037e7a3c3780c241e5d7..1866df350f0ca39d9c04aa2227993fd18d8765ec 100644 --- a/mutalyzer/services/rpc.py +++ b/mutalyzer/services/rpc.py @@ -582,7 +582,7 @@ class MutalyzerService(ServiceBase): "Finished processing chromAccession(%s %s)" % (build, name)) del D,L - return result + return result[0] #chromAccession @srpc(Mandatory.String, Mandatory.String, _returns=Mandatory.String) @@ -674,7 +674,7 @@ class MutalyzerService(ServiceBase): if "c." in variant or "n." in variant: result = [converter.c2chrom(variant)] - elif "g." in variant : + elif "g." in variant or "m." in variant: result = converter.chrom2c(variant, "list", gene=gene) else: result = [""] diff --git a/mutalyzer/website.py b/mutalyzer/website.py index 29c1480f74f8fee58149c0eb77953d99b342e6ab..82151f1bd5b04f38e115039f17ff05204f15e7ea 100644 --- a/mutalyzer/website.py +++ b/mutalyzer/website.py @@ -594,7 +594,7 @@ class PositionConverter: variant = converter.correctChrVariant(variant) if variant: - if not(":c." in variant or ":n." in variant or ":g." in variant): + if not(":c." in variant or ":n." in variant or ":g." in variant or ":m." in variant): #Bad name grammar = Grammar(output) grammar.parse(variant) @@ -605,7 +605,7 @@ class PositionConverter: attr["gName"] = variant - if variant and ":g." in variant: + if variant and (":g." in variant or ":m." in variant): # Do the g2c dance variants = converter.chrom2c(variant, "dict") if variants is None: @@ -1412,9 +1412,9 @@ class Uploader: name = 'chr%s' % name database = Db.Mapping(build) - accession = database.chromAcc(name) - - if not accession: + try: + accession, _ = database.chromAcc(name) + except TypeError: raise InputException('Chromosome not available for build %s: %s' % (build, name))