0
|
1 #!/usr/bin/env python
|
|
2 import argparse
|
|
3 import sys
|
|
4
|
|
5 import psycopg2
|
|
6
|
1
|
7 from sqlalchemy import create_engine
|
0
|
8 from sqlalchemy.engine.url import make_url
|
|
9
|
|
10
|
|
11 class UniqueMGLIDGenerator(object):
|
|
12 def __init__(self):
|
|
13 self.args = None
|
|
14 self.conn = None
|
|
15 self.parse_args()
|
|
16 self.outfh = open(self.args.output, "w")
|
|
17 self.connect_db()
|
|
18 self.engine = create_engine(self.args.database_connection_string)
|
|
19
|
|
20 def parse_args(self):
|
|
21 parser = argparse.ArgumentParser()
|
|
22 parser.add_argument('--database_connection_string', dest='database_connection_string', help='Postgres database connection string'),
|
|
23 parser.add_argument('--output', dest='output', help='Output dataset'),
|
|
24 self.args = parser.parse_args()
|
|
25
|
|
26 def connect_db(self):
|
|
27 url = make_url(self.args.database_connection_string)
|
|
28 args = url.translate_connect_args(username='user')
|
|
29 args.update(url.query)
|
|
30 assert url.get_dialect().name == 'postgresql', 'This script can only be used with PostgreSQL.'
|
|
31 self.conn = psycopg2.connect(**args)
|
|
32
|
|
33 def run(self):
|
|
34 cmd = """
|
|
35 SELECT DISTINCT coral_mlg_rep_sample_id
|
|
36 FROM genotype
|
|
37 WHERE coral_mlg_rep_sample_id is not NULL
|
|
38 ORDER BY coral_mlg_rep_sample_id;
|
|
39 """
|
|
40 cur = self.conn.cursor()
|
|
41 cur.execute(cmd)
|
|
42 rows = cur.fetchall()
|
|
43 for tup in rows:
|
|
44 self.outfh.write("%s\n" % tup[0])
|
|
45 self.outfh.close()
|
|
46
|
|
47 def shutdown(self):
|
|
48 self.conn.close()
|
|
49
|
|
50 def stop_err(self, msg):
|
|
51 sys.stderr.write(msg)
|
|
52 self.outfh.flush()
|
|
53 self.outfh.close()
|
|
54 sys.exit(1)
|
|
55
|
|
56
|
|
57 if __name__ == '__main__':
|
|
58 umlgidg = UniqueMGLIDGenerator()
|
|
59 umlgidg.run()
|
|
60 umlgidg.shutdown()
|