#!/usr/bin/env python3

import birchenv
import birchscript
import os
import os.path
import subprocess
import sys
import re

'''
Given a set of GI or ACCESSION numbers , create a file containing
the corresponding genbank entries

  $1 file containing LOCUS names or ACCESSION numbers
  $2 file to contain all of the GenBank entries
'''


JOBID     = os.getpid()
h_INFILE  = open(sys.argv[1], "r")

 
#If input file contains characters other than [0-9], we assume
#that the input is ACCESSION numbers. By default, we assume
#the input is GI numbers.
InFormat='uid'
SPATTERN=re.compile('[A-Z]',re.IGNORECASE)
for line in h_INFILE :
    #print line
    if re.search(SPATTERN,line) :
       InFormat='accn'
h_INFILE.close()

# Run epost to generate a history file at NCBI, containing links to the
# specified entries.
h_INFILE  = open(sys.argv[1], "r")
EFILE=str(JOBID) +'.epost'
h_EPOSTTMP = open(EFILE, "w")
p = subprocess.Popen(['perl', os.path.join(birchenv.BIRCH, 'bin-' + birchenv.BIRCH_PLATFORM , 'epost'), '-db', 'nuccore' , '-format', InFormat  ], stdin=h_INFILE, stdout=h_EPOSTTMP)
p.wait()
h_INFILE.close()

# Run efetch to retrieve the entries
h_EFILE  = open(EFILE, "r")
h_OUTFILE = open(sys.argv[2], "w")
p = subprocess.Popen(['perl', os.path.join(birchenv.BIRCH, 'bin-' + birchenv.BIRCH_PLATFORM , 'efetch'), '-db', 'nuccore' , '-format', 'gb'  ], stdin=h_EFILE, stdout=h_OUTFILE)
p.wait()
h_EFILE.close()
h_OUTFILE.close()
os.remove(EFILE)






