B
    b2                 @   s   d Z ddlmZ ddlmZmZ ddlmZmZ ddl	m
Z
 ddlmZmZmZ ddlmZ ddlmZmZ dd	lmZmZ G d
d deZeeG dd de
ZG dd deZdd ZdddZdS )zE
This module contains classes for reading various TINKER-style files
    )TinkerError)FileFormatType	load_file)element_by_name	AtomicNum)	Structure)AtomBondResidue)genopen)add_metaclassstring_types)rangezipc               @   s,   e Zd ZdZeeeeeeedZdd ZdS )KeywordControlFilezC Reads and processes a keyword control file for TINKER simulations )
PARAMETERSzA-AXISzB-AXISzC-AXISALPHABETAGAMMAc          	   C   s   d d d d d d d d| _ xt|dD ]}| s0q"| }|d  }||d d }y| j| || j |< W q" tk
r   Y q" tk
r   t	d|| j| f Y q"X q"W d S )N)r   zA-AXISzB-AXISzC-AXISr   r   r   rr    zNMalformed keyword control file! Could not convert the value of %s into type %s)
keywordsopenstripsplitupperreplace
_datatypesKeyError
ValueErrorr   )selffnamelinewordskeyresult r&   8lib/python3.7/site-packages/parmed/tinker/tinkerfiles.py__init__   s     
 zKeywordControlFile.__init__N)__name__
__module____qualname____doc__strfloatr   r(   r&   r&   r&   r'   r      s
   r   c                   s:   e Zd ZdZedd Zedd Zd	 fdd	Z  ZS )
XyzFilea\   Reads and processes a Tinker XYZ file

    Parameters
    ----------
    fname : str or file-like
        Name of the file, or the file object containing the XYZ file contents
    seq : str, optional
        Name of the file containing the residue (and chain) sequence. Default is
        None (so every atom will be part of the same residue)
    c             C   s$  t | dk rdS yt| d  W n tk
r4   dS X yt| d  W n> tk
r   yt| d  W n tk
rz   Y nX dS Y nX dS ydd | dd D  W n tk
r   dS X yd	d | dd D  W n tk
r   Y nX dS yd
d | dd D  W n tk
r   dS X dS )a]   Checks that the tokenized line is consistent with an atom record

        Parameters
        ----------
        words : list of str
            Result of line.split() on a line of the file

        Returns
        -------
        is_atom : bool
            If it is consistent with an atom record, return True. Otherwise,
            False
           Fr      c             S   s   g | ]}t |qS r&   )r.   ).0wr&   r&   r'   
<listcomp>Z   s    z.XyzFile._check_atom_record.<locals>.<listcomp>      c             S   s   g | ]}t |qS r&   )int)r2   r3   r&   r&   r'   r4   _   s    c             S   s   g | ]}t |qS r&   )r7   )r2   r3   r&   r&   r'   r4   f   s    NT)lenr7   r   r.   )r#   r&   r&   r'   _check_atom_record7   s:     
zXyzFile._check_atom_recordc          	   C   s   t | d}|  }|sdS yt|d }W n ttfk
rF   dS X |dkrTdS |  }t|dkrydd |D  W n" tk
r   t|rdS dS X |  }t|S )a!   Identify the file as a Tinker XYZ file

        Parameters
        ----------
        filename : str
            Name of the file to test whether or not it is a mol2 file

        Returns
        -------
        is_fmt : bool
            True if it is a xyz file, False otherwise
        r   Fr   r0   c             S   s   g | ]}t |qS r&   )r.   )r2   r3   r&   r&   r'   r4      s    z%XyzFile.id_format.<locals>.<listcomp>T)	r   readliner   r7   r   
IndexErrorr8   r/   r9   )filenamefr#   natomr&   r&   r'   	id_formatk   s(    

zXyzFile.id_formatNc          	      s  t t|   t|tr(t|d}d}n|}d}|d k	r@t|}yt| 	 d }W n  t
tfk
rx   tdY nX |d k	r|t|jkrt
d| | 	 }t|dkrt|sdd	 |D | _| 	 }td
}d|_d|_|d k	r|jd }t|d |}	ntt|d  }	t|	|d |d d}
dd	 |dd D \|
_|
_|
_| |
|j|j|j|j|j  dd	 |dd  D g}xt!|D ]\}}|	 }|d k	r|j|d  j"}t|d |}	ntt|d  }	t|	|d |d d}
dd	 |dd D \|
_|
_|
_| |
|j|j|j|j|j  |#dd	 |dd  D  qW xZt$| j|D ]J\}
}|
j%d }x4|D ],}||kr| j&#t'|
| j|d   qW qjW |d krx,| jD ]"}
t|
j&dkrt|
j|
_(qW |r|)  d S )Nr   TFr   zBad XYZ file format; first linezISequence file %s # of atoms does not match the # of atoms in the XYZ filer0   c             S   s   g | ]}t |qS r&   )r.   )r2   r3   r&   r&   r'   r4      s    z$XyzFile.__init__.<locals>.<listcomp>ZSYSr1   r6   )atomic_numbernametypec             S   s   g | ]}t |qS r&   )r.   )r2   r3   r&   r&   r'   r4      s    r5   c             S   s   g | ]}t |qS r&   )r7   )r2   r3   r&   r&   r'   r4      s    c             S   s   g | ]}t |qS r&   )r.   )r2   r3   r&   r&   r'   r4      s    c             S   s   g | ]}t |qS r&   )r7   )r2   r3   r&   r&   r'   r4      s    )*superr/   r(   
isinstancer   r   r   r7   r:   r   r   r;   r   r8   atomsr9   boxr
   ZnumberZ_idxZresidues_guess_atomic_numberr   r   r   ZxxZxyZxzZadd_atomrA   chainZinsertion_codeZsegid	enumerateresidueappendr   idxbondsr	   r@   close)r    r!   seqZfxyzZown_handle_xyzZ	seqstructr>   r#   rJ   r@   ZatomZbond_idsir"   rM   rL   )	__class__r&   r'   r(      sl    



