#!/usr/bin/python

# ----------------------------------------------------------------------
# Copyright (2010) Aram Davtyan and Garegin Papoian

# Papoian's Group, University of Maryland at Collage Park
# http://papoian.chem.umd.edu/

# Last Update: 03/04/2011
# ----------------------------------------------------------------------

import sys, os
import numpy
from Bio.SVDSuperimposer import SVDSuperimposer
from VectorAlgebra import *

#from Bio.PDB.PDBParser import PDBParser

atom_type = {'1' : 'C', '2' : 'N', '3' : 'O', '4' : 'C', '5' : 'H', '6' : 'C'}
atom_desc = {'1' : 'C-Alpha', '2' : 'N', '3' : 'O', '4' : 'C-Beta', '5' : 'H-Beta', '6' : 'C-Prime'}
PDB_type = {'1' : 'CA', '2' : 'N', '3' : 'O', '4' : 'CB', '5' : 'HB', '6' : 'C' }

class PDB_Atom:
	no = 0
	ty = ''
	res = 'UNK'
	res_no = 0
	x = 0.0
	y = 0.0
	z = 0.0
	atm = 'C'

	def __init__(self, no, ty, res, res_no, x, y, z, atm):
		self.no = no
		self.ty = ty
		self.res = res
		self.res_no = res_no
		self.x = x
		self.y = y
		self.z = z
		self.atm = atm

	def write_(self, f):
		f.write('ATOM')
		f.write(('       '+str(self.no))[-7:])
		f.write('  ')
		f.write((self.ty+'    ')[:4])
		f.write(self.res)
		f.write(' ')
		f.write('T')
		f.write(('    '+str(self.res_no))[-4:])
		f.write(('            '+str(round(self.x,3)))[-12:])
		f.write(('        '+str(round(self.y,3)))[-8:])
		f.write(('        '+str(round(self.z,3)))[-8:])
		f.write('  1.00')
		f.write('  0.00')
		f.write(('            '+self.atm)[-12:]+'  ')
		f.write('\n')

class Atom:
	No = 0
	ty = ''
	x = 0.0
	y = 0.0
	z = 0.0
	desc = ''

	def __init__(self, No, ty, No_m, x, y, z, desc=''):
		self.No = No
		self.ty = ty
		self.No_m = No_m
		self.x = x
		self.y = y
		self.z = z
		self.desc = desc

	def write_(self, f):
		f.write(str(self.No))
		f.write(' ')
		f.write(PDB_type[self.No_m])
		f.write(' ')
		f.write(str(round(self.x,8)))
		f.write(' ')
		f.write(str(round(self.y,8)))
		f.write(' ')
		f.write(str(round(self.z,8)))
		f.write(' ')
		f.write(self.desc)
		f.write('\n')

if len(sys.argv)!=7 and len(sys.argv)!=8 :
	print "# of arguments", len(sys.argv)
	print "\nCalcQValue.py PDB_Id Frag_PDB_path FragPDB+chain i_start j_start length (1 for rmsd calculation) \n"
	exit()

struct_id = sys.argv[1]
if struct_id[-4:].lower()==".pdb":
	pdb_file = struct_id
else:
	pdb_file = struct_id + ".pdb"

#myhome = os.environ.get("HOME")
#pdbDir = myhome + "/opt/script/PDBs/"
Frag_struct_id = sys.argv[3]

if len(Frag_struct_id) < 5:
  print "\nCalcQValue.py PDB_Id Frag_PDB_path FragPDB+chain i_start j_start length\n"
  print "format of FragPDB+chain, like 1r69a "
  exit()

pdbDir = sys.argv[2]
Frag_pdbID = Frag_struct_id[0:4].upper()
Frag_chain_name = Frag_struct_id[4].upper()
Frag_pdb_file = pdbDir + Frag_pdbID + ".pdb"

#output_file = ""
#if len(sys.argv)>3: output_file = sys.argv[3]

sigma_exp = 0.15
res_flag = 0

res_flag  = 1
res_Start = int(sys.argv[4])
res_len   = int(sys.argv[6])
rmsd_flag = 0
if len(sys.argv) == 8 :
  rmsd_flag = int(sys.argv[7])
  if rmsd_flag != 1 :
  	print "rmsd_flag has to be 1 !!"
	exit()

#res_End = res_Start + res_len - 1
Frag_Start = int(sys.argv[5])
#Frag_End   = res_len + Frag_Start - 1
#print "res_Start:", res_Start, "Frag_Start:", Frag_Start

n_atoms = 0