This scripts takes a sam formatted file sorted by read name produced from the alignment of a interleave fastq file (that is read1/1 and read1/2 format). For each read pair it selects alignment that are close (<5000 kb, see line 22) in the same chromosome and it prints them out. It sets flags to pair both reads (see lines 31-45) and if alignment is correct it sets the pair as proper read (see line 48). If there are more than 1 alignment per read it set flag as 'secondary alignment' except for the one with the highest score (some of MAPQ scores of both reads)
bash -c "python interleave-fastq.py <( zcat fastq/150_R1.fastq.gz) <( zcat fastq/150_R2.fastq.gz)" | sed "s/ 1:N/\/1 1:N/" | sed "s/ 2:N/\/2 2:N/" | gmap -D /home/shared/ref_genome/Poplar/poplarv3_gsnap/ -d poplarv3_gsnap --nthreads=16 --nosplicing -B 5 -f samse -O --read-group-id=test --read-group-name=test /dev/stdin 2> /dev/null | python filter_sam_file.py | sambamba view --format bam /dev/stdin > test.bam
the interleave-fastq.py was obtained from