Skip to content

Commit

Permalink
Fix unicode error for bam read/write
Browse files Browse the repository at this point in the history
  • Loading branch information
kclem committed Oct 20, 2021
1 parent a923a7c commit 8196b6a
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions CRISPResso2/CRISPRessoCORE.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,13 @@ def get_n_reads_fastq(fastq_filename):
return int(float(p.communicate()[0])/4.0)

def get_n_reads_bam(bam_filename,bam_chr_loc=""):
p = sb.Popen("samtools view -c " + bam_filename + " " + bam_chr_loc, shell=True, stdout=sb.PIPE)
return int(float(p.communicate()[0]))
cmd = "samtools view -c " + bam_filename + " " + bam_chr_loc
p = sb.Popen(cmd, shell=True, stdout=sb.PIPE)
try:
retval = int(float(p.communicate()[0]))
except ValueError:
raise CRISPRessoShared.InstallationException('Error when running the command:' + cmd + '\nCheck that samtools is installed correctly.')
return retval

#import time
#start = time.time()
Expand Down Expand Up @@ -499,17 +504,17 @@ def unexplodeCigar(explodedCigarString):
output_sam = output_bam+".sam"
with open(output_sam, "w") as sam_out:
#first, write header to sam
proc = sb.Popen(['samtools', 'view', '-H', bam_filename], stdout=sb.PIPE, stderr=sb.PIPE, bufsize=1)
proc = sb.Popen(['samtools', 'view', '-H', bam_filename], stdout=sb.PIPE, stderr=sb.PIPE, encoding='utf-8')
for line in proc.stdout:
sam_out.write(line)

crispresso_cmd_to_write = ' '.join(sys.argv)
sam_out.write('@PG\tID:crispresso2\tPN:crispresso2\tVN:'+CRISPRessoShared.__version__+'\tCL:"'+crispresso_cmd_to_write+'"\n')

if bam_chr_loc != "":
proc = sb.Popen(['samtools', 'view', bam_filename, bam_chr_loc], stdout=sb.PIPE, bufsize=1)
proc = sb.Popen(['samtools', 'view', bam_filename, bam_chr_loc], stdout=sb.PIPE, encoding='utf-8')
else:
proc = sb.Popen(['samtools', 'view', bam_filename], stdout=sb.PIPE, bufsize=1)
proc = sb.Popen(['samtools', 'view', bam_filename], stdout=sb.PIPE, encoding='utf-8')
for sam_line in proc.stdout:
sam_line_els = sam_line.rstrip().split("\t")
fastq_seq = sam_line_els[9]
Expand Down Expand Up @@ -891,7 +896,7 @@ def rreplace(s, old, new):
info('Index file for input .bam file exists, skipping generation.')
else:
info('Creating index file for input .bam file...')
bam_input_file = _jp(os.path.basename(args.bam_input_file))+".sorted.bam"
bam_input_file = _jp(os.path.basename(args.bam_input))+".sorted.bam"
sb.call('samtools sort -o '+bam_input_file+' ' + args.bam_input, shell=True)
sb.call('samtools index %s ' % (bam_input_file), shell=True)
files_to_remove.append(bam_input_file)
Expand Down

0 comments on commit 8196b6a

Please sign in to comment.