"

""


(
zXyzFile.__init__)N)	r)   r*   r+   r,   staticmethodr9   r?   r(   __classcell__r&   r&   )rQ   r'   r/   *   s   4'r/   c               @   s.   e Zd ZdZd	ddZdd Zedd ZdS )
DynFilez' Reads and processes a Tinker DYN file Nc             C   s   |d k	r|  | d S )N)read)r    r!   r&   r&   r'   r(      s    zDynFile.__init__c             C   s4  t |d}z|  dkr*td| | }t|dd  |dd   | _| _|  dkrttdd	d
 tdD | _| 	 
 }t|d dd| jd< t|d dd| jd< t|d dd| jd< | 	 
 }t|d dd| jd< t|d dd| jd< t|d dd| jd< |  dkr^tddd
 t| jD | _t|| j| j |  }|dkrPd| _dd
 t| jD | _t|| j| j |  dkrtddd
 t| jD | _t|| j| j |  dkr&tddd
 t| jD | _t|| j| j n|dkrd | _d!d
 t| jD | _t|| j| j |  d"krtd#| d$d
 t| jD | _t|| j| j |  d%krtd&| d'd
 t| jD | _t|| j| j ntd(| W d|  X dS ))z Parses the .dyn file r   zNumber of Atoms and Title :z'%s is not a recognized TINKER .dyn filer      NzPeriodic Box Dimensions :zNo periodic box dimension linec             S   s   g | ]}d qS )g        r&   )r2   rP   r&   r&   r'   r4      s    z DynFile.read.<locals>.<listcomp>r0   DEr1   r5         r6   zCurrent Atomic Positions :z No atomic positions in .dyn filec             S   s   g | ]}d d d gqS )g        r&   )r2   rP   r&   r&   r'   r4      s    z"Current Translational Velocities :Tc             S   s   g | ]}d d d gqS )g        r&   )r2   rP   r&   r&   r'   r4      s    zCurrent Angular Velocities :z4Could not find Angular velocity section in .dyn filec             S   s   g | ]}d d d gqS )g        r&   )r2   rP   r&   r&   r'   r4      s   zCurrent Angular Momenta :z3Could not find angular momenta section in .dyn filec             S   s   g | ]}d d d gqS )g        r&   )r2   rP   r&   r&   r'   r4      s   zCurrent Atomic Velocities :Fc             S   s   g | ]}d d d gqS )g        r&   )r2   rP   r&   r&   r'   r4     s    zCurrent Atomic Accelerations :z#Could not find accelerations in %s c             S   s   g | ]}d d d gqS )g        r&   )r2   rP   r&   r&   r'   r4   	  s   z Alternate Atomic Accelerations :z&Could not find old accelerations in %sc             S   s   g | ]}d d d gqS )g        r&   )r2   rP   r&   r&   r'   r4     s   zNo velocities in %s)r   r:   r   r   r7   r>   titler   rF   r   r   r.   r   Z	positionsrT   _read_sectionZ	rigidbodyZtranslational_velocitiesZangular_velocitiesZangular_momentaZ
velocitiesZaccelerationsZold_accelerationsrN   )r    r!   r=   r"   r#   r&   r&   r'   rU      sr    
*


zDynFile.readc          
   C   s   xt |D ]}|    }yXt|d dd|| d< t|d dd|| d< t|d dd|| d< W q
 ttfk
r   tdY q
X q
W dS )z
        Reads a section of an open file into the passed container. The
        container should be a 2-dimensional list of length natom x 3
        r   rW   rX   r1   r5   z$Could not parse values from dyn fileN)	r   r:   r   r   r.   r   r;   r   r   )r=   Z	containerr>   rP   r#   r&   r&   r'   r\     s     zDynFile._read_section)N)r)   r*   r+   r,   r(   rU   rR   r\   r&   r&   r&   r'   rT      s   
@rT   c             C   s&   yt |  dS  tk
r    dS X d S )NTF)r.   r   )thingr&   r&   r'   is_float&  s
    r^   Nc             C   s|   d dd | D } |dks*t|jdkrpt| dkrpyt| d  | d    S  tk
rn   tt|  S X tt|  S )z Guesses the atomic number r   c             s   s   | ]}|  r|V  qd S )N)isalpha)r2   cr&   r&   r'   	<genexpr>0  s    z'_guess_atomic_number.<locals>.<genexpr>Nr1   r   )joinr8   rE   r   r   lowerr   r   )rA   rJ   r&   r&   r'   rG   -  s    rG   )N)r,   Zparmed.exceptionsr   Zparmed.formats.registryr   r   Zparmed.periodic_tabler   r   Zparmed.structurer   Zparmed.topologyobjectsr   r	   r
   Zparmed.utils.ior   Zparmed.utils.sixr   r   Zparmed.utils.six.movesr   r   objectr   r/   rT   r^   rG   r&   r&   r&   r'   <module>   s    'U