i5K-KINBRE-script-share/BAM_and_SAM_files_tools — pull request #2 by ZSlie (2014-06-10 21:59:25+00:00)
Title: Create Unmapped_Reads_In_a_Samfile.md

## Unmapped Reads in a Samfile

#### Part 1:
Filter SAM files to create file with only unmapped reads using the Samtools flag filter.

```
/homes/bioinfo/bioinfo_software/samtools-0.1.18/samtools view -S -f4 /homes/bioinfo/test_de_novo_DE/BT20_rep1_200.sam > ~/unmapped/BT20_rep1_200.unmapped.sam
```
Read more on flag [here](http://picard.sourceforge.net/explain-flags.html.).
Read more on samtools [here](http://samtools.sourceforge.net/samtools.shtml#3.).

#### Part 2:
Convert SAM to fastq
```
java -jar ~/bioinfo_software/picard-tools-1.112/SamToFastq.jar INPUT=/homes/bioinfo/unmapped/BT20_rep1_200.unmapped.sam FASTQ=/homes/bioinfo/unmapped/BT20_rep1_200.unmapped_1.fastq SECOND_END_FASTQ=/homes/bioinfo/unmapped/BT20_rep1_200.unmapped_2.fastq
```
Read more on picard [here](http://picard.sourceforge.net/).

###### Note:
Picard-tools-1.112 does not like relative paths so use the absolute path in filenames
e.g.
```
/homes/jfellers/unmapped/filename
```
rather than
```
"~/unmapped/filename"
```

#### Part 3:
Check for broken pairs
This simple script, broken_pair_removal.pl, finds pairs that have been broken and moves them to a separate file as single end reads. The script takes the fullpath of your forward and reverse reads followed by fullpath of your forward and reverse read output files.
```
perl /homes/sheltonj/abjc/scripts/broken_pair_removal.pl /homes/bioinfo/unmapped/BT20_rep1_200.unmapped_1.fastq /homes/bioinfo/unmapped/BT20_rep1_200.unmapped_2.fastq /homes/bioinfo/unmapped/BT20_rep1_200.unmapped_2_bp.fastq /homes/bioinfo/unmapped/BT20_rep1_200.unmapped_2_bp.fastq
```