#!/usr/bin/env python

import collections
import psycopg2, psycopg2.extras


if __name__ == '__main__':
    sel_sql = """
SELECT G.gene,GE.ac,G.chr,G.strand,
  format('(%s,%s)',T.cds_start_i,T.cds_end_i) as cds_se,
  array_to_string(array_agg(format('(%s,%s)',GE.start_i,GE.end_i) order by G.strand*GE.ord),';') as "exon_ses"
FROM transcripts.genomic_exon GE
JOIN transcripts.transcript T on GE.ac=T.ac
JOIN transcripts.gene G on T.gene=G.gene
WHERE T.ac in ('NM_144670.4','NM_017436.4')
GROUP by G.gene,GE.ac,G.chr,G.strand,cds_se
"""
    conn = psycopg2.connect("host=localhost dbname=reece")
    sel_cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
    sel_cur.execute(sel_sql)
    for row in sel_cur.fetchall():
        print( '\t'.join([row['ac'], row['chr'], str(row['strand']), row['cds_se'],
                          str(len(row['exon_ses'].split(';'))), row['exon_ses']]) )

## <LICENSE>
## Copyright 2014 UTA Contributors (https://bitbucket.org/invitae/uta)
## 
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
## 
##     http://www.apache.org/licenses/LICENSE-2.0
## 
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
## </LICENSE>
