3
ëyû^Š  ã               @   s|   d dl mZmZmZmZ d dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlZd dlZd dlZejeƒZG dd„ dƒZdS )é    )Úabsolute_importÚdivisionÚprint_functionÚunicode_literalsNc               @   sl   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ ZdS )ÚTGraphc             C   s   t ƒ | _|| _d S )N)ÚdictÚ
node_cacheÚgene_id)Úselfr	   © r   úJ/broad/hptmp/bhaas/trinityrnaseq/Analysis/SuperTranscripts/pylib/TGraph.pyÚ__init__   s    zTGraph.__init__c             C   s¨   t jdj||ƒƒ t|ƒdkr&tdƒ‚|| jkr†| j| }|j|ƒ |j|kr€dj| jƒ |ƒdj|j|ƒ }t j	|ƒ t|ƒ‚q¤|S nt
j
| |||ƒ}|| j|< |S dS )zÐ
        Instantiates Node objects, and stores them in a graph.

        *** use this method for instantiating all Node objects ***

        use clear_node_cache() to clear the graph
                
        z{}	{}r   z6Error, non-zero length node_seq required for parameterz:ERROR: have conflicting node sequences for {} node_id: {}
z{}
 vs. 
{}N)ÚloggerÚdebugÚformatÚlenÚRuntimeErrorr   Zadd_transcriptsÚseqÚget_gene_idÚcriticalÚTNode)r
   Ztranscript_idZloc_node_idÚnode_seqZnode_objÚerrmsgr   r   r   Úget_node   s    







zTGraph.get_nodec             C   s   t | jjƒ ƒS )N)Úlistr   Úvalues)r
   r   r   r   Úget_all_nodesA   s    zTGraph.get_all_nodesc             C   s   | j jƒ  dS )z"
        clears the graph
        N)r   Úclear)r
   r   r   r   Úclear_node_cacheD   s    zTGraph.clear_node_cachec             C   s   x| j ƒ D ]}|jƒ  q
W dS )z@
        clear the touch settings for each of the nodes
        N)r   Zclear_touch)r
   Únoder   r   r   Úclear_touch_settingsJ   s    zTGraph.clear_touch_settingsc             C   s4   x.|D ]&}x |D ]}|j |ƒ |j|ƒ qW qW d S )N)Zadd_next_nodeZadd_prev_node)r
   Úfrom_nodes_listÚto_nodes_listÚ	from_nodeÚto_noder   r   r   Ú	add_edgesT   s    


zTGraph.add_edgesc             C   s4   x.|D ]&}x |D ]}|j |ƒ |j|ƒ qW qW d S )N)Zremove_next_nodeZremove_prev_node)r
   r!   r"   r#   r$   r   r   r   Úprune_edges[   s    


zTGraph.prune_edgesc             C   sN   t jdj|ƒƒ | j|jƒ |gƒ | j|g|jƒ ƒ d|_| jj|j	ƒ ƒ d S )Nzpruning node: {}T)
r   r   r   r&   Zget_prev_nodesÚget_next_nodesZdeadr   ÚpopZ
get_loc_id)r
   r   r   r   r   Ú
prune_nodec   s
    zTGraph.prune_nodec             C   s   || j kr| j | S dS dS )zp
        does not instantiate, only retrieves.
        If loc_node_id is not in the graph, returns None
        N)r   )r
   Únode_idr   r   r   Úretrieve_nodek   s    

zTGraph.retrieve_nodec             C   s   | j S )N)r	   )r
   r   r   r   r   w   s    zTGraph.get_gene_idc             C   sô   t jdj|ƒƒ t|dƒ}|jdƒ x¸| jD ]®}| j| }|jƒ }|jƒ }|jƒ }t	|ƒ}d}	t
|	d ƒ}
||	kr’|d|
… d |||
 |…  }|jdj|jƒ |||jƒ |ƒƒ x&|D ]}|jd	j|jƒ |jƒ ƒƒ q¸W q,W |jd
ƒ |jƒ  d S )Nzdrawing graph: {}Úwzdigraph G {
é2   é   r   z...z{} [label="{}:Len{}:T{}:{}"]
z{}->{}
z}
)r   r   r   ÚopenÚwriter   Zget_seqZget_gene_node_idr'   r   ÚintZget_idZget_topological_orderÚclose)r
   ÚfilenameÚofhr*   r   r   Zgene_node_idZ
next_nodesZnode_seq_lenZmax_len_showZmax_len_show_halfZ	next_noder   r   r   Ú
draw_graph{   s&    


 
"
zTGraph.draw_graphc             C   s*   d}x | j ƒ D ]}||jƒ d 7 }qW |S )NÚ Ú
)r   ZtoString)r
   Ztxtr   r   r   r   Ú__repr__™   s    zTGraph.__repr__N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r    r%   r&   r)   r+   r   r5   r8   r   r   r   r   r      s   %
r   )Ú
__future__r   r   r   r   ÚosÚsysÚreÚloggingÚargparseÚcollectionsÚnumpyÚtimer   Ú	getLoggerr9   r   r   r   r   r   r   Ú<module>   s   
