#!/bin/env python 

#This script is like snpPlotScrpt, except that it only loads a single numpy array. 
#For normalization, yVar2 should be set to appropriate number of phased deletion events used.
#In our case, we provide the two numbers for NA12878 (300) and NA19240 (457).
#The other arguments are as in snpPlotScrpt. 

from __future__ import division
from __future__ import print_function
import sys
import numpy as np
import matplotlib.pyplot as plt
import math

yVar2 = 300 #457

if len(sys.argv) != 5:
  print("Usage: {0} file1 WIN bin_size flag".format(sys.argv[0])) 
  print("Set flag to hist, for histogram; otherwise type 0 for it")
  sys.exit()

yVar1 = np.load(sys.argv[1])
winSz = int(sys.argv[2])
binSz = int(sys.argv[3])
flg = sys.argv[4]

#f for flank
nfBins = int(math.ceil(winSz/binSz))
print("nfBins: {0}".format(nfBins))

snpBinSum = np.zeros(2*nfBins)
 
#builds the right bins
for j,i in zip(range(nfBins, 2*nfBins), range(winSz, 2*winSz, binSz)):
  if j < 2*nfBins-1:
    if flg == "hist":
      snpBinSum[j] = sum(yVar1[i:i+binSz]) #just count the counts
    else:
      snpBinSum[j] = sum(yVar1[i:i+binSz])/(yVar2*binSz)
  else:
    if flg == "hist":
      snpBinSum[j] = sum(yVar1[i:]) #just count the counts
    else:
      #print(j, i)
      #print(len(yVar1[i:]))
      snpBinSum[j] = sum(yVar1[i:])/(yVar2*len(yVar1[i:]))
    
#builds the left bins
for j, i in zip(range(nfBins-1, -1, -1), range(winSz-1, -1, -binSz)):
  if j > 0:
    if flg == "hist":
      snpBinSum[j] = sum(yVar1[i:i-binSz:-1])
    else:
      snpBinSum[j] = sum(yVar1[i:i-binSz:-1])/(yVar2*binSz)
  else:
    if flg == "hist":
      snpBinSum[j] = sum(yVar1[i::-1])
    else:
      snpBinSum[j] = sum(yVar1[i::-1])/(yVar2*len(yVar1[i::-1]))

  
#This sets the x-values for the plot. Note that this is essentially
#follows the i index above (we choose every binSz). 
xVar = np.concatenate((np.arange(-winSz, 0, binSz), np.arange(0, winSz, binSz)))

#The code below can be commented out to draw histogram or a plot of density. Or one can
#plot in ipython window directly using matplotlib plot programs.
#plt.figure()
#plt.bar(xVar, snpBinSum, width=400,color='#920000')
#plt.plot(xVar, snpBinSum, color='#920000')
#below, out-of-phase color choice
#plt.plot(xVar, snpBinSum, color='#006ddb')
#plt.show()
