---
title: "33-Apul-lncRNA-matrix"
format: html
editor: visual
---

```{bash}
mkdir -p ../output/33-Apul-lncRNA-matrix
```

```{bash}
mkdir -p ../output/31-Apul-lncRNA-discovery
```

```{bash}
cd ../output/31-Apul-lncRNA-discovery

URL="https://gannet.fish.washington.edu/v1_web/owlshell/bu-github/timeseries_molecular/D-Apul/output/31-Apul-lncRNA-discovery/"

wget -r -np -nd -l1 -A "*sorted.bam,*sorted.bam.bai,*sorted.bai" -R "index.html*" "$URL"
```


```{bash}
cd ../output/31-Apul-lncRNA-discovery

URL="https://gannet.fish.washington.edu/v1_web/owlshell/bu-github/timeseries_molecular/D-Apul/output/31-Apul-lncRNA-discovery/"

wget -r -np -nd -l1 -A "lncRNA*" -R "index.html*" "$URL"
```


```{bash}
head ../output/31-Apul-lncRNA-discovery/lncRNA*
```

```{bash}
echo "Starting execution..."
pwd
echo "Current directory shown above"
```


```{bash}
/home/shared/subread-2.0.5-Linux-x86_64/bin/featureCounts \
  -T 42 \
  -O \
  --fraction \
  -a ../output/31-Apul-lncRNA-discovery/lncRNA.gtf \
  -o ../output/33-Apul-lncRNA-matrix/Apul_lncRNA_counts.txt \
  -t lncRNA \
  -g gene_id \
  -p \
../output/31-Apul-lncRNA-discovery/*sorted.bam
```

# Alternative approach: If the -F flag doesn't work as expected, 
# you can post-process the output file to rename columns
# This script extracts just the filename without the path
```{bash}
# Create a backup of the original file
cp ../output/33-Apul-lncRNA-matrix/Apul_lncRNA_counts.txt ../output/33-Apul-lncRNA-matrix/Apul_lncRNA_counts_original.txt

# Use awk to rename the columns (extract just the filename part)
awk 'BEGIN {OFS="\t"} 
     NR==1 {
       for(i=1; i<=NF; i++) {
         if(i<=6) {print $i}  # Keep the first 6 columns as-is
         else {
           # Extract just the filename from the path
           split($i, path_parts, "/")
           filename = path_parts[length(path_parts)]
           # Remove .sorted.bam extension
           gsub(/\.sorted\.bam$/, "", filename)
           printf "%s", filename
           if(i<NF) printf "\t"
         }
       }
       print ""
     }
     NR>1 {print $0}' ../output/33-Apul-lncRNA-matrix/Apul_lncRNA_counts_original.txt > ../output/33-Apul-lncRNA-matrix/Apul_lncRNA_counts.txt
```


```{bash}
head ../output/33-Apul-lncRNA-matrix/Apul_lncRNA_counts.txt
```

