#!/usr/bin/env python

import sys, csv

def extract_triplets(hgvslist):
    def first_sw_or_none(hgvslist,sw):
        l = [ x for x in hgvslist if x.startswith(sw) ]
        return l[0] if len(l) > 0 else None

    g = first_sw_or_none(hgvslist,'NC_')
    for tx,pro in [ ('NM_001277115.1','NP_001264044.1'), ('NM_003777.3','NP_003768.2') ]:
        c = first_sw_or_none(hgvslist,tx)
        p = first_sw_or_none(hgvslist,pro)
        yield g,c,p

out = csv.DictWriter(sys.stdout,
                     fieldnames=['rsid','HGVSg','HGVSc','HGVSp'],
                     delimiter='\t')
out.writeheader()

for line in sys.stdin:
    rec = line.strip().split('\t')
    for g,c,p in extract_triplets(rec[6].split(',')):
        if c is None:
            continue
        print('\t'.join(['rs'+rec[0],g,c,p or '']))

## <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>
