import numpy as np
from decimal import *
from StringIO import StringIO
def filter(istr, alpha=.05, ostr=None):
	if ostr == None:
		ostr = istr
	ostr1 = ostr+'.uncor'
	ostr2 = ostr+'.corr'
	ifile = open(istr)
	ofile1 = open(ostr1, 'w+')
	ofile2 = open(ostr2, 'w+')
	source = np.genfromtxt(ifile, delimiter='\t', names=True, dtype=None)
	output1 = []
	output2 = []
	k = len(source)
	m = len(source[0]) - 1
	alphacorr = 1 - (1 - alpha)**(float(1/Decimal(k)))
	for row in source:
		if row[m] < alpha:
			output1.append(row)
			if row[m] < alphacorr:
				output2.append(row)
	for row in output1:
		ofile1.write('\t'.join([str(x) for x in row])+'\n')
	for row in output2:
		ofile2.write('\t'.join([str(x) for x in row])+'\n')
	ofile1.close()
	ofile2.close()
	ifile.close()
