Skip to content

Instantly share code, notes, and snippets.

@arq5x
Created March 17, 2011 14:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arq5x/874410 to your computer and use it in GitHub Desktop.
Save arq5x/874410 to your computer and use it in GitHub Desktop.
Grab random subset of FASTQ pairs
# Staring FASTQ files
export FQ1=1.fq
export FQ2=2.fq
# The names of the random subsets
export FQ1SUBSET=1.rand.fq
export FQ2SUBSET=2.rand.fq
# How many random pairs do we want?
export N=100
# (1) Use paste to merge the two into a single line
# (2) Use awk to "linearize" the two mates into a single record.
# Add a random number to the front of each line
# (3) Sort by the random number
# (4) Using head, grab the first N (randomly chosen, thanks to the awk above) records
# (5) Use awk to convert back to 2 separate FASTQ files.
paste $FQ1 $FQ2 | \
awk 'BEGIN{srand()}; {OFS="\t"; \
getline seqs; getline sep; getline quals; \
print rand(),$0,seqs,sep,quals}' | \
sort -k1,1 | \
head -n $N | \
awk '{OFS="\n"; \
print $2,$4,$6,$8 >> ENVIRON["FQ1SUBSET"]; \
print $3,$5,$7,$9 >> ENVIRON["FQ2SUBSET"]}'
@audy
Copy link

audy commented Mar 18, 2011

why do you use export?

just do

blah='asdf'
command "$asdf"

@arq5x
Copy link
Author

arq5x commented Mar 18, 2011

Ha! Good point. It's rote from experience in my "early days".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment