-
Notifications
You must be signed in to change notification settings - Fork 41
/
GL-DPPD-7101-F.md
2654 lines (1753 loc) · 109 KB
/
GL-DPPD-7101-F.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# GeneLab bioinformatics processing pipeline for Illumina RNA-sequencing data
> **This page holds an overview and instructions for how GeneLab processes RNAseq datasets. Exact processing commands, GL-DPPD-7101 version used, and processed data output files for specific datasets are provided in the [Open Science Data Repository (OSDR)](https://osdr.nasa.gov/bio/repo/).**
---
**Date:** August 18, 2022
**Revision:** F
**Document Number:** GL-DPPD-7101-F
**Submitted by:**
Jonathan Oribello (GeneLab Data Processing Team)
**Approved by:**
Amanda Saravia-Butler (GeneLab Data Processing Lead)
Sylvain Costes (GeneLab Project Manager)
Samrawit Gebre (GeneLab Deputy Project Manager and Interim GeneLab Configuration Manager)
Jonathan Galazka (GeneLab Project Scientist)
---
## Updates from previous version
Updated [Ensembl Reference Files](../../GeneLab_Reference_Annotations/Pipeline_GL-DPPD-7110_Versions/GL-DPPD-7110/GL-DPPD-7110_annotations.csv) now use:
- Animals: Ensembl release 107
- Plants: Ensembl plants release 54
- Bacteria: Ensembl bacteria release 54
The DESeq2 Normalization and DGE step, [step 9](#9-normalize-read-counts-perform-differential-gene-expression-analysis-and-add-gene-annotations-in-r), was modified as follows:
- A separate sub-step, [step 9a](#9a-create-sample-runsheet), was added to use the [dp_tools](https://github.com/J-81/dp_tools) program to create a runsheet containing all the metadata needed for running DESeq2, including ERCC spike-in status and sample grouping. This runsheet is imported in the DESeq2 script in place of parsing the ISA.zip file associated with the GLDS dataset.
- GeneLab now creates a custom reference annotation table as detailed in the [GeneLab_Reference_Annotations](../../GeneLab_Reference_Annotations) directory. The GeneLab Reference Annotation tables for each model organism created with [version GL-DPPD-7110](../../GeneLab_Reference_Annotations/Pipeline_GL-DPPD-7110_Versions/GL-DPPD-7110) is now imported in the DESeq2 script to add gene annotations in [step 9f](#9f-prepare-genelab-dge-tables-with-annotations-on-datasets-with-ercc-spike-in) and [step 9i](#9i-prepare-genelab-dge-tables-with-annotations-on-datasets-without-ercc-spike-in).
- Added the `ERCCnorm_SampleTable.csv` output file in [step 9g](#9g-export-genelab-dge-tables-with-annotations-for-datasets-with-ercc-spike-in) to indicate the samples used in the DESeq2 Normalization and DGE step for datasets with ERCC spike-in.
> Note: In most cases, the ERCCnorm_SampleTable.csv and SampleTable.csv files are the same. They will only differ when, for the ERCC-based analysis, samples are removed due to a lack of detectable Group B ERCC spike-in genes.
- Fixed edge case where `contrasts.csv` and `ERCCnorm_contrasts.csv` table header and rows could become out of sync with each other in [step 9c](#9c-configure-metadata-sample-grouping-and-group-comparisons) and [step 9e](#9e-perform-dge-on-datasets-with-ercc-spike-in) by generating rows from header rather than generating both separately.
- Updated R version from 4.1.2 to 4.1.3.
- Fixed edge case where certain scripts would crash if sample names were prefixes of other sample names. This had affected [step 4c](#4c-tablulate-star-counts-in-r), [step 8c](#8c-calculate-total-number-of-genes-expressed-per-sample-in-r), and [step 9d](#9d-import-rsem-genecounts).
- Fixed rare edge case where groupwise mean and standard deviations could become misassociated to incorrect groups. This had affected [step 9f](#9f-prepare-genelab-dge-tables-with-annotations-on-datasets-with-ercc-spike-in) and [step 9i](#9i-prepare-genelab-dge-tables-with-annotations-on-datasets-without-ercc-spike-in).
---
# Table of contents
- [**Software used**](#software-used)
- [**General processing overview with example commands**](#general-processing-overview-with-example-commands)
- [**1. Raw Data QC**](#1-raw-data-qc)
- [1a. Raw Data QC](#1a-raw-data-qc)
- [1b. Compile Raw Data QC](#1b-compile-raw-data-qc)
- [**2. Trim/Filter Raw Data and Trimmed Data QC**](#2-trimfilter-raw-data-and-trimmed-data-qc)
- [2a. Trim/Filter Raw Data](#2a-trimfilter-raw-data)
- [2b. Trimmed Data QC](#2b-trimmed-data-qc)
- [2c. Compile Trimmed Data QC](#2c-compile-trimmed-data-qc)
- [**3. Build STAR Reference**](#3-build-star-reference)
- [**4. Align Reads to Reference Genome then Sort and Index**](#4-align-reads-to-reference-genome-then-sort-and-index)
- [4a. Align Reads to Reference Genome with STAR](#4a-align-reads-to-reference-genome-with-star)
- [4b. Compile Alignment Logs](#4b-compile-alignment-logs)
- [4c. Tablulate STAR Counts in R](#4c-tablulate-star-counts-in-r)
- [4d. Sort Aligned Reads](#4d-sort-aligned-reads)
- [4e. Index Sorted Aligned Reads](#4e-index-sorted-aligned-reads)
- [**5. Create Reference BED File**](#5-create-reference-bed-file)
- [5a. Convert GTF to genePred File](#5a-convert-gtf-to-genepred-file)
- [5b. Convert genePred to BED File](#5b-convert-genepred-to-bed-file)
- [**6. Assess Strandedness, GeneBody Coverage, Inner Distance, and Read Distribution with RSeQC**](#6-assess-strandedness-genebody-coverage-inner-distance-and-read-distribution-with-rseqc)
- [6a. Determine Read Strandedness](#6a-determine-read-strandedness)
- [6b. Compile Strandedness Reports](#6b-compile-strandedness-reports)
- [6c. Evaluate GeneBody Coverage](#6c-evaluate-genebody-coverage)
- [6d. Compile GeneBody Coverage Reports](#6d-compile-genebody-coverage-reports)
- [6e. Determine Inner Distance (For Paired End Datasets)](#6e-determine-inner-distance-for-paired-end-datasets-only)
- [6f. Compile Inner Distance Reports](#6f-compile-inner-distance-reports)
- [6g. Assess Read Distribution](#6g-assess-read-distribution)
- [6h. Compile Read Distribution Reports](#6h-compile-read-distribution-reports)
- [**7. Build RSEM Reference**](#7-build-rsem-reference)
- [**8. Quantitate Aligned Reads**](#8-quantitate-aligned-reads)
- [8a. Count Aligned Reads with RSEM](#8a-count-aligned-reads-with-rsem)
- [8b. Compile RSEM Count Logs](#8b-compile-rsem-count-logs)
- [8c. Calculate Total Number of Genes Expressed Per Sample in R](#8c-calculate-total-number-of-genes-expressed-per-sample-in-r)
- [**9. Normalize Read Counts, Perform Differential Gene Expression Analysis, and Add Gene Annotations in R**](#9-normalize-read-counts-perform-differential-gene-expression-analysis-and-add-gene-annotations-in-r)
- [9a. Create Sample RunSheet](#9a-create-sample-runsheet)
- [9b. Environment Set Up](#9b-environment-set-up)
- [9c. Configure Metadata, Sample Grouping, and Group Comparisons](#9c-configure-metadata-sample-grouping-and-group-comparisons)
- [9d. Import RSEM GeneCounts](#9d-import-rsem-genecounts)
- [9e. Perform DGE on Datasets With ERCC Spike-In](#9e-perform-dge-on-datasets-with-ercc-spike-in)
- [9f. Prepare GeneLab DGE Tables with Annotations on Datasets With ERCC Spike-In](#9f-prepare-genelab-dge-tables-with-annotations-on-datasets-with-ercc-spike-in)
- [9g. Export GeneLab DGE Tables with Annotations for Datasets With ERCC Spike-In](#9g-export-genelab-dge-tables-with-annotations-for-datasets-with-ercc-spike-in)
- [9h. Perform DGE on Datasets Without ERCC Spike-In](#9h-perform-dge-on-datasets-without-ercc-spike-in)
- [9i. Prepare GeneLab DGE Tables with Annotations on Datasets Without ERCC Spike-In](#9i-prepare-genelab-dge-tables-with-annotations-on-datasets-without-ercc-spike-in)
- [9j. Export GeneLab DGE Tables with Annotations for Datasets Without ERCC Spike-In](#9j-export-genelab-dge-tables-with-annotations-for-datasets-without-ercc-spike-in)
- [**10. Evaluate ERCC Spike-In Data**](#10-evaluate-ercc-spike-in-data)
- [10a. Evaluate ERCC Count Data in Python](#10a-evaluate-ercc-count-data-in-python)
- [10b. Perform DESeq2 Analysis of ERCC Counts in R](#10b-perform-deseq2-analysis-of-ercc-counts-in-r)
- [10c. Analyze ERCC DESeq2 Results in Python](#10c-analyze-ercc-deseq2-results-in-python)
---
# Software used
|Program|Version|Relevant Links|
|:------|:------:|:-------------|
|FastQC|0.11.9|[https://www.bioinformatics.babraham.ac.uk/projects/fastqc/](https://www.bioinformatics.babraham.ac.uk/projects/fastqc/)|
|MultiQC|1.12|[https://multiqc.info/](https://multiqc.info/)|
|Cutadapt|3.7|[https://cutadapt.readthedocs.io/en/stable/](https://cutadapt.readthedocs.io/en/stable/)|
|TrimGalore!|0.6.7|[https://www.bioinformatics.babraham.ac.uk/projects/trim_galore/](https://www.bioinformatics.babraham.ac.uk/projects/trim_galore/)|
|STAR|2.7.10a|[https://github.com/alexdobin/STAR](https://github.com/alexdobin/STAR)|
|RSEM|1.3.1|[https://github.com/deweylab/RSEM](https://github.com/deweylab/RSEM)|
|Samtools|1.15|[http://www.htslib.org/](http://www.htslib.org/)|
|gtfToGenePred|377|[http://hgdownload.cse.ucsc.edu/admin/exe/](http://hgdownload.cse.ucsc.edu/admin/exe/)|
|genePredToBed|377|[http://hgdownload.cse.ucsc.edu/admin/exe/](http://hgdownload.cse.ucsc.edu/admin/exe/)|
|infer_experiment|4.0.0|[http://rseqc.sourceforge.net/#infer-experiment-py](http://rseqc.sourceforge.net/#infer-experiment-py)|
|geneBody_coverage|4.0.0|[http://rseqc.sourceforge.net/#genebody-coverage-py](http://rseqc.sourceforge.net/#genebody-coverage-py)|
|inner_distance|4.0.0|[http://rseqc.sourceforge.net/#inner-distance-py](http://rseqc.sourceforge.net/#inner-distance-py)|
|read_distribution|4.0.0|[http://rseqc.sourceforge.net/#read-distribution-py](http://rseqc.sourceforge.net/#read-distribution-py)|
|R|4.1.3|[https://www.r-project.org/](https://www.r-project.org/)|
|Bioconductor|3.14.0|[https://bioconductor.org](https://bioconductor.org)|
|DESeq2|1.34|[https://bioconductor.org/packages/release/bioc/html/DESeq2.html](https://bioconductor.org/packages/release/bioc/html/DESeq2.html)|
|tximport|1.27.1|[https://github.com/mikelove/tximport](https://github.com/mikelove/tximport)|
|tidyverse|1.3.1|[https://www.tidyverse.org](https://www.tidyverse.org)|
|stringr|1.4.1|[https://github.com/tidyverse/stringr](https://github.com/tidyverse/stringr)|
|dp_tools|1.1.8|[https://github.com/J-81/dp_tools](https://github.com/J-81/dp_tools)|
|pandas|1.5.0|[https://github.com/pandas-dev/pandas](https://github.com/pandas-dev/pandas)|
|seaborn|0.12.0|[https://seaborn.pydata.org/](https://seaborn.pydata.org/)|
|matplotlib|3.6.0|[https://matplotlib.org/stable](https://matplotlib.org/stable)|
|jupyter notebook|6.4.12|[https://jupyter-notebook.readthedocs.io/](https://jupyter-notebook.readthedocs.io/)|
|numpy|1.23.3|[https://numpy.org/](https://numpy.org/)|
|scipy|1.9.1|[https://scipy.org/](https://scipy.org/)|
|singularity|3.9|[https://sylabs.io/](https://sylabs.io/)|
---
# General processing overview with example commands
> Exact processing commands for specific datasets are provided in the [GLDS_Processing_Scripts](../GLDS_Processing_Scripts) directory.
>
> All output files marked with a \# are published for each RNAseq processed dataset in the [GLDS repository](https://genelab-data.ndc.nasa.gov/genelab/projects).
---
## 1. Raw Data QC
<br>
### 1a. Raw Data QC
```bash
fastqc -o /path/to/raw_fastqc/output/directory *.fastq.gz
```
**Parameter Definitions:**
- `-o` – the output directory to store results
- `*.fastq.gz` – the input reads are specified as a positional argument, and can be given all at once with wildcards like this, or as individual arguments with spaces inbetween them
**Input Data:**
- *fastq.gz (raw reads)
**Output Data:**
- *fastqc.html (FastQC report)
- *fastqc.zip (FastQC data)
<br>
### 1b. Compile Raw Data QC
```bash
multiqc --interactive -n raw_multiqc -o /path/to/raw_multiqc/output/directory /path/to/directory/containing/raw_fastqc/files
```
**Parameter Definitions:**
- `--interactive` - force reports to use interactive plots
- `-n` - prefix name for output files
- `-o` – the output directory to store results
- `/path/to/directory/containing/raw_fastqc/files` – the directory holding the output data from the fastqc run, provided as a positional argument
**Input Data:**
- *fastqc.zip (FastQC data, output from [Step 1a](#1a-raw-data-qc))
**Output Data:**
- raw_multiqc.html\# (multiqc report)
- /raw_multiqc_data\# (directory containing multiqc data)
<br>
---
## 2. Trim/Filter Raw Data and Trimmed Data QC
<br>
### 2a. Trim/Filter Raw Data
```bash
trim_galore --gzip \
--path_to_cutadapt /path/to/cutadapt \
--cores NumberOfThreads \
--phred33 \
--illumina \ # if adapters are not illumina, replace with adapters used
--output_dir /path/to/TrimGalore/output/directory \
--paired \ # only for PE studies, remove this parameter if raw data are SE
sample1_R1_raw.fastq.gz sample1_R2_raw.fastq.gz sample2_R1_raw.fastq.gz sample2_R2_raw.fastq.gz
# if SE, replace the last line with only the forward reads (R1) of each sample
```
**Parameter Definitions:**
- `--gzip` – compress the output files with `gzip`
- `--path_to_cutadapt` - specify path to cutadapt software if it is not in your `$PATH`
- `--cores` - specify the number of threads available on the server node to perform trimming
- `--phred33` - instructs cutadapt to use ASCII+33 quality scores as Phred scores for quality trimming
- `--illumina` - defines the adapter sequence to be trimmed as the first 13bp of the Illumina universal adapter `AGATCGGAAGAGC`
- `--output_dir` - the output directory to store results
- `--paired` - indicates paired-end reads - both reads, forward (R1) and reverse (R2) must pass length threshold or else both reads are removed
- `sample1_R1_raw.fastq.gz sample1_R2_raw.fastq.gz sample2_R1_raw.fastq.gz sample2_R2_raw.fastq.gz` – the input reads are specified as a positional argument, paired-end read files are listed pairwise such that the forward reads (*R1_raw.fastq.gz) are immediately followed by the respective reverse reads (*R2_raw.fastq.gz) for each sample
**Input Data:**
- *fastq.gz (raw reads)
**Output Data:**
- *fastq.gz\# (trimmed reads)
- *trimming_report.txt\# (trimming report)
<br>
### 2b. Trimmed Data QC
```bash
fastqc -o /path/to/trimmed_fastqc/output/directory *.fastq.gz
```
**Parameter Definitions:**
- `-o` – the output directory to store results
- `*.fastq.gz` – the input reads are specified as a positional argument, and can be given all at once with wildcards like this, or as individual arguments with spaces inbetween them
**Input Data:**
- *fastq.gz (trimmed reads, output from [Step 2a](#2a-trimfilter-raw-data))
**Output Data:**
- *fastqc.html (FastQC report)
- *fastqc.zip (FastQC data)
<br>
### 2c. Compile Trimmed Data QC
```bash
multiqc --interactive -n trimmed_multiqc -o /path/to/trimmed_multiqc/output/directory /path/to/directory/containing/trimmed_fastqc/files
```
**Parameter Definitions:**
- `--interactive` - force reports to use interactive plots
- `-n` - prefix name for output files
- `-o` – the output directory to store results
- `/path/to/directory/containing/trimmed_fastqc/files` – the directory holding the output data from the fastqc run, provided as a positional argument
**Input Data:**
- *fastqc.zip (FastQC data, output from [Step 2b](#2b-trimmed-data-qc))
**Output Data:**
- trimmed_multiqc.html\# (multiqc report)
- /trimmed_multiqc_data\# (directory containing multiqc data)
<br>
---
## 3. Build STAR Reference
```bash
STAR --runThreadN NumberOfThreads \
--runMode genomeGenerate \
--limitGenomeGenerateRAM 55000000000 \
--genomeSAindexNbases 14 \
--genomeDir /path/to/STAR/genome/directory \
--genomeFastaFiles /path/to/genome/fasta/file \
--sjdbGTFfile /path/to/annotation/gtf/file \
--sjdbOverhang ReadLength-1
```
**Parameter Definitions:**
- `--runThreadN` – number of threads available on server node to create STAR reference
- `--runMode` - instructs STAR to run genome indices generation job
- `--limitGenomeGenerateRAM` - maximum RAM available (in bytes) to generate STAR reference, at least 35GB are needed for mouse and the example above shows 55GB
- `--genomeSAindexNbases` - length (in bases) of the SA pre-indexing string, usually between 10 and 15. Longer strings require more memory but allow for faster searches. This value should be scaled down for smaller genomes (like bacteria) to min(14, log2(GenomeLength)/2 - 1). For example, for a 1 megaBase genome this value would be 9.
- `--genomeDir` - specifies the path to the directory where the STAR reference will be stored. At least 100GB of available disk space is required for mammalian genomes.
- `--genomeFastaFiles` - specifies one or more fasta file(s) containing the genome reference sequences
- `--sjdbGTFfile` – specifies the file(s) containing annotated transcripts in the standard gtf format
- `--sjdbOverhang` - indicates the length of the genomic sequence around the annotated junction to be used in constructing the splice junctions database. The length should be one less than the maximum length of the reads.
**Input Data:**
- *.fasta (genome sequence, this scRCP version uses the Ensembl fasta file indicated in the `fasta` column of the [GL-DPPD-7110_annotations.csv](../../GeneLab_Reference_Annotations/Pipeline_GL-DPPD-7110_Versions/GL-DPPD-7110/GL-DPPD-7110_annotations.csv) GeneLab Annotations file)
- *.gtf (genome annotation, this scRCP version uses the Ensembl gtf file indicated in the `gtf` column of the [GL-DPPD-7110_annotations.csv](../../GeneLab_Reference_Annotations/Pipeline_GL-DPPD-7110_Versions/GL-DPPD-7110/GL-DPPD-7110_annotations.csv) GeneLab Annotations file)
**Output Data:**
STAR genome reference, which consists of the following files:
- chrLength.txt
- chrNameLength.txt
- chrName.txt
- chrStart.txt
- exonGeTrInfo.tab
- exonInfo.tab
- geneInfo.tab
- Genome
- genomeParameters.txt
- SA
- SAindex
- sjdbInfo.txt
- sjdbList.fromGTF.out.tab
- sjdbList.out.tab
- transcriptInfo.tab
<br>
---
## 4. Align Reads to Reference Genome then Sort and Index
<br>
### 4a. Align Reads to Reference Genome with STAR
```bash
STAR --twopassMode Basic \
--limitBAMsortRAM 65000000000 \
--genomeDir /path/to/STAR/genome/directory \
--outSAMunmapped Within \
--outFilterType BySJout \
--outSAMattributes NH HI AS NM MD MC \
--outFilterMultimapNmax 20 \
--outFilterMismatchNmax 999 \
--outFilterMismatchNoverReadLmax 0.04 \
--alignIntronMin 20 \
--alignIntronMax 1000000 \
--alignMatesGapMax 1000000 \ # for PE only
--alignSJoverhangMin 8 \
--alignSJDBoverhangMin 1 \
--sjdbScore 1 \
--readFilesCommand zcat \
--runThreadN NumberOfThreads \
--outSAMtype BAM SortedByCoordinate \
--quantMode TranscriptomeSAM GeneCounts \
--outSAMheaderHD @HD VN:1.4 SO:coordinate \
--outFileNamePrefix /path/to/STAR/output/directory/<sample_id> \
--readFilesIn /path/to/trimmed_forward_reads \
/path/to/trimmed_reverse_reads # only needed for PE studies
```
**Parameter Definitions:**
- `--twopassMode` – specifies 2-pass mapping mode; the `Basic` option instructs STAR to perform the 1st pass mapping, then automatically extract junctions, insert them into the genome index, and re-map all reads in the 2nd mapping pass
- `--limitBAMsortRAM` - maximum RAM available (in bytes) to sort the bam files, the example above indicates 65GB
- `--genomeDir` - specifies the path to the directory where the STAR reference is stored
- `--outSAMunmapped` - specifies output of unmapped reads in the sam format; the `Within` option instructs STAR to output the unmapped reads within the main sam file
- `--outFilterType` - specifies the type of filtering; the `BySJout` option instructs STAR to keep only those reads that contain junctions that passed filtering in the SJ.out.tab output file
- `--outSAMattributes` - list of desired sam attributes in the order desired for the output sam file; sam attribute descriptions can be found [here](https://samtools.github.io/hts-specs/SAMtags.pdf)
- `--outFilterMultimapNmax` – specifies the maximum number of loci the read is allowed to map to; all alignments will be output only if the read maps to no more loci than this value
- `--outFilterMismatchNmax` - maximum number of mismatches allowed to be included in the alignment output
- `--outFilterMismatchNoverReadLmax` - ratio of mismatches to read length allowed to be included in the alignment output; the `0.04` value indicates that up to 4 mismatches are allowed per 100 bases
- `--alignIntronMin` - minimum intron size; a genomic gap is considered an intron if its length is equal to or greater than this value, otherwise it is considered a deletion
- `--alignIntronMax` - maximum intron size
- `--alignMatesGapMax` - maximum genomic distance (in bases) between two mates of paired-end reads; this option should be removed for single-end reads
- `--alignSJoverhangMin` - minimum overhang (i.e. block size) for unannotated spliced alignments
- `--alignSJDBoverhangMin` - minimum overhang (i.e. block size) for annotated spliced alignments
- `--sjdbScore` - additional alignment score for alignments that cross database junctions
- `--readFilesCommand` - specifies command needed to interpret input files; the `zcat` option indicates input files are compressed with gzip and zcat will be used to uncompress the gzipped input files
- `--runThreadN` - indicates the number of threads to be used for STAR alignment and should be set to the number of available cores on the server node
- `--outSAMtype` - specifies desired output format; the `BAM SortedByCoordinate` options specify that the output file will be sorted by coordinate and be in the bam format
- `--quantMode` - specifies the type(s) of quantification desired; the `TranscriptomeSAM` option instructs STAR to output a separate sam/bam file containing alignments to the transcriptome and the `GeneCounts` option instructs STAR to output a tab delimited file containing the number of reads per gene
- `--outSAMheaderHD` - indicates a header line for the sam/bam file
- `--outFileNamePrefix` - specifies the path to and prefix for the output file names; for GeneLab the prefix is the sample id
- `--readFilesIn` - path to input read 1 (forward read) and read 2 (reverse read); for paired-end reads, read 1 and read 2 should be separated by a space; for single-end reads only read 1 should be indicated
**Input Data:**
- STAR genome reference (output from [Step 3](#3-build-star-reference))
- *fastq.gz (trimmed reads, output from [Step 2a](#2a-trimfilter-raw-data))
**Output Data:**
- *Aligned.sortedByCoord.out.bam (sorted mapping to genome)
- *Aligned.toTranscriptome.out.bam\# (sorted mapping to transcriptome)
- *Log.final.out\# (log file containing alignment info/stats such as reads mapped, etc)
- *ReadsPerGene.out.tab (tab delimitated file containing STAR read counts per gene with 4 columns that correspond to different strandedness options: column 1 = gene ID, column 2 = counts for unstranded RNAseq, column 3 = counts for 1st read strand aligned with RNA, column 4 = counts for 2nd read strand aligned with RNA)
- *Log.out (main log file containing detailed info about the STAR run)
- *Log.progress.out (minute-by-minute report containing job progress statistics, such as the number of processed reads, % of mapped reads etc.)
- *SJ.out.tab\# (high confidence collapsed splice junctions in tab-delimited format)
- *_STARgenome (directory containing the following:)
- sjdbInfo.txt
- sjdbList.out.tab
- *_STARpass1 (directory containing the following:)
- Log.final.out
- SJ.out.tab
- *_STARtmp (directory containing the following:)
- BAMsort (directory containing subdirectories that are empty – this was the location for temp files that were automatically removed after successful completion)
<br>
### 4b. Compile Alignment Logs
```bash
multiqc --interactive -n align_multiqc -o /path/to/aligned_multiqc/output/directory /path/to/*Log.final.out/files
```
**Parameter Definitions:**
- `--interactive` - force reports to use interactive plots
- `-n` - prefix name for output files
- `-o` – the output directory to store results
- `/path/to/*Log.final.out/files` – the directory holding the *Log.final.out output files from the [STAR alignment step](#4a-align-reads-to-reference-genome-with-star), provided as a positional argument
**Input Data:**
- *Log.final.out (log file containing alignment info/stats such as reads mapped, etc., output from [Step 4a](#4a-align-reads-to-reference-genome-with-star))
**Output Data:**
- align_multiqc.html\# (multiqc report)
- /align_multiqc_data\# (directory containing multiqc data)
<br>
### 4c. Tablulate STAR Counts in R
```R
print("Make STAR counts table")
print("")
work_dir="/path/to/working/directory/where/script/is/executed/from" ## Must contain samples.txt file
align_dir="/path/to/directory/containing/STAR/counts/files"
setwd(file.path(work_dir))
### Pull in sample names where the "samples.txt" file is a single column list of sample names ###
study <- read.csv(Sys.glob(file.path(work_dir,"samples.txt")), header = FALSE, row.names = 1, stringsAsFactors = TRUE)
##### Import Data
ff <- list.files(file.path(align_dir), pattern = "ReadsPerGene.out.tab", recursive=TRUE, full.names = TRUE)
## Reorder the *genes.results files to match the ordering of the ISA samples
ff <- ff[sapply(rownames(study), function(x)grep(paste0(align_dir, '/', x,'_ReadsPerGene.out.tab$'), ff, value=FALSE))]
# Remove the first 4 lines
counts.files <- lapply( ff, read.table, skip = 4 )
# Get counts aligned to either strand for unstranded data by selecting col 2, to the first (forward) strand by selecting col 3 or to the second (reverse) strand by selecting col 4
counts <- as.data.frame( sapply( counts.files, function(x) x[ , 3 ] ) )
# Add column and row names
colnames(counts) <- rownames(study)
row.names(counts) <- counts.files[[1]]$V1
##### Export unnormalized counts table
setwd(file.path(align_dir))
write.csv(counts,file='STAR_Unnormalized_Counts.csv')
## print session info ##
print("Session Info below: ")
print("")
sessionInfo()
```
**Input Data:**
- samples.txt (A newline delimited list of sample IDs)
- *ReadsPerGene.out.tab (STAR counts per gene, output from [Step 4a](#4a-align-reads-to-reference-genome-with-star))
**Output Data:**
- STAR_Unnormalized_Counts.csv\# (Table containing raw STAR counts for each sample)
<br>
### 4d. Sort Aligned Reads
```bash
samtools sort -m 3G \
--threads NumberOfThreads \
-o /path/to/*Aligned.sortedByCoord_sorted.out.bam \
/path/to/*Aligned.sortedByCoord.out.bam
```
**Parameter Definitions:**
- `-m` - memory available per thread, `3G` indicates 3 gigabytes, this can be changed based on user resources
- `--threads` - number of threads available on server node to sort genome alignment files
- `/path/to/*Aligned.sortedByCoord.out.bam` – path to the *Aligned.sortedByCoord.out.bam output files from the [STAR alignment step](#4a-align-reads-to-reference-genome-with-star), provided as a positional argument
**Input Data:**
- *Aligned.sortedByCoord.out.bam (sorted mapping to genome file, output from [Step 4a](#4a-align-reads-to-reference-genome-with-star))
**Output Data:**
- *Aligned.sortedByCoord_sorted.out.bam\# (samtools sorted genome aligned bam file)
<br>
### 4e. Index Sorted Aligned Reads
```bash
samtools index -@ NumberOfThreads /path/to/*Aligned.sortedByCoord_sorted.out.bam
```
**Parameter Definitions:**
- `-@` - number of threads available on server node to index the sorted alignment files
- `/path/to/*Aligned.sortedByCoord_sorted.out.bam` – the path to the sorted *Aligned.sortedByCoord_sorted.out.bam output files from the [step 4d](#4d-sort-aligned-reads), provided as a positional argument
**Input Data:**
- *Aligned.sortedByCoord_sorted.out.bam (sorted mapping to genome file, output from [Step 4d](#4d-sort-aligned-reads))
**Output Data:**
- *Aligned.sortedByCoord_sorted.out.bam.bai\# (index of sorted mapping to genome file)
<br>
---
## 5. Create Reference BED File
<br>
### 5a. Convert GTF to genePred File
```bash
gtfToGenePred /path/to/annotation/gtf/file \
/path/to/output/genePred/file
```
**Parameter Definitions:**
- `/path/to/annotation/gtf/file` – specifies the file(s) containing annotated reference transcripts in the standard gtf format, provided as a positional argument
- `/path/to/output/genePred/file` – specifies the location and name of the output genePred file(s), provided as a positional argument
**Input Data:**
- *.gtf (genome annotation, this scRCP version uses the Ensembl gtf file indicated in the `gtf` column of the [GL-DPPD-7110_annotations.csv](../../GeneLab_Reference_Annotations/Pipeline_GL-DPPD-7110_Versions/GL-DPPD-7110/GL-DPPD-7110_annotations.csv) GeneLab Annotations file)
**Output Data:**
- *.genePred (genome annotation in genePred format)
<br>
### 5b. Convert genePred to BED File
```bash
genePredToBed /path/to/annotation/genePred/file \
/path/to/output/BED/file
```
**Parameter Definitions:**
- `/path/to/annotation/genePred/file` – specifies the file(s) containing annotated reference transcripts in the genePred format, provided as a positional argument
- `/path/to/output/BED/file` – specifies the location and name of the output BED file(s), provided as a positional argument
**Input Data:**
- *.genePred (genome annotation in genePred format, output from [Step 5a](#5a-convert-gtf-to-genepred-file))
**Output Data:**
- *.bed (genome annotation in BED format)
<br>
---
## 6. Assess Strandedness, GeneBody Coverage, Inner Distance, and Read Distribution with RSeQC
<br>
### 6a. Determine Read Strandedness
```bash
infer_experiment.py -r /path/to/annotation/BED/file \
-i /path/to/*Aligned.sortedByCoord_sorted.out.bam \
-s 15000000 > /path/to/*infer_expt.out
```
**Parameter Definitions:**
- `-r` – specifies the path to the reference annotation BED file
- `-i` - specifies the path to the input bam file(s)
- `-s` - specifies the number of reads to be sampled from the input bam file(s), 15M reads are sampled
- `>` - redirects standard output to specified file
- `/path/to/*infer_expt.out` - specifies the location and name of the file containing the infer_experiment standard output
**Input Data:**
- *.bed (genome annotation in BED format, output from [Step 5b](#5b-convert-genepred-to-bed-file))
- *Aligned.sortedByCoord_sorted.out.bam (sorted mapping to genome file, output from [Step 4d](#4d-sort-aligned-reads))
- *Aligned.sortedByCoord_sorted.out.bam.bai (index of sorted mapping to genome file, output from [Step 4e](#4e-index-sorted-aligned-reads), although not indicated in the command, this file must be present in the same directory as the respective \*Aligned.sortedByCoord_sorted.out.bam file)
**Output Data:**
- *infer_expt.out (file containing the infer_experiment standard output)
<br>
### 6b. Compile Strandedness Reports
```bash
multiqc --interactive -n infer_exp_multiqc -o /path/to/infer_exp_multiqc/output/directory /path/to/*infer_expt.out/files
```
**Parameter Definitions:**
- `--interactive` - force reports to use interactive plots
- `-n` - prefix name for output files
- `-o` – the output directory to store results
- `/path/to/*infer_expt.out/files` – the directory holding the *infer_expt.out output files from the [read strandedness step](#6a-determine-read-strandedness), provided as a positional argument
**Input Data:**
- *infer_expt.out (file containing the infer_experiment standard output, output from [Step 6a](#6a-determine-read-strandedness))
**Output Data:**
- infer_exp_multiqc.html\# (multiqc report)
- /infer_exp_multiqc_data\# (directory containing multiqc data)
<br>
### 6c. Evaluate GeneBody Coverage
```bash
geneBody_coverage.py -r /path/to/annotation/BED/file \
-i /path/to/*Aligned.sortedByCoord_sorted.out.bam \
-o /path/to/geneBody_coverage/output/directory/<sample_id>
```
**Parameter Definitions:**
- `-r` – specifies the path to the reference annotation BED file
- `-i` - specifies the path to the input bam file(s)
- `-o` - specifies the path to the output directory
- `/path/to/geneBody_coverage/output/directory/<sample_id>` - specifies the location and name of the directory containing the geneBody_coverage output files
**Input Data:**
- *.bed (genome annotation in BED format, output from [Step 5b](#5b-convert-genepred-to-bed-file))
- *Aligned.sortedByCoord_sorted.out.bam (sorted mapping to genome file, output from [Step 4d](#4d-sort-aligned-reads))
- *Aligned.sortedByCoord_sorted.out.bam.bai (index of sorted mapping to genome file, output from [Step 4e](#4e-index-sorted-aligned-reads), although not indicated in the command, this file must be present in the same directory as the respective \*Aligned.sortedByCoord_sorted.out.bam file)
**Output Data:**
- *.geneBodyCoverage.curves.pdf (genebody coverage line plot)
- *.geneBodyCoverage.r (R script that generates the genebody coverage line plot)
- *.geneBodyCoverage.txt (tab delimited file containing genebody coverage values used to generate the line plot)
<br>
### 6d. Compile GeneBody Coverage Reports
```bash
multiqc --interactive -n genebody_cov_multiqc -o /path/to/geneBody_coverage_multiqc/output/directory /path/to/geneBody_coverage/output/files
```
**Parameter Definitions:**
- `--interactive` - force reports to use interactive plots
- `-n` - prefix name for output files
- `-o` – the output directory to store results
- `/path/to/geneBody_coverage/output/files` – the directory holding the geneBody_coverage output files from [step 6c](#6c-evaluate-genebody-coverage), provided as a positional argument
**Input Data:**
- *.geneBodyCoverage.txt (tab delimited file containing genebody coverage values, output from [Step 6c](#6c-evaluate-genebody-coverage))
**Output Data:**
- geneBody_cov_multiqc.html\# (multiqc report)
- /geneBody_cov_multiqc_data\# (directory containing multiqc data)
<br>
### 6e. Determine Inner Distance (For Paired End Datasets ONLY)
```bash
inner_distance.py -r /path/to/annotation/BED/file \
-i /path/to/*Aligned.sortedByCoord_sorted.out.bam \
-k 15000000 \
-l -150 \
-u 350 \
-o /path/to/inner_distance/output/directory
```
**Parameter Definitions:**
- `-r` – specifies the path to the reference annotation BED file
- `-i` - specifies the path to the input bam file(s)
- `-k` - specifies the number of reads to be sampled from the input bam file(s), 15M reads are sampled
- `-l` - specifies the lower bound of inner distance (bp).
- `-u` - specifies the upper bound of inner distance (bp)
- `/path/to/inner_distance/output/directory` - specifies the location and name of the directory containing the inner_distance output files
**Input Data:**
- *.bed (genome annotation in BED format, output from [Step 5b](#5b-convert-genepred-to-bed-file))
- *Aligned.sortedByCoord_sorted.out.bam (sorted mapping to genome file, output from [Step 4d](#4d-sort-aligned-reads))
- *Aligned.sortedByCoord_sorted.out.bam.bai (index of sorted mapping to genome file, output from [Step 4e](#4e-index-sorted-aligned-reads), although not indicated in the command, this file must be present in the same directory as the respective \*Aligned.sortedByCoord_sorted.out.bam file)
**Output Data:**
- *.inner_distance.txt (log of read-wise inner distance results)
- *.inner_distance_freq.txt (tab delimited table of inner distances mapped to number of reads with that distance)
- *.inner_distance_plot.pdf (histogram plot of inner distance distribution)
- *.inner_distance_plot.r (R script that generates the histogram plot)
<br>
### 6f. Compile Inner Distance Reports
```bash
multiqc --interactive -n inner_dist_multiqc /path/to/inner_dist_multiqc/output/directory /path/to/inner_dist/output/files
```
**Parameter Definitions:**
- `--interactive` - force reports to use interactive plots
- `-n` - prefix name for output files
- `-o` – the output directory to store results
- `/path/to/inner_dist/output/files` – the directory holding the inner_distance output files from [Step 6e](#6e-determine-inner-distance-for-paired-end-datasets-only), provided as a positional argument
**Input Data:**
- *.inner_distance_freq.txt (tab delimited table of inner distances from [step 6e](#6e-determine-inner-distance-for-paired-end-datasets-only))
**Output Data:**
- inner_distance_multiqc.html\# (multiqc report)
- /inner_distance_multiqc_data\# (directory containing multiqc data)
<br>
### 6g. Assess Read Distribution
```bash
read_distribution.py -r /path/to/annotation/BED/file \
-i /path/to/*Aligned.sortedByCoord_sorted.out.bam > /path/to/*read_dist.out
```
**Parameter Definitions:**
- `-r` – specifies the path to the reference annotation BED file
- `-i` - specifies the path to the input bam file(s)
- `>` - redirects standard output to specified file
- `/path/to/*read_dist.out` - specifies the location and name of the file containing the read_distribution standard output
**Input Data:**
- *.bed (genome annotation in BED format, output from [Step 5b](#5b-convert-genepred-to-bed-file))
- *Aligned.sortedByCoord_sorted.out.bam (sorted mapping to genome file, output from [Step 4d](#4d-sort-aligned-reads))
- *Aligned.sortedByCoord_sorted.out.bam.bai (index of sorted mapping to genome file, output from [Step 4e](#4e-index-sorted-aligned-reads), although not indicated in the command, this file must be present in the same directory as the respective \*Aligned.sortedByCoord_sorted.out.bam file)
**Output Data:**
- *read_dist.out (file containing the read distribution standard output)
<br>
### 6h. Compile Read Distribution Reports
```bash
multiqc --interactive -n read_dist_multiqc -o /path/to/read_dist_multiqc/output/directory /path/to/*read_dist.out/files
```
**Parameter Definitions:**
- `--interactive` - force reports to use interactive plots
- `-n` - prefix name for output files
- `-o` – the output directory to store results
- `/path/to/*read_dist.out/files` – the directory holding the *read_dist.out output files from [Step 6g](#6g-assess-read-distribution) provided as a positional argument
**Input Data:**
- *read_dist.out (files containing the read_distribution standard output, output from [Step 6g](#6g-assess-read-distribution))
**Output Data:**
- read_dist_multiqc.html\# (multiqc report)
- /read_dist_multiqc_data\# (directory containing multiqc data)
<br>
---
## 7. Build RSEM Reference
```bash
rsem-prepare-reference --gtf /path/to/annotation/gtf/file \
/path/to/genome/fasta/file \
/path/to/RSEM/genome/directory/RSEM_ref_prefix
```
**Parameter Definitions:**
- `--gtf` – specifies the file(s) containing annotated transcripts in the standard gtf format
- `/path/to/genome/fasta/file` – specifies one or more fasta file(s) containing the genome reference sequences, provided as a positional argument
- `/path/to/RSEM/genome/directory/RSEM_ref_prefix` - specifies the path to the directory where the RSEM reference will be stored and the prefix desired for the RSEM reference files, provided as a positional argument
**Input Data:**
- *.fasta (genome sequence, this scRCP version uses the Ensembl fasta file indicated in the `fasta` column of the [GL-DPPD-7110_annotations.csv](../../GeneLab_Reference_Annotations/Pipeline_GL-DPPD-7110_Versions/GL-DPPD-7110/GL-DPPD-7110_annotations.csv) GeneLab Annotations file)
- *.gtf (genome annotation, this scRCP version uses the Ensembl gtf file indicated in the `gtf` column of the [GL-DPPD-7110_annotations.csv](../../GeneLab_Reference_Annotations/Pipeline_GL-DPPD-7110_Versions/GL-DPPD-7110/GL-DPPD-7110_annotations.csv) GeneLab Annotations file)
**Output Data:**
RSEM genome reference, which consists of the following files:
- RSEM_ref_prefix.chrlist
- RSEM_ref_prefix.grp
- RSEM_ref_prefix.idx.fa
- RSEM_ref_prefix.n2g.idx.fa
- RSEM_ref_prefix.seq
- RSEM_ref_prefix.ti
- RSEM_ref_prefix.transcripts.fa
<br>
---
## 8. Quantitate Aligned Reads
<br>
### 8a. Count Aligned Reads with RSEM
```bash
rsem-calculate-expression --num-threads NumberOfThreads \
--alignments \
--bam \
--paired-end \
--seed 12345 \
--seed-length 20 \
--estimate-rspd \
--no-bam-output \
--strandedness reverse|forward|none \
/path/to/*Aligned.toTranscriptome.out.bam \
/path/to/RSEM/genome/directory/RSEM_ref_prefix \
/path/to/RSEM/counts/output/directory/<sample_id>
```
**Parameter Definitions:**
- `--num-threads` – specifies the number of threads to use
- `--alignments` - indicates that the input file contains alignments in sam, bam, or cram format
- `--bam` - specifies that the input alignments are in bam format
- `--paired-end` - indicates that the input reads are paired-end reads; this option should be removed if the input reads are single-end
- `--seed` - the seed for the random number generators used in calculating posterior mean estimates and credibility intervals; must be a non-negative 32-bit integer
- `--seed-length 20` - instructs RSEM to ignore any aligned read if it or its mates' (for paired-end reads) length is less than 20bp
- `--estimate-rspd` - instructs RSEM to estimate the read start position distribution (rspd) from the data
- `--no-bam-output` - instructs RSEM not to output any bam file
- `--strandedness` - defines the strandedness of the RNAseq reads; the `reverse` option is used if read strandedness (output from [step 6](#6a-determine-read-strandedness)) is antisense, `forward` is used with sense strandedness, and `none` is used if strandedness is half sense half antisense
- `/path/to/*Aligned.toTranscriptome.out.bam` - specifies path to input bam files, provided as a positional argument
- `/path/to/RSEM/genome/directory/RSEM_ref_prefix` - specifies the path to the directory where the RSEM reference is stored and its prefix, provided as a positional argument
- `/path/to/RSEM/counts/output/directory` – specifies the path to and prefix for the output file names; for GeneLab the prefix is the sample id
**Input Data:**
- RSEM genome reference (output from [Step 7](#7-build-rsem-reference))
- *Aligned.toTranscriptome.out.bam (sorted mapping to transcriptome, output from [Step 4a](#4a-align-reads-to-reference-genome-with-star))
**Output Data:**
- *genes.results\# (counts per gene)
- *isoforms.results\# (counts per isoform)
- *stat (directory containing the following stats files)
- *cnt
- *model
- *theta
<br>
### 8b. Compile RSEM Count Logs
```bash
multiqc --interactive -n RSEM_count_multiqc -o /path/to/RSEM_count_multiqc/output/directory /path/to/*stat/files
```
**Parameter Definitions:**
- `--interactive` - force reports to use interactive plots
- `-n` - prefix name for output files
- `-o` – the output directory to store results
- `/path/to/*stat/files` – the directories holding the *stat output files from the [RSEM Counts step](#8a-count-aligned-reads-with-rsem), provided as a positional argument
**Input Data:**
- *stat (directory containing the following stats files, output from [Step 8a](#8a-count-aligned-reads-with-rsem))
- *cnt
- *model
- *theta
**Output Data:**
- RSEM_count_multiqc.html\# (multiqc report)
- /RSEM_count_multiqc_data\# (directory containing multiqc data)
<br>
### 8c. Calculate Total Number of Genes Expressed Per Sample in R
```R
library(tximport)
library(tidyverse)
work_dir="/path/to/working/directory/where/script/is/executed/from" ## Must contain samples.txt file
counts_dir="/path/to/directory/containing/RSEM/counts/files"
setwd(file.path(work_dir))
### Pull in sample names where the "samples.txt" file is a single column list of sample names ###
samples <- read.csv(Sys.glob(file.path(work_dir,"samples.txt")), header = FALSE, row.names = 1, stringsAsFactors = TRUE)
##### Import RSEM Gene Count Data
files <- list.files(file.path(counts_dir),pattern = ".genes.results", full.names = TRUE)
### reorder the genes.results files to match the ordering of the samples in the metadata file
files <- files[sapply(rownames(samples), function(x)grep(paste0(counts_dir, '/', x,'.genes.results$'), files, value=FALSE))]
names(files) <- rownames(samples)
txi.rsem <- tximport(files, type = "rsem", txIn = FALSE, txOut = FALSE)
##### Count the number of genes with non-zero counts for each sample
rawCounts <- txi.rsem$counts
NumNonZeroGenes <- (as.matrix(colSums(rawCounts > 0), row.names = 1))
colnames(NumNonZeroGenes) <- c("Number of genes with non-zero counts")
##### Export the number of genes with non-zero counts for each sample
setwd(file.path(counts_dir))
write.csv(NumNonZeroGenes,file='NumNonZeroGenes.csv')
## print session info ##
print("Session Info below: ")
print("")
sessionInfo()
```
**Input Data:**
- samples.txt (A newline delimited list of sample IDs)
- *genes.results (RSEM counts per gene, output from [Step 8a](#8a-count-aligned-reads-with-rsem))
**Output Data:**
- NumNonZeroGenes.csv (A samplewise table of the number of genes expressed)
<br>
---
## 9. Normalize Read Counts, Perform Differential Gene Expression Analysis, and Add Gene Annotations in R
<br>
### 9a. Create Sample RunSheet
> Note: Rather than running the command below to create the runsheet needed for processing, the runsheet may also be created manually by following the [file specification](../Workflow_Documentation/NF_RCP-F/examples/runsheet/README.md).
```bash
### Download the *ISA.zip file from the GeneLab Repository ###
dpt-get-isa-archive \
--accession GLDS-###
### Parse the metadata from the *ISA.zip file to create a sample runsheet ###
dpt-isa-to-runsheet --accession GLDS-### \