This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Error using bamCoverage to generate bw file

Dear All,

I am trying to generate bw files using bamCoverage. But, everytime it causes errors like this:

bamFilesList: ['./alignment/bam/M-EK-0D-IgG-1_S31_L003.mapped.sorted.bam']
binLength: 50
numberOfSamples: None
blackListFileName: None
skipZeroOverZero: False
bed_and_bin: False
genomeChunkSize: None
defaultFragmentLength: read length
numberOfProcessors: 1
verbose: False
region: None
bedFile: None
minMappingQuality: None
ignoreDuplicates: False
chrsToSkip: []
stepSize: 50
center_read: False
samFlag_include: None
samFlag_exclude: None
minFragmentLength: 0
maxFragmentLength: 0
zerosToNans: False
smoothLength: None
save_data: False
out_file_for_raw_data: None
maxPairedFragmentLength: 1000
Traceback (most recent call last):
  File "/home/anaconda3/bin/bamCoverage", line 12, in <module>
    main(args)
  File "/home/anaconda3/lib/python3.8/site-packages/deeptools/bamCoverage.py", line 256, in main
    wr.run(writeBedGraph.scaleCoverage, func_args, args.outFileName,
  File "/home/anaconda3/lib/python3.8/site-packages/deeptools/writeBedGraph.py", line 171, in run
    bedGraphToBigWig(chrom_names_and_size, [x[3] for x in res], out_file_name)
  File "/home/anaconda3/lib/python3.8/site-packages/deeptools/writeBedGraph.py", line 293, in bedGraphToBigWig
    bw.addHeader(chromSizes, maxZooms=10)
RuntimeError: Received an error while writing the bigWig header
[bwClose] There was an error while finishing writing a bigWig file! The output is likely truncated.

I used the following commands to generate bam files:

# bowtie2 alignment
bowtie2 -p6 --very-sensitive-local --no-mixed --no-discordant --phred33 -I 10 -X 2000 -x ./bt2/chr \
-1 ./trim/${SAMPLE}_R1_001.trimmed.fastq.gz \
-2 ./trim/${SAMPLE}_R2_001.trimmed.fastq.gz \
-S ./alignment/sam/${SAMPLE}_bowtie2.sam &> ./alignment/sam/${SAMPLE}_bowtie2.txt;

# remove duplicate with picard
java -jar picard.jar MarkDuplicates \
I=./alignment/removeDuplicate/${SAMPLE}_bowtie2.sorted.sam \
O=./alignment/removeDuplicate/${SAMPLE}_bowtie2.sorted.rmDup.sam \
REMOVE_DUPLICATES=true \
M=./alignment/removeDuplicate/picard_summary/${SAMPLE}_picard.rmDup.txt;

# Filtering mapped reads by the mapping quality filtering
minQualityScore=2
samtools view -q $minQualityScore ./alignment/removeDuplicate/${SAMPLE}_bowtie2.sorted.rmDup.sam >./alignment/sam/${SAMPLE}_bowtie2.qualityScore2.sam; 

#convert to bam files
samtools view -bS -h -F 0x04 ./alignment/removeDuplicate/${SAMPLE}_bowtie2.sorted.rmDup.sam > ./alignment/bam/${SAMPLE}_bowtie2.mapped.bam; \
samtools sort ./alignment/bam/${SAMPLE}_bowtie2.mapped.bam \
-o ./alignment/bam/${SAMPLE}.mapped.sorted.bam; \
samtools index ./alignment/bam/${SAMPLE}.mapped.sorted.bam \
./alignment/bam/${SAMPLE}.mapped.sorted.bai;

#bamCoverage command
bamCoverage -b ./alignment/bam/${SAMPLE}.mapped.sorted.bam -o ./alignment/bigwig/${SAMPLE}.bw

It sounds like the header problem, but not clear how to resolve it... "RuntimeError: Received an error while writing the bigWig header"

I sincerely appreciate reading this long email and your kind help.

Best,

bowtie2 bamcoverage

1 answer

samtools view -q $minQualityScore ./alignment/removeDuplicate/${SAMPLE}_bowtie2.sorted.rmDup.sam >./alignment/sam/${SAMPLE}_bowtie2.qualityScore2.sam; 

Add option -h to preserve the header. Or better work with BAM files right away, SAMs have no value tbh.

Thank you so much. I did not use -h with -q. I will also work with BAM rather than SAM! Thank you again. Best,

Hi ATpoint,

I changed my script like this:

# bowtie2 alignment
bowtie2 -p6 --very-sensitive-local --no-mixed --no-discordant --phred33 -I 10 -X 2000 -x ./bt2/chr \
-1 ./trim/${SAMPLE}_R1_001.trimmed.fastq.gz \
-2 ./trim/${SAMPLE}_R2_001.trimmed.fastq.gz \
-S ./alignment/sam/${SAMPLE}_bowtie2.sam &> ./alignment/sam/${SAMPLE}_bowtie2.txt;

#Filtering mapped reads by the mapping quality filtering
minQualityScore=2

#Filter and keep the mapped read pairs
for SAMPLE in $SAMPLES; \
do samtools view -b -h -q $minQualityScore -F 0x04 ./alignment/sam/${SAMPLE}_bowtie2.sam | samtools sort -o ./alignment/bam/${SAMPLE}.sorted.bam; \
samtools index ./alignment/bam/${SAMPLE}.sorted.bam ./alignment/bam/${SAMPLE}.sorted.bai; \
done

#Remove duplicates 
for SAMPLE in $SAMPLES; \
do java -jar picard.jar MarkDuplicates \
I=./alignment/bam/${SAMPLE}.sorted.bam \
O=./alignment/bam/${SAMPLE}.sorted.rmDup.bam \
REMOVE_DUPLICATES=true \
M=./alignment/bam/picard_summary/${SAMPLE}_picard.rmDup.txt; \
samtools index ./alignment/bam/${SAMPLE}.sorted.rmDup.bam ./alignment/bam/${SAMPLE}.sorted.rmDup.bai; \
#done

#bw file generation
for SAMPLE in $SAMPLES; \
do bamCoverage -b ./alignment/bam/${SAMPLE}.sorted.bam -o ./alignment/bigwig/${SAMPLE}_ori.bw --normalizeUsing RPKM --binSize 10 -p 10 2> ./alignment/bigwig/${SAMPLE}_ori.bw.log;
done

As -h was used with samtools, I expected bamCoverage might go forward but it caused the same error...

normalization: RPKM
bamFilesList: ['./alignment/bam/F-EK-0D-H3K4me3-1_S26_L003.sorted.rmDup.bam']
binLength: 10
numberOfSamples: None
blackListFileName: None
skipZeroOverZero: False
bed_and_bin: False
genomeChunkSize: None
defaultFragmentLength: read length
numberOfProcessors: 6
verbose: False
region: None
bedFile: None
minMappingQuality: None
ignoreDuplicates: False
chrsToSkip: []
stepSize: 10
center_read: False
samFlag_include: None
samFlag_exclude: None
minFragmentLength: 0
maxFragmentLength: 0
zerosToNans: False
smoothLength: None
save_data: False
out_file_for_raw_data: None
maxPairedFragmentLength: 1000
Traceback (most recent call last):
  File "/home/anaconda3/bin/bamCoverage", line 12, in <module>
    main(args)
  File "/home/anaconda3/lib/python3.8/site-packages/deeptools/bamCoverage.py", line 256, in main
    wr.run(writeBedGraph.scaleCoverage, func_args, args.outFileName,
  File "/home/anaconda3/lib/python3.8/site-packages/deeptools/writeBedGraph.py", line 171, in run
    bedGraphToBigWig(chrom_names_and_size, [x[3] for x in res], out_file_name)
  File "/home/anaconda3/lib/python3.8/site-packages/deeptools/writeBedGraph.py", line 293, in bedGraphToBigWig
    bw.addHeader(chromSizes, maxZooms=10)
RuntimeError: Received an error while writing the bigWig header
[bwClose] There was an error while finishing writing a bigWig file! The output is likely truncated.

I also tried to use bamCoverage with bam files of which duplicates were not removed. However, it caused the same error. I guess that samtools argument may not be correct, but I do not know what it is.
Would you please help me know what I can try to fix it?

Thank you for your support.

Best

Log in to answer this question.