#!/bin/bash
##  qsub -q batch velmakefa.sh
# not..
#PBS -N bamindx
#PBS -l mem=48gb,nodes=1:ppn=24,walltime=19:55:00
#PBS -o bamindx.$$.out
#PBS -e bamindx.$$.err
#PBS -V

bindir=/bio/bio-grid/mb/bin/

#ncpu=24
#workd=$HOME/scratch/chrs/cacao
#export scripts=$HOME/bio/evigene/scripts/rnaseq
#export bindir=$HOME/bio/bin
#cd $workd/rnas/bams7/

cutloc=scaffold_4:1-2000000
cutnam=sc4_2M

# env bamlist=xxxx qsub -q batch velmakefa.sh
#bamlist1=`ls ncgr{0909,0910}*.bam`
#bamlist2=`ls ncgr090[567]*.bam`
#bamlist3=`ls cgb*1-mars11s8p.bam`
#bamlist4=`ls cgb*1.bam`
bamlist4=`ls cgb*.bam`

bamlist=$bamlist4

for bam in $bamlist ; do {
 if [ -f $bam.bai ]; then 
   echo samtools view $bam $cutloc
   nam=`basename $bam .bam`
   $bindir/samtools view -b -o $nam.$cutnam.bam  $bam $cutloc > log.$nam$cutnam 2>&1
 fi
} done

exit
# wait

# Usage:   samtools view [options] <in.bam>|<in.sam> [region1 [...]]
# Options: -b       output BAM
#          -h       print header for the SAM output
#          -H       print header only (no alignments)
#          -S       input is SAM
#          -u       uncompressed BAM output (force -b)
#          -x       output FLAG in HEX (samtools-C specific)
#          -X       output FLAG in string (samtools-C specific)
#          -t FILE  list of reference names and lengths (force -S) [null]
#          -T FILE  reference sequence file (force -S) [null]
#          -o FILE  output file name [stdout]
#          -R FILE  list of read groups to be outputted [null]
#          -f INT   required flag, 0 for unset [0]
#          -F INT   filtering flag, 0 for unset [0]
#          -q INT   minimum mapping quality [0]
#          -l STR   only output reads in library STR [null]
#          -r STR   only output reads in read group STR [null]
#          -?       longer help
