annotate unique_mlg_id.py @ 0:c39c543605a2 draft

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