<pre><code>#a bash script

#!/bin/bash


# Set your file paths and variables
raw_data_dir=&quot;~/rna_seq/raw_data&quot;
output_dir=&quot;~/rna_seq/results&quot;
genome=&quot;path/to/genome_index&quot;
qualimap_dir=&quot;../qualimap_v2.3&quot;  # Adjust as needed

# Create output directories
mkdir -p &quot;<span class="math-container">$output_dir/fastqc"
mkdir -p "$</span>output_dir/star&quot;
mkdir -p &quot;$output_dir/qualimap&quot;

# Loop through all paired-end FASTQ files
for fq1 in &quot;<span class="math-container">$raw_data_dir"/*_1.fastq.gz; do
    fq2="$</span>{fq1/_1.fastq.gz/_2.fastq.gz}&quot;  # Construct the second read file name

    # Run FastQC
    fastqc -t 8 -o &quot;<span class="math-container">$output_dir/fastqc" "$</span>fq1&quot; &quot;$fq2&quot;

    # Run STAR
    sample_name=<span class="math-container">$(basename "$</span>fq1&quot; _1.fastq.gz)
    align_out=&quot;<span class="math-container">$output_dir/star/$</span>{sample_name}_&quot;
    STAR --runThreadN 8 --genomeDir &quot;<span class="math-container">$genome" --readFilesIn "$</span>fq1&quot; &quot;<span class="math-container">$fq2" --outFileNamePrefix "$</span>align_out&quot; --outSAMtype BAM SortedByCoordinate --outSAMunmapped Within --outSAMattributes Standard

    # Run Qualimap
    qualimap_out=&quot;<span class="math-container">$output_dir/qualimap/$</span>{sample_name}&quot;
    &quot;<span class="math-container">$qualimap_dir/qualimap" rnaseq -outdir "$</span>qualimap_out&quot; -a proportional -bam &quot;<span class="math-container">${align_out}Aligned.sortedByCoord.out.bam" -gtf "$</span>gtf&quot; --java-mem-size=16G
done
</code></pre>

CC BY-SA 4.0
2024-06-04T03:09:48.750Z
It is the problem with the sample_name.  It is better to put the sample name as`${sra}`.  e.g. inside `01.data/${sra}_1.fastq.gz` or `01.data/${sra}_2.fastq.gz`  and then continue the for loop as `for sra in ${sra_list[@]}`
envs_h_gang_5
CC BY-SA 4.0
2024-06-04T03:16:21.377Z
It is the problem with the sample_name (or samplename).
envs_h_gang_5
CC BY-SA 4.0
2024-06-04T17:30:08.950Z
I made similar changes and it worked for me. yes the problem was with samplename. Thanks btw!
S_Malik
CC BY-SA 4.0
2024-06-03T06:10:03.130Z
2024-06-03T06:11:05.623Z
2024-06-03T06:11:05.623Z
three ii
<p>I am analyzing bulk RNA-seq data for Paired-End. I have separate scripts for fastqc, STAR &amp; qualimap but want to run them in a single script which looks like this</p>
<p>USAGE: sh rna-qc.sh &lt;path/to/sample_file/sample_R1.fastq&gt;</p>
<p>The script takes as input the R1 fastq file ONLY</p>
<pre><code>#!/bin/bash/

# initialize a variable to store the name of the input fastq file for Read1
fq1=$1

# grab the path information to use for loading the Read2 fastq file
path=`temp=$( realpath &quot;$fq1&quot; ) &amp;&amp; dirname &quot;$temp&quot;`

# get base of filename 
samplename=`basename $fq _1.fastq.gz`
echo &quot;Starting analysis of sample $samplename&quot;

# create a variable to store the read 2 file
fq2=<span class="math-container">${path}/$</span>{samplename}_2.fastq.gz

# directory with the genome index files + name of the gene annotation file
genome=~/star/genomeIndex/
gtf=~/star/

# create the whole path
mkdir -p results/fastqc/
mkdir -p results/STAR/
mkdir -p results/qualimap/

# set up output filenames and locations
fastqc_out=results/fastqc/
align_out=results/STAR/<span class="math-container">${samplename}
align_out_bam=results/STAR/$</span>{samplename}_Aligned.sortedByCoord.out.bam
qualimap_out=results/qualimap/${samplename}.qualimap

echo &quot;Processing file $fq&quot;

echo &quot;Starting QC for $samplename&quot;

# Run FastQC
fastqc -t 8 -o <span class="math-container">$fastqc_out $</span>fq1 $fq2

# Run STAR
STAR --runThreadN 8 --genomeDir <span class="math-container">$genome --readFilesIn $</span>fq1 <span class="math-container">$fq2 --outFileNamePrefix $</span>align_out --outSAMtype BAM SortedByCoordinate --outSAMunmapped Within --outSAMattributes Standard

# Run Qualimap
../qualimap_v2.3/qualimap rnaseq \
-outdir <span class="math-container">$qualimap_out \
-a proportional \
-bam $</span>align_out_bam \
-gtf $gtf \
-pe
--java-mem-size=16G
</code></pre>
<p>This script named <code>rna-qc.sh</code> is then run using following loop</p>
<pre><code>for fq in ~/rna_seq/raw_data/*_1.fastq.gz
do
rna-qc.sh $fq
done
</code></pre>
<p>then following is shown:</p>
<pre><code>Starting analysis of sample _1.fastq.gz
Processing file
Starting QC for _1.fastq.gz
Skipping '/home/saad/rna_seq/raw_data/_1.fastq.gz_2.fastq.gz' which didn't exist, or couldn't be read
Started analysis of ERR4231010_1.fastq.gz
Approx 5% complete for ERR4231010_1.fastq.gz
.......
</code></pre>
<p>so on and only fastQC analysis completes on files. Please guide where could be issue in naming?</p>
<p>Also I want to know what argument should be used for qualimap of PE samples? Thanks!</p>

CC BY-SA 4.0
2024-05-31T15:28:20.690Z
What does the STDERR say? Run it like so: `rna-qc.sh $fq 2>$fq.rna-qc.err`
Ram RS
CC BY-SA 4.0
2024-06-03T11:28:16.433Z
the problem was with the path, basename or realpath was not working when absolute path was given. following changes were made and it worked from same directory
```fq1="$1"
path=$(dirname "$fq1")
# get base of filename
samplename=$(basename "$fq1" _1.fastq.gz)
# create a variable to store the read 2 file
fq2="${path}/${samplename}_2.fastq.gz"```
S_Malik
CC BY-SA 4.0
2024-06-06T19:25:36.790Z
If you have fixed the problem yourself, please add your fix as an answer rather than a comment. If your fix is similar or identical to another answer, you can instead upvote and/or accept that answer.
gringer
<rna-seq><ngs><qc><loop>
RNA-seq QC and alignment error in script
# RNA-seq QC and alignment error in script

(Asked by: username_0 on 2024-05-31)

I am analyzing bulk RNA-seq data for Paired-End. I have separate scripts for fastqc, STAR & qualimap but want to run them in a single script which looks like this
USAGE: sh rna-qc.sh 
The script takes as input the R1 fastq file ONLY
#!/bin/bash/

# initialize a variable to store the name of the input fastq file for Read1
fq1=$1

# grab the path information to use for loading the Read2 fastq file
path=`temp=$( realpath "$fq1" ) && dirname "$temp"`

# get base of filename 
samplename=`basename $fq _1.fastq.gz`
echo "Starting analysis of sample $samplename"

# create a variable to store the read 2 file
fq2=${path}/${samplename}_2.fastq.gz

# directory with the genome index files + name of the gene annotation file
genome=~/star/genomeIndex/
gtf=~/star/

# create the whole path
mkdir -p results/fastqc/
mkdir -p results/STAR/
mkdir -p results/qualimap/

# set up output filenames and locations
fastqc_out=results/fastqc/
align_out=results/STAR/${samplename}
align_out_bam=results/STAR/${samplename}_Aligned.sortedByCoord.out.bam
qualimap_out=results/qualimap/${samplename}.qualimap

echo "Processing file $fq"

echo "Starting QC for $samplename"

# Run FastQC
fastqc -t 8 -o $fastqc_out $fq1 $fq2

# Run STAR
STAR --runThreadN 8 --genomeDir $genome --readFilesIn $fq1 $fq2 --outFileNamePrefix $align_out --outSAMtype BAM SortedByCoordinate --outSAMunmapped Within --outSAMattributes Standard

# Run Qualimap
../qualimap_v2.3/qualimap rnaseq \
-outdir $qualimap_out \
-a proportional \
-bam $align_out_bam \
-gtf $gtf \
-pe
--java-mem-size=16G

This script named rna-qc.sh is then run using following loop
for fq in ~/rna_seq/raw_data/*_1.fastq.gz
do
rna-qc.sh $fq
done

then following is shown:
Starting analysis of sample _1.fastq.gz
Processing file
Starting QC for _1.fastq.gz
Skipping '/home/saad/rna_seq/raw_data/_1.fastq.gz_2.fastq.gz' which didn't exist, or couldn't be read
Started analysis of ERR4231010_1.fastq.gz
Approx 5% complete for ERR4231010_1.fastq.gz
.......

so on and only fastQC analysis completes on files. Please guide where could be issue in naming?
Also I want to know what argument should be used for qualimap of PE samples? Thanks!

**Tags:** rna-seq, ngs, qc, loop
> **Comments:**
> **username_1**: What does the STDERR say? Run it like so: `rna-qc.sh $fq 2>$fq.rna-qc.err`  
> **username_0**: the problem was with the path, basename or realpath was not working when absolute path was given. following changes were made and it worked from same directory ```fq1="$1" path=$(dirname "$fq1") # get base of filename samplename=$(basename "$fq1" _1.fastq.gz) # create a variable to store the read 2 file fq2="${path}/${samplename}_2.fastq.gz"```  
> **username_2**: If you have fixed the problem yourself, please add your fix as an answer rather than a comment. If your fix is similar or identical to another answer, you can instead upvote and/or accept that answer.  

---

## 1 answers

### Answer by username_3 on 2024-06-03. Score: 1

#a bash script

#!/bin/bash


# Set your file paths and variables
raw_data_dir="~/rna_seq/raw_data"
output_dir="~/rna_seq/results"
genome="path/to/genome_index"
qualimap_dir="../qualimap_v2.3"  # Adjust as needed

# Create output directories
mkdir -p "$output_dir/fastqc"
mkdir -p "$output_dir/star"
mkdir -p "$output_dir/qualimap"

# Loop through all paired-end FASTQ files
for fq1 in "$raw_data_dir"/*_1.fastq.gz; do
    fq2="${fq1/_1.fastq.gz/_2.fastq.gz}"  # Construct the second read file name

    # Run FastQC
    fastqc -t 8 -o "$output_dir/fastqc" "$fq1" "$fq2"

    # Run STAR
    sample_name=$(basename "$fq1" _1.fastq.gz)
    align_out="$output_dir/star/${sample_name}_"
    STAR --runThreadN 8 --genomeDir "$genome" --readFilesIn "$fq1" "$fq2" --outFileNamePrefix "$align_out" --outSAMtype BAM SortedByCoordinate --outSAMunmapped Within --outSAMattributes Standard

    # Run Qualimap
    qualimap_out="$output_dir/qualimap/${sample_name}"
    "$qualimap_dir/qualimap" rnaseq -outdir "$qualimap_out" -a proportional -bam "${align_out}Aligned.sortedByCoord.out.bam" -gtf "$gtf" --java-mem-size=16G
done

> **Comments:**
> **username_4**: It is the problem with the sample_name.  It is better to put the sample name as`${sra}`.  e.g. inside `01.data/${sra}_1.fastq.gz` or `01.data/${sra}_2.fastq.gz`  and then continue the for loop as `for sra in ${sra_list[@]}`  
> **username_4**: It is the problem with the sample_name (or samplename).  
> **username_0**: I made similar changes and it worked for me. yes the problem was with samplename. Thanks btw!  

---

