<commit_before>#!/usr/bin/python
import sys
import os
import inspect
import subprocess as sub
import shlex

currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0,parentdir) 
import common
import config as cfg
	
	
	
	
	
	
	
	
	
	
def runCommand(cmd, outfile=False):
	cmd = shlex.split(cmd)
	
	if outfile:
		if os.path.exists(outfile):
			stdout = open(outfile, 'a')
			stdout.flush()
		else:
			stdout = open(outfile, 'w')
		p = sub.Popen(cmd, stdout=stdout, stderr=sub.STDOUT)
		
	else:
		p = sub.Popen(cmd)
		
	p.wait()
	
	return 0










def runOne(fastqFile, species, trim, statsDir, tempDir, samDir):
  
	#get environment prepared#
	mapVars = cfg.Map()

 	fastqFile = common.zipping(fastqFile)
	
	sampleName = os.path.basename(fastqFile)[:-6]
  
	statFile = statsDir + sampleName + '.mapstats.txt'

  
  
	#run bowtie#
	cmd =	mapVars.bowtieOptions +	\
			[
			'-5', str(trim[0]), 
			'-3', str(trim[1]), 
			mapVars.indexDict[species], 
			fastqFile, 
			tempDir + sampleName + '.sam'
			]
	cmd = ' '.join(cmd)
	runCommand(cmd, outfile=statFile)
	
	
	
	#sam to bam#
	cmd =	[
			mapVars.samtools, 'view', '-bS', 
		  	'-o', tempDir + sampleName + '.bam',
		 	tempDir + sampleName + '.sam'
			]
	cmd = ' '.join(cmd)
	runCommand(cmd)
	

	
	#sort bam#
	cmd =	[
			mapVars.samtools, 'sort', 
			'-T', tempDir + sampleName + '.sorted',
			'-o', tempDir + sampleName + '.sorted.bam',
		 	tempDir + sampleName + '.bam'
			]
	cmd = ' '.join(cmd)
	runCommand(cmd)
	
	
	
	#remove duplicates#
	cmd =	[
			mapVars.samtools, 'rmdup', '-s', 
			tempDir + sampleName + '.sorted.bam',
			tempDir + sampleName + '.unique.bam'
			]
	cmd = ' '.join(cmd)
	runCommand(cmd, outfile=statFile)

	
	
	#bam to sam#
	cmd =	[
			mapVars.samtools, 'view', '-h', 
		  	'-o', samDir + sampleName + '.unique.sam',
		 	tempDir + sampleName + '.unique.bam'
			]
	cmd = ' '.join(cmd)
	runCommand(cmd)


	
	printText = '\t\tFinished mapping and removing PCR duplicates for ' + os.path.basename(fastqFile)
	print(printText)




	
<commit_msg>Update mapfile.py<commit_after>#!/usr/bin/python
import sys
import os
import inspect
import subprocess as sub
import shlex

currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0,parentdir) 
import common
import config as cfg
	
	
	
	
	
	
	
	
	
	
def runCommand(cmd, outfile=False):
	cmd = shlex.split(cmd)
	
	if outfile:
		if os.path.exists(outfile):
			stdout = open(outfile, 'a')
			stdout.flush()
		else:
			stdout = open(outfile, 'w')
		p = sub.Popen(cmd, stdout=stdout, stderr=sub.STDOUT)
		
	else:
		p = sub.Popen(cmd)
		
	p.wait()
	
	return 0










def runOne(fastqFile, species, trim, statsDir, tempDir, samDir):
  
	#get environment prepared#
	mapVars = cfg.Map()

 	fastqFile = common.zipping(fastqFile)
	
	sampleName = os.path.basename(fastqFile)[:-6]
  
	statFile = statsDir + sampleName + '.map.stats.txt'

  
  
	#run bowtie#
	cmd =	mapVars.bowtieOptions +	\
			[
			'-5', str(trim[0]), 
			'-3', str(trim[1]), 
			mapVars.indexDict[species], 
			fastqFile, 
			tempDir + sampleName + '.sam'
			]
	cmd = ' '.join(cmd)
	runCommand(cmd, outfile=statFile)
	
	
	
	#sam to bam#
	cmd =	[
			mapVars.samtools, 'view', '-bS', 
		  	'-o', tempDir + sampleName + '.bam',
		 	tempDir + sampleName + '.sam'
			]
	cmd = ' '.join(cmd)
	runCommand(cmd)
	

	
	#sort bam#
	cmd =	[
			mapVars.samtools, 'sort', 
			'-T', tempDir + sampleName + '.sorted',
			'-o', tempDir + sampleName + '.sorted.bam',
		 	tempDir + sampleName + '.bam'
			]
	cmd = ' '.join(cmd)
	runCommand(cmd)
	
	
	
	#remove duplicates#
	cmd =	[
			mapVars.samtools, 'rmdup', '-s', 
			tempDir + sampleName + '.sorted.bam',
			tempDir + sampleName + '.unique.bam'
			]
	cmd = ' '.join(cmd)
	runCommand(cmd, outfile=statFile)

	
	
	#bam to sam#
	cmd =	[
			mapVars.samtools, 'view', '-h', 
		  	'-o', samDir + sampleName + '.unique.sam',
		 	tempDir + sampleName + '.unique.bam'
			]
	cmd = ' '.join(cmd)
	runCommand(cmd)


	
	printText = '\t\tFinished mapping and removing PCR duplicates for ' + os.path.basename(fastqFile)
	print(printText)




	

{'fraction_non_alphanumeric': 0.15728201473033976, 'fraction_numerical': 0.003326205749584224, 'mean_word_length': 2.9943074003795065, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4580946', 'n_tokens_mistral': 1882, 'n_tokens_neox': 1646, 'n_words': 335}