"""
     bin/example.py: CCP4MG Molecular Graphics Program
     Copyright (C) 2001-2008 University of York, CCLRC

     This library is free software: you can redistribute it and/or
     modify it under the terms of the GNU Lesser General Public License
     version 3, modified in accordance with the provisions of the 
     license to address the requirements of UK law.
 
     You should have received a copy of the modified GNU Lesser General 
     Public License along with this library.  If not, copies may be 
     downloaded from http://www.ccp4.ac.uk/ccp4license.php
 
     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU Lesser General Public License for more details.
"""


import threading
import sys, os
sys.path.append("lib")
sys.path.append("python/ui")
sys.path.append("qtgui")
import xmapview
import Queue
from build import *
from eventdefs import *
from mmdb import *
from mgapp import *
from mmut import *
import COLOUR
from point_funcs import *
from resources import *
import getopt

print sys.argv, len(sys.argv)
if len(sys.argv)<2:
  print "Usage: python example.py pdb_file"
  sys.exit()

try:
  opts, args = getopt.getopt(sys.argv[2:], "m:f:p:s", ["mtz-file=","f-col=","phi-col=","draw_symmetry"])
except getopt.GetoptError:
  print "Usage: python example.py <pdb_file> [-m <mtz_file>|--mtz-file=<mtz-file>] [-f <f column>|--f-col=<f column>] [-p <phi column>|--phi-col=<phi column>] [-s|--draw_symmetry]"
  sys.exit()

mtz_file_name = None
f_col = None
phi_col = None
do_sym = 0

print "Options:", opts, args
for o, a in opts:
  if o in ("-m", "--mtz-file"):
    mtz_file_name = a
  if o in ("-f", "--f-col"):
    f_col = a
  if o in ("-p", "--phi-col"):
    phi_col = a
  if o in ("-s", "--draw_symmetry"):
    do_sym = 1

clipper_field = None
if mtz_file_name:
  if f_col == None:
    import headers
    head = headers.header_info(mtz_file_name)
    f_col = head.GetFColLabel(0)
  if phi_col == None:
    import headers
    head = headers.header_info(mtz_file_name)
    phi_col = head.GetPhiColLabel(0)
  print "Loading mtz file: ", mtz_file_name, f_col, phi_col
  clipper_field = xmapview.XMap(mtz_file_name, f_col, phi_col)

print sys.argv[1]
r = CResources()
if r.load_resources(): sys.exit()

print "Resources?",r.CMGSBase,r.CMolBondParams
MMDBManager1 = CMMANManager(r.CMGSBase,r.CMolBondParams)
InitMatType()
MMDBManager1.SetFlag(MMDBF_AutoSerials)

RC = MMDBManager1.ReadCoorFile(sys.argv[1])
if RC > 0:
  print "Problem opening PDB file"
  sys.exit()

MMDBManager1.GetMolBonds()
secstr = CSecStructure(MMDBManager1)

selHnd = MMDBManager1.NewSelection()
selHnd2 = MMDBManager1.NewSelection()
MMDBManager1.SelectAtoms(selHnd2, 0,"*",ANY_RES,"*",ANY_RES,"*","*","CA","C","*",SKEY_OR )
MMDBManager1.SelectAtoms(selHnd, 0,"A",2,"*",2,"*","PRO","*","*","*",SKEY_OR )
MMDBManager1.SelectAtoms(selHnd, 0,"*",ANY_RES,"*",ANY_RES,"*","*","*","*","*",SKEY_OR )

SelAtoms = newPPCAtom()
SelAtoms2 = newPPCAtom()

print "SelAtoms",SelAtoms
print "SelAtoms2",SelAtoms2

nSelAtoms = MMDBManager1.GetSelIndex (selHnd,SelAtoms)
nSelAtoms2 = MMDBManager1.GetSelIndex (selHnd2,SelAtoms2)

print "nSelAtoms",nSelAtoms
print "nSelAtoms2",nSelAtoms2
atmPtr = CAtomPtr(getPCAtom(SelAtoms2,0))
print atmPtr.name
res = atmPtr.GetResidue()
resAtoms = newPPCAtom()
p = intp()
res.GetAtomTable(resAtoms,p)
print p.value()

CMolBondParams = CMolBondParams(r.CMGSBase)

com = MMDBManager1.CentreOfMass(selHnd)
print "CentreOfMass",com

tmp = doublea(1)
com_py=tmp.frompointer(com)

origin = (com_py[0], com_py[1],com_py[2])

import mapview
import field



CHBond = CHBond(MMDBManager1,selHnd)
CHBond.Calculate()


import pygl_coord
tree = pygl_coord.Tree()

conn = Connectivity()

conn.AddBonds(MMDBManager1,selHnd,SelAtoms,nSelAtoms,selHnd)

nats = conn.GetNumberOfAtoms()

ats = pygl_coord.CartesianVectorPtr(PPCAtomsToCartesians(nats,conn.GetAtoms()))
conn_lists = pygl_coord.IntIntVectorPtr(conn.GetConnectivityLists())
ext_conn_lists = pygl_coord.IntVectorPtr(conn.GetExternalConnectivityLists())

print ats, conn_lists, ext_conn_lists

import pygl_coord

tree = pygl_coord.Tree()

tree.SetCoords(ats,0,conn_lists)

tree.Print()

source = MMDBManager1.Source()
print source

title = MMDBManager1.StructureTitle()
print title
