<commit_before>import argparse
import sys
import logging
import os
import errno
from asyncore import read

from Mutect import MultiMutectItem

def check_file_exists(file):
  if not os.path.exists(file):
    raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), file)

def filterMutect(logger, inputFile, outputFile, minNormalDepth, minTumorDepth, minMinorAlleleDepth): 
  check_file_exists(inputFile) 

  logger.info("Processing %s ..." % inputFile)

  lineCount = 0
  passedCount = 0
  failedCount = 0
  with open(outputFile, "wt") as fout:
    if inputFile.endswith(".gz"):
      fin = gzip.open(inputFile,'rt')
    else:
      fin = open(inputFile, "rt")

    with fin:
      for line in fin:
        if line.startswith("#"):
          fout.write(line)
          continue

        lineCount += 1
        if lineCount % 10000 == 0:
          logger.info("%d" % lineCount)

        item = MultiMutectItem(line)

        passed = False
        for sample in item.Samples:
          if sample.NormalDepth >= minNormalDepth and sample.TumorDepth >= minTumorDepth and sample.MinorAlleleDepth >= minMinorAlleleDepth:
            passed = True
            break

        if passed:
          fout.write(line)
          passedCount += 1
        else:
          failedCount += 1
      #  logger.info("Discarded:" + item.Line)
      
  with open(outputFile + ".stat", "wt") as fout:
    fout.write("minNormalDepth\t%d\n" % minNormalDepth)
    fout.write("minTumorDepth\t%d\n" % minTumorDepth)
    fout.write("minMinorAlleleDepth\t%d\n" % minMinorAlleleDepth)
    fout.write("Passed\t%d\n" % passedCount)
    fout.write("Failed\t%d\n" % failedCount)

  logger.info("Passed=%d, failed=%d" % (passedCount, failedCount))
    
def main():
  DEBUG=False
  NotDEBUG=not DEBUG

  parser = argparse.ArgumentParser(description="filter mutect result to keep tumor sample only.",
                                  formatter_class=argparse.ArgumentDefaultsHelpFormatter)
<commit_msg>add tmp file<commit_after>import argparse
import sys
import logging
import os
import errno
import gzip
from asyncore import read

from Mutect import MultiMutectItem

def check_file_exists(file):
  if not os.path.exists(file):
    raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), file)

def filterMutect(logger, inputFile, outputFile, minNormalDepth, minTumorDepth, minMinorAlleleDepth): 
  check_file_exists(inputFile) 

  logger.info("Processing %s ..." % inputFile)

  lineCount = 0
  passedCount = 0
  failedCount = 0

  tmpFile = outputFile + ".tmp.vcf"
  with open(tmpFile, "wt") as fout:
    if inputFile.endswith(".gz"):
      fin = gzip.open(inputFile,'rt')
    else:
      fin = open(inputFile, "rt")

    with fin:
      for line in fin:
        if line.startswith("#"):
          fout.write(line)
          continue

        lineCount += 1
        if lineCount % 10000 == 0:
          logger.info("%d" % lineCount)

        item = MultiMutectItem(line)

        passed = False
        for sample in item.Samples:
          if sample.NormalDepth >= minNormalDepth and sample.TumorDepth >= minTumorDepth and sample.MinorAlleleDepth >= minMinorAlleleDepth:
            passed = True
            break

        if passed:
          fout.write(line)
          passedCount += 1
        else:
          failedCount += 1
      #  logger.info("Discarded:" + item.Line)
      
  with open(outputFile + ".stat", "wt") as fout:
    fout.write("minNormalDepth\t%d\n" % minNormalDepth)
    fout.write("minTumorDepth\t%d\n" % minTumorDepth)
    fout.write("minMinorAlleleDepth\t%d\n" % minMinorAlleleDepth)
    fout.write("Passed\t%d\n" % passedCount)
    fout.write("Failed\t%d\n" % failedCount)

  if os.path.isfile(outputFile):
    os.remove(outputFile)
  os.rename(tmpFile, outputFile)

  logger.info("Passed=%d, failed=%d" % (passedCount, failedCount))
    
def main():
  DEBUG=False
  NotDEBUG=not DEBUG

  parser = argparse.ArgumentParser(description="filter mutect result to keep tumor sample only.",
                                  formatter_class=argparse.ArgumentDefaultsHelpFormatter)

{'fraction_non_alphanumeric': 0.10969637610186092, 'fraction_numerical': 0.005876591576885406, 'mean_word_length': 2.8501413760603205, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1171583', 'n_tokens_mistral': 1357, 'n_tokens_neox': 1285, 'n_words': 292}