user3902509 user3902509 - 4 months ago 9
Bash Question

Shell, input file to use as prefix in other commands?

I'm newer to shell and am trying to adopt someone else's code for my own use.

I have a file called "sampleprefix.txt" that looks like this:

Sample1_A
Sample2_A
Sample3_B
Sample3_A
Sample4_C
Sample4_A


And I'd like to write a shell script that will read the sampleprefix.txt file line by line and use each line as my ${prefix} in the script.

I would like this shell script:

file ="sampleprefix.txt"
while read prefix ; do
merge ${prefix}.fastq
done < sampleprefix.txt #use ${prefix} as prefix:
VSEARCH="vsearch"
OUTPUT="combined_nc.fasta"
COMBINED="combined.fasta"

#Part I:
wc -l ${prefix}.fastq >> test.txt

#Part II:
"${VSEARCH}" --fastq_filter ${prefix}.fastq --fastaout ${prefix}.fasta
cat *.fasta > ${COMBINED}

#Part III:
"${VSEARCH}" --uchime_denovo ${COMBINED} --chimeras chimeras_denovo.fasta --nonchimeras ${OUTPUT} --uchimeout uchimeinfo_denovo


To output these three parts:

Part I:

wc -l Sample1_A.fastq >> test.txt
wc -l Sample2_A.fastq >> test.txt
...
wc -l Sample4_A.fastq >> test.txt
#Continually add wc -l for each file to test.txt


Part II:

vsearch --fastq_filter Sample1_A.fastq --fastaout Sample1_A.fasta
vsearch --fastq_filter Sample2_A.fastq --fastaout Sample2_A.fasta
...
vsearch --fastq_filter Sample4_A.fastq --fastaout Sample4_A.fasta
#again looping through each line to create a new file (.fasta) with the same prefixes

cat *.fasta > combined.fasta


Part III:

vsearch --uchime_denovo combined.fasta --chimeras chimeras_denovo.fasta --nonchimeras combined_nc.fasta --uchimeout uchimeinfo_denovo


I believe the annotations I have for parsing through the sampleprefix.txt file are incorrect. But i do not understand how to fix it.
The use of "vsearch" is not relevant, as I just need to figure out the syntax of going through the prefix.txt file in shell. vsearch is just a program I'm using. Please let me know if you have any suggestions.

Answer

Maybe I don't really get how it's supposed to work, but wouldn't moving the end of loop line to the end of the script just do it ? I mean

file ="sampleprefix.txt"
while read prefix ; do
  merge ${prefix}.fastq

VSEARCH="vsearch"
OUTPUT="combined_nc.fasta"
COMBINED="combined.fasta"

#Part I:
wc -l ${prefix}.fastq >> test.txt

#Part II:
"${VSEARCH}" --fastq_filter ${prefix}.fastq --fastaout ${prefix}.fasta
cat *.fasta > ${COMBINED}

#Part III:
"${VSEARCH}" --uchime_denovo ${COMBINED} --chimeras chimeras_denovo.fasta --nonchimeras ${OUTPUT} --uchimeout uchimeinfo_denovo

done < sampleprefix.txt  #use ${prefix} as prefix: