0
|
1 #!/usr/bin/env python
|
2
|
2
|
0
|
3
|
|
4 import argparse
|
|
5 import sys
|
|
6
|
3
|
7 import psycopg2
|
|
8
|
|
9 from sqlalchemy import MetaData
|
0
|
10 from sqlalchemy import create_engine
|
|
11 from sqlalchemy.engine.url import make_url
|
|
12
|
|
13 metadata = MetaData()
|
|
14
|
|
15 SKIP_VALS = ['#CHROM', 'POS', 'ID', 'REF', 'ALT', 'QUAL', 'FILTER', 'INFO', 'FORMAT']
|
|
16
|
|
17
|
|
18 class EnsureSynced(object):
|
|
19 def __init__(self):
|
|
20 self.args = None
|
|
21 self.conn = None
|
|
22 self.parse_args()
|
|
23 self.outfh = open(self.args.output, "w")
|
|
24 self.connect_db()
|
|
25 self.engine = create_engine(self.args.database_connection_string)
|
|
26 self.metadata = MetaData(self.engine)
|
|
27 self.affy_ids_from_db = []
|
|
28 self.affy_ids_from_file = []
|
|
29
|
|
30 def connect_db(self):
|
|
31 url = make_url(self.args.database_connection_string)
|
|
32 args = url.translate_connect_args(username='user')
|
|
33 args.update(url.query)
|
|
34 assert url.get_dialect().name == 'postgresql', 'This script can only be used with PostgreSQL.'
|
|
35 self.conn = psycopg2.connect(**args)
|
|
36
|
|
37 def get_affy_ids_from_db(self):
|
6
|
38 cmd = "SELECT coral_mlg_rep_sample_id, coral_mlg_clonal_id FROM genotype WHERE coral_mlg_rep_sample_id IS NOT NULL AND coral_mlg_rep_sample_id != '' AND coral_mlg_clonal_id != 'failed' ORDER BY coral_mlg_rep_sample_id;”
|
0
|
39 cur = self.conn.cursor()
|
|
40 cur.execute(cmd)
|
|
41 rows = cur.fetchall()
|
|
42 for row in rows:
|
|
43 self.affy_ids_from_db.append(row[0])
|
4
|
44 self.affy_ids_from_db.sort()
|
0
|
45
|
|
46 def get_affy_ids_from_file(self, f):
|
|
47 with open(f) as fh:
|
|
48 for line in fh:
|
|
49 line = line.strip()
|
|
50 if line in SKIP_VALS:
|
|
51 # Skip the first 9 lines in the file.
|
|
52 continue
|
|
53 self.affy_ids_from_file.append(line)
|
|
54 self.affy_ids_from_file.sort()
|
|
55
|
|
56 def get_difference(self, list1, list2):
|
|
57 if len(list1) > len(list2):
|
|
58 return list(set(list1) - set(list2))
|
|
59 return list(set(list2) - set(list1))
|
|
60
|
|
61 def log(self, msg):
|
|
62 self.outfh.write("%s\n" % msg)
|
|
63
|
|
64 def parse_args(self):
|
|
65 parser = argparse.ArgumentParser()
|
|
66 parser.add_argument('--database_connection_string', dest='database_connection_string', help='Postgres database connection string'),
|
|
67 parser.add_argument('--affy_ids_from_file', dest='affy_ids_from_file', help='Affy ids taken from all previously genotyped samples vcf file')
|
|
68 parser.add_argument('--output', dest='output', help='Output dataset'),
|
|
69 self.args = parser.parse_args()
|
|
70
|
|
71 def run(self):
|
|
72 self.get_affy_ids_from_db()
|
|
73 self.get_affy_ids_from_file(self.args.affy_ids_from_file)
|
|
74 if self.affy_ids_from_db == self.affy_ids_from_file:
|
|
75 in_sync = True
|
|
76 self.log("The selected file is in sync with the database.\n\n")
|
|
77 else:
|
|
78 in_sync = False
|
|
79 self.log("The selected file is not in sync with the database.\n\n")
|
|
80 num_affy_ids_from_db = len(self.affy_ids_from_db)
|
|
81 self.log("Number of Affymetrix ids in the database: %d\n" % num_affy_ids_from_db)
|
|
82 num_affy_ids_from_file = len(self.affy_ids_from_file)
|
|
83 self.log("Number of Affymetrix ids in the file: %d\n" % num_affy_ids_from_file)
|
|
84 if not in_sync:
|
|
85 if num_affy_ids_from_db > num_affy_ids_from_file:
|
|
86 self.log("The database contains the following Affymetrix ids that are not in the file.\n")
|
|
87 else:
|
|
88 self.log("The file contains the following Affymetrix ids that are not in the database.\n")
|
|
89 diff_list = self.get_difference(self.affy_ids_from_db, self.affy_ids_from_file)
|
|
90 for affy_id in diff_list:
|
|
91 self.log("%s\n" % affy_id)
|
|
92 self.outfh.flush()
|
|
93 self.outfh.close()
|
|
94 sys.exit(1)
|
|
95
|
|
96 def shutdown(self):
|
|
97 self.outfh.flush()
|
|
98 self.outfh.close()
|
|
99 self.conn.close()
|
|
100
|
|
101
|
|
102 if __name__ == '__main__':
|
|
103 es = EnsureSynced()
|
|
104 es.run()
|
|
105 es.shutdown()
|