
    DUf                     F    d Z ddlZddlZddlZd	dZd Zd
dZd Zd	dZdS )z4
Module to help create scaled bigWig files from BAM
    NFc                    | dz   }t           j                            |          rN|sLt          |          D ]<}|                    d          rt          |                                          }|c S ddddd| g}t          j        |t          j	        t          j	                  }|
                                \  }}|j        rt          d	|z            t          |          }t           j                            |          sIt          |d
          }	|	                    t          |          dz              |	                                 |S )z|
    Scale is cached in a bam.scale file containing the number of mapped reads.
    Use force=True to override caching.
    z.scale#samtoolsviewz-cz-F0x4stdoutstderrzsamtools says: %sw
)ospathexistsopen
startswithfloatstrip
subprocessPopenPIPEcommunicate
returncode
ValueErrorwritestrclose)
bamforcescale_fnline	readcountcmdspr	   r
   fouts
             V/var/www/html/software/conda/lib/python3.11/site-packages/pybedtools/contrib/bigwig.pymapped_read_countr&   	   s5   
 X~H	w~~h  NN 	 	Ds## djjll++IdE37DjojoNNNA]]__NFF| 7,v5666fI 7>>(## Hc""

3y>>D()))

    c                    t          j        t          j        |                    }d| j        ||g}	 t	          j        |t          j        t          j                  }|                                \  }}n# t          $ r t          d          w xY w|j	        r+t          dd                    |          d|d|          |S )NbedGraphToBigWigr   bedGraphToBigWig was not found on the path. This is an external tool from UCSC which can be downloaded from http://hgdownload.soe.ucsc.edu/admin/exe/. Alternatatively, use `conda install ucsc-bedgraphtobigwig`cmds:  
stderr:
stdout:
pybedtoolschromsizes_to_file
chromsizesfnr   r   r   r   FileNotFoundErrorr   r   join)bedgraphgenomeoutputgenome_filer"   r#   r	   r
   s           r%   bedgraph_to_bigwigr:   '   s    /
0Ef0M0MNNK[&AD	
T*/*/RRR 
 
 
4
 
 	

 	| 
j03O
 
 	
 M   AA6 6Bc                    d| g}||                     d|g           ||                     d|g           ||                     d|g           ||                     d|g           t          j                                        }|                    |           	 t          j        |t
          j        t
          j                  }|                                \  }}	n# t          $ r t          d          w xY w|j
        r+t          dd	                    |          d
|	d|          t          j        |          S )NbigWigToBedGraphz-chromz-startz-endz-udcDirr   bigWigToBedGraph was not found on the path. This is an external tool from UCSC which can be downloaded from http://hgdownload.soe.ucsc.edu/admin/exe/. Alternatatively, use `conda install ucsc-bedgraphtobigwig`r+   r,   r-   r.   )extendr0   BedTool_tmpappendr   r   r   r   r4   r   r   r5   )
r3   chromstartendudcDirr"   outfnr#   r	   r
   s
             r%   bigwig_to_bedgraphrH   <   sZ   #DXu%&&&Xu%&&&
VSM"""Y'(((##%%EKK	
T*/*/RRR 
 
 
4
 
 	

 	| 
j03O
 
 	
 e$$$s   AC   C:c                    t          j        t          j        |                    }d| j        ||g}	 t	          j        |t          j        t          j                  }|                                \  }}n# t          $ r t          d          w xY w|j	        r+t          dd                    |          d|d|          |S )NwigToBigWigr   r>   r+   r,   r-   r.   r/   )wigr7   r8   r9   r"   r#   r	   r
   s           r%   wig_to_bigwigrL   [   s    /
0Ef0M0MNNK36;7D	
T*/*/RRR 
 
 
4
 
 	

 	| 
j03O
 
 	
 Mr;   c                 b   t          j        t          j        |                    }t          dd|          }|rt	          |           }d|dz  z  }||d<    t          j        |           j        di |}d|j        ||g}		 t          j	        |	t          j
        t          j
        d          }
|
                                \  }}n# t          $ r t          d          w xY w|
j        rd	|v rt          d
           |                                }|j        |	d<   	 t          j	        |	t          j
        t          j
        d          }
|
                                \  }}n# t          $ r t          d          w xY w|
j        r+t!          dd                    |	          d|d|          dS )a  
    Given a BAM file `bam` and assembly `genome`, create a bigWig file scaled
    such that the values represent scaled reads -- that is, reads per million
    mapped reads.

    (Disable this scaling step with scale=False; in this case values will
    indicate number of reads)

    Assumes that `bedGraphToBigWig` from UCSC tools is installed; see
    http://genome.ucsc.edu/goldenPath/help/bigWig.html for more details on the
    format.
    T)bgsplitg   g    .Ascaler)   )r	   r
   universal_newlinesr*   bedSortz+BAM header was not sorted; sorting bedGraphzbedSort was not found on the path. This is an external tool from UCSC which can be downloaded from http://hgdownload.soe.ucsc.edu/admin/exe/. Alternatatively, use `conda install ucsc-bedgraphtobigwig`r+   r,   z	
stderr: z	
stdout: N )r0   r1   r2   dictr&   r@   genome_coverager3   r   r   r   r   r4   r   printsortr   r5   )r   r7   r8   rR   r9   kwargsr!   _scalexr"   r#   r	   r
   ys                 r%   bam_to_bigwigr^   p   s    /
0Ef0M0MNNKT555F !%c**	i#o& w/
3/99&99Ak6:D
??#	
 
 
  
 
 
4
 
 	

 	| 	V++;<<<FFHH$Q	 !!#'	  A ]]__NFFF  	 	 	#8  	 	| 
j25((4....&&&&&Q
 
 	

 
s   AC C AE   E:)F)NNNN)	__doc__r0   r   r   r&   r:   rH   rL   r^   rU   r'   r%   <module>r`      s         				       <  *% % % %>  *<
 <
 <
 <
 <
 <
r'   