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):
|
|
38 cmd = "SELECT affy_id FROM sample WHERE genotype_id NOT IN (SELECT id FROM genotype WHERE coral_mlg_clonal_id = 'failed') ORDER BY affy_id;"
|
|
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])
|
|
44
|
|
45 def get_affy_ids_from_file(self, f):
|
|
46 with open(f) as fh:
|
|
47 for line in fh:
|
|
48 line = line.strip()
|
|
49 if line in SKIP_VALS:
|
|
50 # Skip the first 9 lines in the file.
|
|
51 continue
|
|
52 self.affy_ids_from_file.append(line)
|
|
53 self.affy_ids_from_file.sort()
|
|
54
|
|
55 def get_difference(self, list1, list2):
|
|
56 if len(list1) > len(list2):
|
|
57 return list(set(list1) - set(list2))
|
|
58 return list(set(list2) - set(list1))
|
|
59
|
|
60 def log(self, msg):
|
|
61 self.outfh.write("%s\n" % msg)
|
|
62
|
|
63 def parse_args(self):
|
|
64 parser = argparse.ArgumentParser()
|
|
65 parser.add_argument('--database_connection_string', dest='database_connection_string', help='Postgres database connection string'),
|
|
66 parser.add_argument('--affy_ids_from_file', dest='affy_ids_from_file', help='Affy ids taken from all previously genotyped samples vcf file')
|
|
67 parser.add_argument('--output', dest='output', help='Output dataset'),
|
|
68 self.args = parser.parse_args()
|
|
69
|
|
70 def run(self):
|
|
71 self.get_affy_ids_from_db()
|
|
72 self.get_affy_ids_from_file(self.args.affy_ids_from_file)
|
|
73 if self.affy_ids_from_db == self.affy_ids_from_file:
|
|
74 in_sync = True
|
|
75 self.log("The selected file is in sync with the database.\n\n")
|
|
76 else:
|
|
77 in_sync = False
|
|
78 self.log("The selected file is not in sync with the database.\n\n")
|
|
79 num_affy_ids_from_db = len(self.affy_ids_from_db)
|
|
80 self.log("Number of Affymetrix ids in the database: %d\n" % num_affy_ids_from_db)
|
|
81 num_affy_ids_from_file = len(self.affy_ids_from_file)
|
|
82 self.log("Number of Affymetrix ids in the file: %d\n" % num_affy_ids_from_file)
|
|
83 if not in_sync:
|
|
84 if num_affy_ids_from_db > num_affy_ids_from_file:
|
|
85 self.log("The database contains the following Affymetrix ids that are not in the file.\n")
|
|
86 else:
|
|
87 self.log("The file contains the following Affymetrix ids that are not in the database.\n")
|
|
88 diff_list = self.get_difference(self.affy_ids_from_db, self.affy_ids_from_file)
|
|
89 for affy_id in diff_list:
|
|
90 self.log("%s\n" % affy_id)
|
|
91 self.outfh.flush()
|
|
92 self.outfh.close()
|
|
93 sys.exit(1)
|
|
94
|
|
95 def shutdown(self):
|
|
96 self.outfh.flush()
|
|
97 self.outfh.close()
|
|
98 self.conn.close()
|
|
99
|
|
100
|
|
101 if __name__ == '__main__':
|
|
102 es = EnsureSynced()
|
|
103 es.run()
|
|
104 es.shutdown()
|