-
Notifications
You must be signed in to change notification settings - Fork 4
/
18_cleanup_duplicate_entries.py
executable file
·105 lines (88 loc) · 3.5 KB
/
18_cleanup_duplicate_entries.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#! /usr/bin/python3
#
# This source code is part of icgc, an ICGC processing pipeline.
#
# Icgc is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Icgc is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see<http://www.gnu.org/licenses/>.
#
# Contact: ivana.mihalek@gmail.com
#
from config import Config
from icgc_utils.common_queries import *
from icgc_utils.processes import *
#########################################
#########################################
# profile decorator is for the use with kernprof (a line profiler):
# ./icgc_utils/kernprof.py -l 18_cleanup_duplicate_entries.py
# followed by
# python3 -m line_profiler 18_cleanup_duplicate_entries.py.lprof
# see here https://github.com/rkern/line_profiler#line-profiler
# the reason I am using local kernprof.py is that I don't know where pip
# installed its version (if anywhere)
#@profile
def remove_duplicates(table_rows, other_args, verbose=False):
table = other_args[0]
db = connect_to_mysql(Config.mysql_conf_file)
cursor = db.cursor()
switch_to_db(cursor,"icgc")
# loop over all duplicate entries
for line in table_rows:
[mega_id, ct] = line
if not mega_id: continue # we do not have one of the IDs (happens with TCGA)
[icgc_mutation_id, icgc_donor_id, icgc_specimen_id, icgc_sample_id] = mega_id.split("_")
# check the full length of the entry
qry = "select * from %s " % table
qry += "where icgc_mutation_id = '%s' " % icgc_mutation_id
qry += "and icgc_donor_id = '%s' " % icgc_donor_id
qry += "and icgc_specimen_id = '%s' " % icgc_specimen_id
qry += "and icgc_sample_id = '%s' " % icgc_sample_id
ret2 = search_db(cursor,qry)
resolve_duplicate_mutations(cursor, table, ret2, verbose)
cursor.close()
db.close()
print ("\tprocess {} exiting; worked on table {}".format(get_process_id(), table))
return
#########################################
#########################################
def main():
print("disabled")
exit()
db = connect_to_mysql(Config.mysql_conf_file)
cursor = db.cursor()
#########################
# which temp somatic tables do we have
qry = "select table_name from information_schema.tables "
qry += "where table_schema='icgc' and table_name like '%simple_somatic'"
tables = [field[0] for field in search_db(cursor,qry)]
switch_to_db(cursor,"icgc")
number_of_chunks = 10 # myISAM does not deadlock
for table in tables:
print("\n====================")
print("inspecting ", table)
# column names/headers
# a hack to get all entries that have all relevant ids identical
qry = "select concat(icgc_mutation_id,'_', icgc_donor_id,'_',icgc_specimen_id,'_',icgc_sample_id) as mega_id, "
qry += "count(*) as c from %s group by mega_id having c>1 " % table
ret = search_db(cursor,qry)
if not ret:
print("\tno duplicates found in", table)
continue
print("\t%s has %d duplicates" % (table, len(ret)))
#print(ret)
processes = parallelize(number_of_chunks, remove_duplicates, ret, [table])
if processes: wait_join(processes)
cursor.close()
db.close()
#########################################
if __name__ == '__main__':
main()