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mZ d dlZejeZdZG dd dZdS )    )absolute_importdivisionprint_functionunicode_literalsN)Topological_sortg?c               @   s\   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S )Compact_graph_wholec             C   s   d S )N )selfr   r   W/broad/hptmp/bhaas/trinityrnaseq/Analysis/SuperTranscripts/pylib/Compact_graph_whole.py__init__   s    zCompact_graph_whole.__init__c             C   s   t j|j }x|D ]}|j r"qtjdj|j  t|j	 }t
|dkr|d }t
|j dkr~tjdj||j  qtjdj|| |j|j  |j|j |j   |j|j	 |g |j| qW d S )Nzcompact_unbranched( {} )   r   z8prev node: {} id downward branched, so can't compact. {}z$linear compaction of nodes {} and {})r   topologically_sortget_all_nodesis_deadloggerdebugformattoStringlistget_prev_nodeslenget_next_nodesadd_transcriptsget_transcriptsset_seqget_seq	add_edges
prune_node)r	   tgraphZ	all_nodesnode
prev_nodes	prev_noder   r   r
   compact_unbranched   s     
z&Compact_graph_whole.compact_unbranchedc       
      C   s\  d}d}xL|rV|d7 }t jdj|| |j  d}tj|j }tjrrdj||}|j	| t jdj| x|D ]}|j
 dkrqxt jdj||j  |j }t|dkr| j|r| j||r| j||rd}|j }	t|	dkrx| j|	rx| j||	rx| j|	|rxd}qxW |rtjrJd	j||}|j	| t jdj| | j| qW d S )
NTr   r   z)
	### Num allowed variants: {}, Round: {}Fz8ladeda.compacted_whole.BEGIN.num_allowed_{}.round_{}.dotz-wrote dot: {}z5

///////// R[{}]  Exploring compaction at node: {}

z6ladeda.compacted_whole.END.num_allowed_{}.round_{}.dot)r   r   r   clear_touch_settingsr   r   r   TGLOBALSDEBUG
draw_graphget_touched_valr   r   r   	untouched$all_have_lower_topological_orderingscompact_upwardr   %all_have_higher_topological_orderingscompact_downwardr"   )
r	   r   num_allowed_variantsZcompacted_flagroundZsorted_nodesZdot_filenamer   r    
next_nodesr   r   r
   compact_graph8   s<    


""
z!Compact_graph_whole.compact_graphc             C   s"   x|D ]}|j  dkrdS qW dS )Nr   FT)r'   )r	   	node_listr   r   r   r
   r(   g   s    
zCompact_graph_whole.untouchedc             C   s:   |j  }|dk rtdx|D ]}|j  |krdS qW dS )Nr   z*Error, must run topological ordering firstFT)get_topological_orderRuntimeError)r	   r   other_nodes_list
topo_order
other_noder   r   r
   r)   o   s    
z8Compact_graph_whole.all_have_lower_topological_orderingsc             C   s:   |j  }|dk rtdx|D ]}|j  |k rdS qW dS )Nr   z*Error, must run topological ordering firstFT)r2   r3   )r	   r   r4   r5   r6   r   r   r
   r+   z   s    
z9Compact_graph_whole.all_have_higher_topological_orderingsc             C   sp   t jdj|| t|}xPtdt|d D ]:}x4t|d t|D ]}| j|| || |rFdS qFW q.W dS )Nz+compact_upward: {} max_allowed_variants: {}r   r   TF)r   r   r   r   ranger   compact_upward_node_pair)r	   r    r-   ijr   r   r
   r*      s    z"Compact_graph_whole.compact_upwardc             C   s0  t jdj||| |j|s(|j|r>t jdj|| dS t jdj|| t|j t|j k rr|| }}|j }|j }t|}|d d d }|d d d }d}	x*td|D ]}
||
 ||
 kr|	d7 }	qW |	|krt jdj|	||| dS |	| tkr"t jdj|	|t|| dS |j }t jd	j|j	  t jd
j|j	  |j
|j  |j|g|j  |j|g|j  |j  |j  t||krt|| }|d| }|j| |j|g|g n|j|j |g |j| t jdj|j	  t jdj|j	  t jdj|| dS )Nz9compact_upward_node_pair({}, {}, num_allowed_variants: {}z6-not parallel paths. excluding compaction of {} and {}Fz-not ancestral: {}, {}r   r   z.num_mm: {} exceeds {} for node pair: {} and {}zEnum_mm: {} / len: {} exceeds max mm rate: {} for node pair: {} and {}znode_A before mods: {}znode_B before mods: {}znode_A after mods: {}znode_B after mods: {}z%

	*** compacted nodes: {} and {} ***Tr;   )r   r   r   is_ancestralr   r   r7   MAX_MM_RATE	get_graphr   r   r   r   r   prune_edgestouchr   r   r   )r	   node_Anode_Br-   seqAseqBshorter_lenZseqA_revZseqB_revnum_mmr9   r   deltar   r   r
   r8      sT    


z,Compact_graph_whole.compact_upward_node_pairc             C   sp   t jdj|| t|}xPtdt|d D ]:}x4t|d t|D ]}| j|| || |rFdS qFW q.W dS )Nz-compact_downward: {} max_allowed_variants: {}r   r   TF)r   r   r   r   r7   r   compact_downward_node_pair)r	   r/   r-   r9   r:   r   r   r
   r,      s    z$Compact_graph_whole.compact_downwardc       
      C   s  |j |s|j |r*tjdj|| dS t|j t|j k rL|| }}|j }|j }t|}d}x*td|D ]}|| || krt|d7 }qtW ||krtjdj|||| dS || tkrtjdj||t|| dS |j }	tjdj|j	  tjdj|j	  |j
|j  |	j|j |g |	j|j |g |j  |j  t||kr||d  }|j| |	j|g|g n|	j|g|j  |	j| tjd	j|j	  tjd
j|j	  tjdj|| dS )Nz6-not parallel paths. excluding compaction of {} and {}Fr   r   z.num_mm: {} exceeds {} for node pair: {} and {}zEnum_mm: {} / len: {} exceeds max mm rate: {} for node pair: {} and {}znode_A before mods: {}znode_B before mods: {}znode_A after mods: {}znode_B after mods: {}z%

	*** compacted nodes: {} and {} ***T)is_descendantr   r   r   r   r   r7   r=   r>   r   r   r   r   r   r?   r@   r   r   r   )
r	   rA   rB   r-   rC   rD   rE   rF   r9   r   r   r   r
   rH      sJ    


z.Compact_graph_whole.compact_downward_node_pairN)__name__
__module____qualname__r   r"   r0   r(   r)   r+   r*   r8   r,   rH   r   r   r   r
   r      s   /Wr   )
__future__r   r   r   r   ossysreloggingTGraphTNodeNode_alignmentr   r$   	getLoggerrJ   r   r=   r   r   r   r   r
   <module>   s   
