# Path: lib/util.py
# STATUS_NORMAL=1
# STATUS_MAX_MEMORY_EXCEEDED=2
# STATUS_MAX_RUNTIME_EXCEEDED=3
# MAX_LEN_OUT = 10000
# def runAndMeasure(command,detailed=True,maxtime=0,maxmem=0):
# def runSimple(command):
# def runAndMeasureInternal(return_queue,command,maxtime=0,maxmem=0):
# def runInternal(control_queue,command,max_memory):
# def measureProcess(queue, initial_pids, command, measurement_interval=1, max_runtime=0, max_memory=0, debug=False):
# 	def extendTargets(targets, proc):
# 	def killTargets(targets):
# 	def updateTargets(targets, metrics):
# 	def updateMetrics(target, metrics):
# def loadConfig(name, parent_dir="", already_included=[]):
# def setCallDir(d):
# def setRootDir(d):
# def enterCallDir():
# def enterRootDir():
# def getRootDir():
# def nl2br(text):
# def msg(text="", level=1):
# def yes_no(b):
# def md5(text):
# def merge(x, y):
# def formatFilesize(n):
# def percent(val, base, offset=0):
# def abs_path(path):
# def get_sam_header_line_count(filename):
# def is_sam_sorted(filename):
# def sort_sam(filename,threads=1):
# def sort_sam_picard(filename,threads=-1):
# def line_count(filename):
# def sanitize_string(s):
# def makeExportDropdown(plot_id,csv_filename):
# def parseMD(md,seq):
# 
# Path: lib/gsample.py
# def log(msg):
# def index(reference, fastindex_path = "tools/fastindex"):
# def index_legacy(reference):
# def downsample(index, contig_filename, downsampled_filename, region_count, target_len, single_contig=False,
# 			   region_list=False, spacer_len=200):
# def csample(infile, region_size, sample_fraction, spacer_len=200, fastindex_path=""):
# def ctranslate(reference_file, sampled_index_file, sam_file, target_file, fastindex_path=""):
# 
# Path: tools/fastq2sam.py
# def encode_qname(qname, retain_petag=True):
# 	def __init__(self):
# 	def __init__(self, filename, pe):
# 	def readline(self):
# 	def next_read(self):
# 	def __init__(self):
# 	def align(read):
# 	def align(self, read, paired=False, is_read1=True):
# 	def align_pair(self, read1, read2):
# 	def align(self, read, paired=False, is_read1=True):
# 	def align_pair(self, read1, read2):
# 	def __init__(self, aligner, outfile):
# 	def write(self, what):
# 	def write_sam_header(self, reads):
# 	def write_single(self, read, to):
# 	def align_se(self, infile):
# 	def align_pe(self, infile_1, infile_2):
# class Object:
# class FASTQ:
# class Aligner:
# class DummyAligner(Aligner):
# class dwgsim(Aligner):
# class Converter:


import os
import shutil
import time
import yaml
from lib import util
from lib import gsample
from tools import fastq2sam

		platform_ids = {"illumina":0,"ion_torrent":2}
		params = ""

		if self.dataset["platform"] == "ion_torrent":
			params += " -f TACGTACGTCTGAGCATCGATCGATGTACAGC "

		read_length_1 = str(self.dataset["read_length"])
		if self.dataset["paired"]:
			read_length_2 = read_length_1
			read_2_fastq2sam = " dwgout.bwa.read2.fastq"
			read_count = int(self.dataset["read_count"]/2)
		else:
			read_length_2 = "0"
			read_2_fastq2sam = ""
			read_count = self.dataset["read_count"]

		if self.dataset["insert_size"] != None:
			params += " -d %d -s %d " % (self.dataset["insert_size"],self.dataset["insert_size_error"])

		if self.dataset["error_rate_mult"] != 1:
			self.dataset["extra_params"] += " -e %f " % (self.dataset["error_rate_mult"] * 0.02)

		params += " -r %f " % self.dataset["mutation_rate"]
		params += " -R %f " % self.dataset["mutation_indel_frac"]

		self.dataset["simulator_cmd"] = self.bin_path + " -c " + str(platform_ids[self.dataset["platform"]]) + " -1 " + read_length_1 + " -2 " + read_length_2 + " -N " + str(read_count) + params + " -y 0 " + self.dataset["extra_params"] + " " + self.dataset["reference_sim"] + " dwgout"
		self.subprogram(self.dataset["simulator_cmd"])

		aligner = fastq2sam.dwgsim()

