#=============================================================================================
# model.py
#
# Use MODELLER to complete PDB files that are missing atoms and residues.
#
# Written 2007-05-20 by
# Gregory R. Bowman <gbowman@stanford.edu>
# Biophysics Program, Stanford University
# Pande group
#
# Modifications 2012-08-08 by
# John D. Chodera <jchodera@berkeley.edu>
# California Institute for Quantitative Biosciences (QB3)
# University of California, Berkeley
#=============================================================================================
# TODO:
# * Add the ability to fetch a PDB file from the RCSB for source files and/or templates.
#=============================================================================================
# CHANGELOG:
#=============================================================================================


#=============================================================================================
# GLOBAL IMPORTS
#=============================================================================================

import os
import os.path
import shutil
import tempfile

import modeller

#=============================================================================================
# SUBROUTINES
#=============================================================================================

one_letter_code = { 'ALA' : 'A',
                    'ASN' : 'N',
                    'ASP' : 'D',
                    'ARG' : 'R',
                    'CYS' : 'C',
                    'GLN' : 'Q',
                    'GLU' : 'E',               
                    'GLY' : 'G',
                    'HIS' : 'H',                
                    'ILE' : 'I',
                    'LEU' : 'L',
                    'NLE' : 'n',
                    'LYS' : 'K',
                    'MET' : 'M',
                    'PRO' : 'P',
                    'PHE' : 'F',
                    'SER' : 'S',
                    'THR' : 'T',
                    'TRP' : 'W',
                    'TYR' : 'Y',
                    'VAL' : 'V',
                    'UNK' : 'X' }

three_letter_code = dict()
for key in one_letter_code.keys():
    value = one_letter_code[key]
    three_letter_code[value] = key

def read_file(filename):
    infile = open(filename, 'r')
    lines = infile.readlines()
    infile.close()
    return lines

def write_file(filename, contents):
    outfile = open(filename, 'w')
    if type(contents) == list:
        for line in contents:
            outfile.write(line)
            if line[-1] != '\n':
                outfile.write('\n')
    else:
        outfile.write(contents)
    outfile.close()
    return

def generate_seqres(sequence, chainid):
    sequence_length = len(sequence)
    line_index = 1
    sequence_index = 0
    while (sequence_index < sequence_length):
        print "SEQRES%4d %c %4d " % (line_index, chainid, sequence_length),
        line_count = 0
        while (line_count < 13):
            print "%3s" % three_letter_code[sequence[sequence_index]],
            sequence_index += 1
            line_count += 1
        print ""
        line_index += 1

        
        
#=============================================================================================
# MAIN
#=============================================================================================

if __name__ == '__main__':
    import doctest
    doctest.testmod()
    
    # Generate SEQRES cards.

    sequence = "GHMQTQGLAKDAWEIPRESLRLEVKLGQGCFGEVWMGTWNGTTRVAIKTLKPGTMSPEAFLQEAQVMKKLRHEKLVQLYAVVSEEPIYIVTEYMSKGSLLDFLKGETGKYLRLPQLVDMAAQIASGMAYVERMNYVHRDLRAANILVGENLVCKVADFGLARLIEDNEYTARQGAKFPIKWTAPEAALYGRFTIKSDVWSFGILLTELTTKGRVPYPGMVNREVLDQVERGYRMPCPPECPESLHDLMCQCWRKEPEERPTFEYLQAFLEDYFTSTEPQYQPGENL"

    generate_seqres(sequence, 'A')
    generate_seqres(sequence, 'B')

