
xYc           @   s2  d  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 m Z m	 Z	 d d l
 m Z d d l m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z i d d 6d	 d
 6d d 6d d 6Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z  d   Z! d S(   sQ  Module Description

Copyright (c) 2010,2011 Tao Liu <taoliu@jimmy.harvard.edu>

This code is free software; you can redistribute it and/or modify it
under the terms of the BSD License (see the file COPYING included with
the distribution).

@status:  experimental
@version: $Revision$
@author:  Tao Liu
@contact: taoliu@jimmy.harvard.edu
iN(   t   ArgumentError(   t   Popent   PIPE(   t   log(
   t	   BEDParsert   ELANDResultParsert   ELANDMultiParsert   ELANDExportParsert	   SAMParsert	   BAMParsert   BAMPEParsert   BEDPEParsert   BowtieParsert   guess_parserg   `At   hsg   {At   mmg    *uAt   ceg    8At   dmc         C   s  y t  |  j |  _ Wnh y t |  j  |  _ Wq t j d |  j  t j d d j t  j     t j d  q Xn Xt	 |  _
 |  j j   |  _ |  j d k r t |  _ n8|  j d k r t |  _ n|  j d k r t |  _ n|  j d k rt |  _ n |  j d	 k r!t |  _ n |  j d
 k rEt |  _ t |  _
 n |  j d k rrt |  _ t |  _
 t |  _ n{ |  j d k rt |  _ t |  _ nW |  j d k rt |  _ n< |  j d k rt |  _ n! t j d |  j  t j d  |  j d k r:|  j d k r:|  j j   s:t j d  t j d  q:n  |  j d k  rft j d  t j d  n  |  j r|  j rt j d  t j d  n  |  j rdM |  _! t" |  j d  d |  _# n" t" |  j$ d  d |  _! dM |  _# |  j r
t" |  j% d  d |  _& n  |  j j   |  _ |  j' d |  _( |  j' d |  _) |  j( |  j) k rrt j d |  j'  t j d  n  t* j+ j |  j, |  j- d  |  _. t* j+ j |  j, |  j- d  |  _/ t* j+ j |  j, |  j- d  |  _0 t* j+ j |  j, |  j- d  |  _1 t* j+ j |  j, |  j- d  |  _2 t* j+ j |  j, |  j- d  |  _3 t* j+ j |  j, |  j- d  |  _4 t* j+ j |  j, |  j- d   |  _5 |  j6 rt* j+ j |  j, |  j- d!  |  _7 n	 dM |  _7 t* j+ j |  j, |  j- d"  |  _8 t j9 d# d$ |  j: d d% d& d' d( d) t j; d* d+  t j< |  _ t j= |  _> t j? |  _? t j@ |  _@ d, j d- d. j t jA d  d/ d0 |  j- d1 |  j d2 |  jB d3 |  jC d4 |  j d5 |  jD d6 |  j' f	  |  _E |  j r+|  j r|  jE d7 |  j 7_E |  jE d8 |  j% 7_E |  jE d9 7_E qy|  jE d: |  j 7_E |  jE d9 7_E nN |  j rc|  jE d; |  j$ 7_E |  jE d< |  j% 7_E n |  jE d= |  j$ 7_E |  jF r|  jE d> 7_E |  jG d k r|  jE d? |  jG 7_E qn* |  jH r|  jE d@ 7_E n |  jE dA 7_E |  jI dB k r|  jE dC |  jI 7_E n  |  jC r9|  jE dD |  jJ |  jK f 7_E n |  jE dE |  jK 7_E |  j rj|  jE dF 7_E n |  jE dG 7_E |  jL dB k r|  jE dH |  jL 7_E n  |  j dN k rd |  _M |  jE dI 7_E n |  jE dJ 7_E |  j r|  jE dK 7_E n  |  jN r|  jO r|  jE dL 7_E n  |  S(O   sR   Validate options from a OptParser object.

    Ret: Validated options object.
    s*   Error when interpreting --gsize option: %ss4   Available shortcuts of effective genome sizes are %st   ,i   t   ELANDt   BEDt
   ELANDMULTIt   ELANDEXPORTt   SAMt   BAMt   BAMPEt   BEDPEt   BOWTIEt   AUTOs!   Format "%s" cannot be recognized!t   autot   alls1   --keep-dup should be 'auto', 'all' or an integer!s   --extsize must >= 1!s.   --broad can't be combined with --call-summits!i
   ii    s8   Upper limit of mfold should be greater than lower limit!s
   _peaks.xlss
   _peaks.beds   _peaks.narrowPeaks   _peaks.broadPeaks   _peaks.gappedPeaks   _summits.beds   _treat_pileup.bdgs   _control_lambda.bdgs   _cutoff_analysis.txts   _model.rt   leveli   t   formats+   %(levelname)-5s @ %(asctime)s: %(message)s t   datefmts   %a, %d %b %Y %H:%M:%St   streamt   filemodet   ws   
s   # Command line: %st    s   # ARGUMENTS LIST:s   # name = %ss   # format = %ss   # ChIP-seq file = %ss   # control file = %ss   # effective genome size = %.2es   # band width = %ds   # model fold = %s
s1   # pvalue cutoff for narrow/strong regions = %.2e
s.   # pvalue cutoff for broad/weak regions = %.2e
sH   # qvalue will not be calculated and reported as -1 in the final output.
s   # pvalue cutoff = %.2e
s1   # qvalue cutoff for narrow/strong regions = %.2e
s.   # qvalue cutoff for broad/weak regions = %.2e
s   # qvalue cutoff = %.2e
sC   # Larger dataset will be randomly sampled towards smaller dataset.
s"   # Random seed has been set as: %d
s9   # Smaller dataset will be scaled towards larger dataset.
s9   # Larger dataset will be scaled towards smaller dataset.
g      ?s&   # Using a custom scaling factor: %.2e
s>   # Range for calculating regional lambda is: %d bps and %d bps
s3   # Range for calculating regional lambda is: %d bps
s   # Broad region calling is on
s   # Broad region calling is off
s0   # Additional cutoff on fold-enrichment is: %.2f
s   # Paired-End mode is on
s   # Paired-End mode is off
s&   # Searching for subpeak summits is on
s:   # MACS will save fragment pileup signal per million reads
N(   R   R   (P   t   efgsizet   gsizet   floatt   loggingt   errort   joint   keyst   syst   exitt   Falset	   gzip_flagR    t   upperR   t   parserR   R   R   R   R	   t   TrueR
   t   nomodelR   R   R   t   keepduplicatest   isdigitt   extsizet   broadt   call_summitst   pvaluet   Nonet
   log_qvalueR   t
   log_pvaluet   qvaluet   broadcutofft   log_broadcutofft   mfoldt   lmfoldt   umfoldt   ost   patht   outdirt   namet   peakxlst   peakbedt   peakNarrowPeakt   peakBroadPeakt   peakGappedPeakt	   summitbedt	   bdg_treatt   bdg_controlt   cutoff_analysist   cutoff_analysis_filet   modelRt   basicConfigt   verboset   stderrt   criticalt   warningt   warnt   debugt   infot   argvt   tfilet   cfilet   bwt   argtxtt
   downsamplet   seedt   tolarget   ratiot
   smalllocalt
   largelocalt   fecutofft   shiftt   do_SPMRt	   store_bdg(   t   options(    (    sP   /oak/stanford/groups/akundaje/marinovg/programs/MACS-2.1.0/MACS2/OptValidator.pyt   opt_validate*   s     									""""""""	%	"	





						"			c         C   sR  t  |  _ |  j r@ d& |  _ t |  j d  d |  _ d |  _ n+ t |  j d  d |  _ d& |  _ d |  _ |  j	 r d& |  _
 t |  j	 d  d |  _ d |  _ n+ t |  j d  d |  _
 d& |  _ d |  _ |  j d |  _ |  j d |  _ |  j d |  _ |  j d |  _ |  j d	 |  _ |  j d	 |  _ |  j d	 |  _ t |  _ |  j d
 k pa|  j d
 k s|  j d
 k rt d d   n! |  j d
 k rt d d   n  t  |  _ d j d d |  j d |  j d |  j d |  j d |  j d |  j d |  j d
 f	  |  _  n d j d d |  j d |  j d |  j d |  j d |  j d
 f  |  _  |  j r|  j  d |  j 7_  |  j  d 7_  n |  j  d |  j 7_  |  j	 r|  j  d |  j 7_  |  j  d 7_  n |  j  d |  j
 7_  t! j" d d |  j# d d d  d! d" d# t$ j% d$ d%  t! j& |  _' t! j( |  _) t! j* |  _* t! j+ |  _+ |  S('   sR   Validate options from a OptParser object.

    Ret: Validated options object.
    i
   it   pt   qs   _diffpeaks.xlss   _diffpeaks.beds   _diffpeaks_by_peaks1.xlss   _diffpeaks_by_peaks2.xlss
   _logLR.bdgt    t   peaks1sE   Must specify both peaks1 and peaks2, or neither (to call peaks again)t   peaks2s   
s   # ARGUMENTS LIST:s   # name = %ss   # ChIP-seq file 1 = %ss   # control file 1 = %ss   # ChIP-seq file 2 = %ss   # control file 2 = %ss   # Peaks, condition 1 = %ss   # Peaks, condition 2 = %ss-   # treat/control -log10(pvalue) cutoff = %.2e
s^   # treat/control -log10(qvalue) will not be calculated and reported as -1 in the final output.
s-   # treat/control -log10(qvalue) cutoff = %.2e
s$   # differential pvalue cutoff = %.2e
sU   # differential qvalue will not be calculated and reported as -1 in the final output.
s$   # differential qvalue cutoff = %.2e
R   i   R    s+   %(levelname)-5s @ %(asctime)s: %(message)s R!   s   %a, %d %b %Y %H:%M:%SR"   R#   R$   N(,   R/   R0   t   peaks_pvalueR;   t   peaks_log_qvalueR   t   peaks_log_pvaluet   track_score_methodt   peaks_qvaluet   diff_pvalueR<   R=   t   score_methodt   diff_qvalueRG   RH   RI   t   peak1xlst   peak2xlst   bdglogLRt	   bdgpvaluet   bdglogFCR3   t
   call_peaksRo   Rp   R    R+   t   t1bdgt   c1bdgt   t2bdgt   c2bdgR_   R)   RS   RT   R-   RU   RV   R*   RW   RX   RY   RZ   (   Rj   (    (    sP   /oak/stanford/groups/akundaje/marinovg/programs/MACS-2.1.0/MACS2/OptValidator.pyt   diff_opt_validate   s    											











			c         C   se  y t  |  j |  _ Wnh y t |  j  |  _ Wq t j d |  j  t j d d j t  j     t j d  q Xn Xt	 |  _
 |  j j   |  _ |  j d k r t |  _ n |  j d k r t |  _ n |  j d k r t |  _ n |  j d k rt |  _ n |  j d	 k r!t |  _ n{ |  j d
 k rEt |  _ t |  _
 nW |  j d k r`t |  _ n< |  j d k r{t |  _ n! t j d |  j  t j d  |  j d k r|  j d k r|  j j   st j d  t j d  qn  |  j j   |  _ t j d d |  j d d d d d d t j d d  t j |  _ t j |  _ t j |  _ t j  |  _  |  S(   sR   Validate options from a OptParser object.

    Ret: Validated options object.
    s*   Error when interpreting --gsize option: %ss4   Available shortcuts of effective genome sizes are %sR   i   R   R   R   R   R   R   R   R   s!   Format "%s" cannot be recognized!R   R   s1   --keep-dup should be 'auto', 'all' or an integer!R   i   i
   R    s+   %(levelname)-5s @ %(asctime)s: %(message)s R!   s   %a, %d %b %Y %H:%M:%SR"   R#   R$   (!   R&   R'   R(   R)   R*   R+   R,   R-   R.   R/   R0   R    R1   R   R2   R   R   R   R   R	   R3   R   R   R5   R6   RS   RT   RU   RV   RW   RX   RY   RZ   (   Rj   (    (    sP   /oak/stanford/groups/akundaje/marinovg/programs/MACS-2.1.0/MACS2/OptValidator.pyt   opt_validate_filterdupT  sZ     			c         C   s	  t  |  _ |  j j   |  _ |  j d k r6 t |  _ n |  j d k rQ t |  _ n |  j d k rl t |  _ n |  j d k r t |  _ n |  j d k r t	 |  _ n{ |  j d k r t
 |  _ t |  _ nW |  j d k r t |  _ n< |  j d k r t |  _ n! t j d	 |  j  t j d
  |  j j   |  _ |  j rg|  j d k rt j d  t j d
  qn8 |  j r|  j d k rt j d  t j d
  qn  t j d d |  j d d d d d d t j d d  t j |  _ t j |  _ t j |  _ t j |  _ |  S(   sR   Validate options from a OptParser object.

    Ret: Validated options object.
    R   R   R   R   R   R   R   R   s!   Format "%s" cannot be recognized!i   g      Y@sK   Percentage can't be bigger than 100.0. Please check your options and retry!i    sX   Number of tags can't be smaller than or equal to 0. Please check your options and retry!R   i   i
   R    s+   %(levelname)-5s @ %(asctime)s: %(message)s R!   s   %a, %d %b %Y %H:%M:%SR"   R#   R$   (   R/   R0   R    R1   R   R2   R   R   R   R   R	   R3   R   R   R)   R*   R-   R.   t
   percentaget   numberRS   RT   RU   RV   RW   RX   RY   RZ   (   Rj   (    (    sP   /oak/stanford/groups/akundaje/marinovg/programs/MACS-2.1.0/MACS2/OptValidator.pyt   opt_validate_randsample  sP    					c         C   s  t  |  _ |  j j   |  _ |  j d k r6 t |  _ n |  j d k rQ t |  _ n |  j d k rl t |  _ n |  j d k r t |  _ n |  j d k r t	 |  _ n{ |  j d k r t
 |  _ t |  _ nW |  j d k r t |  _ n< |  j d k r t |  _ n! t j d	 |  j  t j d
  |  j j   |  _ t j d d |  j d d d d d d t j d d  t j |  _ t j |  _ t j |  _ t j |  _ |  S(   sR   Validate options from a OptParser object.

    Ret: Validated options object.
    R   R   R   R   R   R   R   R   s!   Format "%s" cannot be recognized!i   R   i   i
   R    s+   %(levelname)-5s @ %(asctime)s: %(message)s R!   s   %a, %d %b %Y %H:%M:%SR"   R#   R$   (   R/   R0   R    R1   R   R2   R   R   R   R   R	   R3   R   R   R)   R*   R-   R.   RS   RT   RU   RV   RW   RX   RY   RZ   (   Rj   (    (    sP   /oak/stanford/groups/akundaje/marinovg/programs/MACS-2.1.0/MACS2/OptValidator.pyt   opt_validate_refinepeak  s@    			c         C   s  y t  |  j |  _ Wnh y t |  j  |  _ Wq t j d |  j  t j d d j t  j     t j d  q Xn Xt	 |  _
 |  j j   |  _ |  j d k r t |  _ n8|  j d k r t |  _ n|  j d k r t |  _ n|  j d k rt |  _ n |  j d	 k r!t |  _ n |  j d
 k rEt |  _ t |  _
 n |  j d k rrt |  _ t |  _
 t |  _ n{ |  j d k rt |  _ t |  _ nW |  j d k rt |  _ n< |  j d k rt |  _ n! t j d |  j  t j d  |  j j   |  _ |  j d |  _ |  j d |  _ |  j |  j k rUt j d |  j  t j d  n  t j j |  j |  j   |  _! t j" d d |  j# d d d d d d t j$ d d  t j% |  _ t j& |  _' t j( |  _( t j) |  _) |  S(   sR   Validate options from a OptParser object.

    Ret: Validated options object.
    s*   Error when interpreting --gsize option: %ss4   Available shortcuts of effective genome sizes are %sR   i   R   R   R   R   R   R   R   R   R   R   s!   Format "%s" cannot be recognized!i    s8   Upper limit of mfold should be greater than lower limit!R   i   i
   R    s+   %(levelname)-5s @ %(asctime)s: %(message)s R!   s   %a, %d %b %Y %H:%M:%SR"   R#   R$   (*   R&   R'   R(   R)   R*   R+   R,   R-   R.   R/   R0   R    R1   R   R2   R   R   R   R   R	   R3   R
   R4   R   R   R   RA   RB   RC   RD   RE   RF   t   rfileRR   RS   RT   RU   RV   RW   RX   RY   RZ   (   Rj   (    (    sP   /oak/stanford/groups/akundaje/marinovg/programs/MACS-2.1.0/MACS2/OptValidator.pyt   opt_validate_predictd  sl     						c         C   s  t  |  _ |  j j   |  _ |  j d k r6 t |  _ n |  j d k rQ t |  _ n |  j d k rl t |  _ n |  j d k r t |  _ n |  j d k r t	 |  _ n{ |  j d k r t
 |  _ t |  _ nW |  j d k r t |  _ n< |  j d k r t |  _ n! t j d	 |  j  t j d
  |  j j   |  _ t j d d |  j d d d d d d t j d d  t j |  _ t j |  _ t j |  _ t j |  _ |  j d k rt j d  t j d
  n  |  S(   sR   Validate options from a OptParser object.

    Ret: Validated options object.
    R   R   R   R   R   R   R   R   s!   Format "%s" cannot be recognized!i   R   i   i
   R    s+   %(levelname)-5s @ %(asctime)s: %(message)s R!   s   %a, %d %b %Y %H:%M:%SR"   R#   R$   i    s   --extsize must > 0!(   R/   R0   R    R1   R   R2   R   R   R   R   R	   R3   R   R   R)   R*   R-   R.   RS   RT   RU   RV   RW   RX   RY   RZ   R7   (   Rj   (    (    sP   /oak/stanford/groups/akundaje/marinovg/programs/MACS-2.1.0/MACS2/OptValidator.pyt   opt_validate_pileupN  sF    			c         C   s   t  j d d d d d d d t j d d	  t  j |  _ t  j |  _ t  j |  _ t  j	 |  _	 xD t
 |  j  D]3 } | d k rk t  j d |  t j d  qk qk W|  j r t |  j  t |  j  k r t  j d  t j d  q n  |  S(   sR   Validate options from a OptParser object.

    Ret: Validated options object.
    R   i   R    s+   %(levelname)-5s @ %(asctime)s: %(message)s R!   s   %a, %d %b %Y %H:%M:%SR"   R#   R$   t   ppoist   qpoist   subtractt   logFEt   FEt   logLRt   slogLRt   maxs   Invalid method: %si   sM   The number and the order of arguments for --ofile must be the same as for -m.(   R   R   R   R   R   R   R   R   (   R)   RS   R-   RU   RV   R*   RW   RX   RY   RZ   t   sett   methodR.   t   ofilet   len(   Rj   R   (    (    sP   /oak/stanford/groups/akundaje/marinovg/programs/MACS-2.1.0/MACS2/OptValidator.pyt   opt_validate_bdgcmp  s$    		c         C   s   t  j d d d d d d d t j d d	  t  j |  _ t  j |  _ t  j |  _ t  j	 |  _	 |  j
 d k r t  j d |  j
  t j d  n  t |  j  d k r t  j d  t j d  n  |  S(   sR   Validate options from a OptParser object.

    Ret: Validated options object.
    R   i   R    s+   %(levelname)-5s @ %(asctime)s: %(message)s R!   s   %a, %d %b %Y %H:%M:%SR"   R#   R$   t   fisherR   t   means   Invalid method: %si   i   s   Only support two replicates!(   R   R   R   (   R)   RS   R-   RU   RV   R*   RW   RX   RY   RZ   R   R.   R   t   ifile(   Rj   (    (    sP   /oak/stanford/groups/akundaje/marinovg/programs/MACS-2.1.0/MACS2/OptValidator.pyt   opt_validate_cmbreps  s     	c         C   s   t  j d d d d d d d t j d d	  t  j |  _ t  j |  _ t  j |  _ t  j	 |  _	 |  j
 j   d k r t  j d |  j
  t j d  n  |  j
 j   d k r |  j r t  j d  t j d  n  |  S(   sR   Validate options from a OptParser object.

    Ret: Validated options object.
    R   i   R    s+   %(levelname)-5s @ %(asctime)s: %(message)s R!   s   %a, %d %b %Y %H:%M:%SR"   R#   R$   t   multiplyt   addt   p2qR   t   mins   Invalid method: %si   s+   Need EXTRAPARAM for method multiply or add!(   R   R   R   R   R   (   R   R   (   R)   RS   R-   RU   RV   R*   RW   RX   RY   RZ   R   t   lowerR.   t
   extraparam(   Rj   (    (    sP   /oak/stanford/groups/akundaje/marinovg/programs/MACS-2.1.0/MACS2/OptValidator.pyt   opt_validate_bdgopt  s     	("   t   __doc__R-   RD   t   reR)   t   argparseR    t
   subprocessR   R   t   mathR   t   MACS2.IO.ParserR   R   R   R   R   R	   R
   R   R   R   R&   Rk   R   R   R   R   R   R   R   R   R   (    (    (    sP   /oak/stanford/groups/akundaje/marinovg/programs/MACS-2.1.0/MACS2/OptValidator.pyt   <module>   s,   F

		h	B	;	1	L	6	#	-