# Path: lib/util.py
# STATUS_NORMAL=1
# STATUS_MAX_MEMORY_EXCEEDED=2
# STATUS_MAX_RUNTIME_EXCEEDED=3
# MAX_LEN_OUT = 10000
# def runAndMeasure(command,detailed=True,maxtime=0,maxmem=0):
# def runSimple(command):
# def runAndMeasureInternal(return_queue,command,maxtime=0,maxmem=0):
# def runInternal(control_queue,command,max_memory):
# def measureProcess(queue, initial_pids, command, measurement_interval=1, max_runtime=0, max_memory=0, debug=False):
# 	def extendTargets(targets, proc):
# 	def killTargets(targets):
# 	def updateTargets(targets, metrics):
# 	def updateMetrics(target, metrics):
# def loadConfig(name, parent_dir="", already_included=[]):
# def setCallDir(d):
# def setRootDir(d):
# def enterCallDir():
# def enterRootDir():
# def getRootDir():
# def nl2br(text):
# def msg(text="", level=1):
# def yes_no(b):
# def md5(text):
# def merge(x, y):
# def formatFilesize(n):
# def percent(val, base, offset=0):
# def abs_path(path):
# def get_sam_header_line_count(filename):
# def is_sam_sorted(filename):
# def sort_sam(filename,threads=1):
# def sort_sam_picard(filename,threads=-1):
# def line_count(filename):
# def sanitize_string(s):
# def makeExportDropdown(plot_id,csv_filename):
# def parseMD(md,seq):
# 
# Path: lib/gsample.py
# def log(msg):
# def index(reference, fastindex_path = "tools/fastindex"):
# def index_legacy(reference):
# def downsample(index, contig_filename, downsampled_filename, region_count, target_len, single_contig=False,
# 			   region_list=False, spacer_len=200):
# def csample(infile, region_size, sample_fraction, spacer_len=200, fastindex_path=""):
# def ctranslate(reference_file, sampled_index_file, sam_file, target_file, fastindex_path=""):
# 
# Path: tools/fastq2sam.py
# def encode_qname(qname, retain_petag=True):
# 	def __init__(self):
# 	def __init__(self, filename, pe):
# 	def readline(self):
# 	def next_read(self):
# 	def __init__(self):
# 	def align(read):
# 	def align(self, read, paired=False, is_read1=True):
# 	def align_pair(self, read1, read2):
# 	def align(self, read, paired=False, is_read1=True):
# 	def align_pair(self, read1, read2):
# 	def __init__(self, aligner, outfile):
# 	def write(self, what):
# 	def write_sam_header(self, reads):
# 	def write_single(self, read, to):
# 	def align_se(self, infile):
# 	def align_pe(self, infile_1, infile_2):
# class Object:
# class FASTQ:
# class Aligner:
# class DummyAligner(Aligner):
# class dwgsim(Aligner):
# class Converter:


# Path: lib/simulator.py
import os
import shutil
import time
import yaml
from lib import util
from lib import gsample
from tools import fastq2sam


		platform_ids = {"illumina":0,"ion_torrent":2}
		params = ""

		if self.dataset["platform"] == "ion_torrent":
			params += " -f TACGTACGTCTGAGCATCGATCGATGTACAGC "

		read_length_1 = str(self.dataset["read_length"])
		if self.dataset["paired"]:
			read_length_2 = read_length_1
			read_2_fastq2sam = " dwgout.bwa.read2.fastq"
			read_count = int(self.dataset["read_count"]/2)
		else:
			read_length_2 = "0"
			read_2_fastq2sam = ""
			read_count = self.dataset["read_count"]

		if self.dataset["insert_size"] != None:
			params += " -d %d -s %d " % (self.dataset["insert_size"],self.dataset["insert_size_error"])

		if self.dataset["error_rate_mult"] != 1:
			self.dataset["extra_params"] += " -e %f " % (self.dataset["error_rate_mult"] * 0.02)

		params += " -r %f " % self.dataset["mutation_rate"]
		params += " -R %f " % self.dataset["mutation_indel_frac"]

		self.dataset["simulator_cmd"] = self.bin_path + " -c " + str(platform_ids[self.dataset["platform"]]) + " -1 " + read_length_1 + " -2 " + read_length_2 + " -N " + str(read_count) + params + " -y 0 " + self.dataset["extra_params"] + " " + self.dataset["reference_sim"] + " dwgout"
		self.subprogram(self.dataset["simulator_cmd"])

		aligner = fastq2sam.dwgsim()

		conv = fastq2sam.Converter(aligner, "dwgout.bwa.read1.fastq.sam")