# Screen
* Screens the genome for ERV like regions by comparing the genome to a set of known retroviral ORFs using Exonerate.
* Confirms the Exonerate regions using UBLAST
* Finds and confirms ORFs within these regions
* Finds the most similar known retroviral ORF in the database to each of the newly identified ORFs


# Functions
1. [initiate](#initiate)<br>
2. [genomeToChroms](#genomeToChroms)<br>
3. [prepDBs](#prepDBs)<br>
4. [runExonerate](#runExonerate)<br>
5. [cleanExonerate](#cleanExonerate)<br>
6. [mergeOverlaps](#mergeOverlaps)<br>
7. [makeFastas](#makeFastas)<br>
8. [renameFastas](#renameFastas)<br>
9. [makeUBLASTDb](#makeUBLASTDb)<br>
10. [runUBLASTCheck](#runUBLASTCheck)<br>
11. [classifyWithExonerate](#classifyWithExonerate)<br>
12. [getORFs](#getORFs)<br>
13. [checkORFsUBLAST](#checkORFsUBLAST)<br>
14. [assignGroups](#assignGroups)<br>
15. [summariseScreen](#summariseScreen)<br>
16. [Screen](#Screen)<br>

## initiate<a name="initiate"></a>

**Input Files** <br>
`pipeline.ini`<br>

**Output Files**<br>
`init.txt`<br>

**Parameters**<br>
`[genome] file`<br>
`[paths] path_to_ERVsearch`<br>
`[paths] path_to_usearch`<br>
`[paths] path_to_exonerate`<br>

Initialises the pipeline and checks that the required parameters in the pipeline.ini are set and valid and that the required software is in your $PATH.

Checks that:
* The input genome file exists.
* The correct path to ERVsearch is provided.
* samtools, bedtools, FastTree and mafft are in the $PATH
* The correct paths to usearch and exonerate are provided.
 
`init.txt` is a placeholder to show that this step has been completed.


### genomeToChroms<a name="genomeToChroms"></a>

**Input Files** 
`[genome] file`<br>
`keep_chroms.txt`<br>

**Output Files**<br>
`host_chromosomes.dir/*fasta`<br>

**Parameters**<br>
`[genome] file`<br>
`[genomesplits] split`<br>
`[genomesplits] split_n`<br>
`[genomesplits] force`<br>

Splits the host genome provided by the user into FASTA files of a suitable size to run Exonerate efficiently.

If `genomesplits_split` in the pipeline.ini is False, the genome is split into one fasta file for each sequence - each chromosome, scaffold or contig.

If `genomesplits_split` in the pipeline.ini is True, the genome is split into the number of batches specified by the `genomesplits_splitn` parameter, unless the total number of sequences in the input file is less than this number.

The pipeline will fail if the number of sequences which would result from the genomesplits settings would result in >500 Exonerate runs, however it is possible to force the pipeline to run despite this by setting `genomesplits_force` to True.

If the file keep_chroms.txt exists in the working directory only chromosomes listed in this file will be kept.

An unzipped copy of zipped and gzipped fasta files will be created or a link to the file if it is already unzipped. this will be named `genome.fa` and be in the working directory.

This function generates a series of fasta files which are stored in the host_chromosomes.dir directory.

### prepDBs<a name="prepDBs"></a>

**Input Files**<br>
None<br>

**Output Files** <br>
`gene_databases.dir/GENE.fasta`<br>

**Parameters**<br>
`[database] use_custom_db`<br>
`[database] gag`<br>
`[database] pol`<br>
`[database] env`<br>

 Retrieves the gag, pol and env amino acid sequence database fasta files and puts a copy of each gene_databases.dir directory.
    
 If custom databases are used they are retrieved and named as gag.fasta  pol.fasta, env.fasta so the path doesn't need to be changed every time.

### runExonerate<a name="runExonerate"></a>

**Input Files** <br>
`gene_databases.dir/GENE.fasta`<br>
`host_chromosomes.dir/*fasta`<br>

**Output Files** <br>
`raw_exonerate_output.dir/GENE_*.tsv`<br>

**Parameters**<br>
`[paths] path_to_exonerate`<br>


Runs the `protein2dna` algorithm in the [Exonerate software package](https://www.ebi.ac.uk/about/vertebrate-genomics/software/exonerate-manual) with the host chromosomes (or other regions) in `host_chromosomes.dir` as target sequences and the FASTA files from prepDBs as the query sequences.

The raw output of Exonerate is stored in the raw_exonerate_output directory, one file is created for each combination of query and target sequences.

This step is carried out with low stringency as results are later filtered using UBLAST and Exonerate.
 
### cleanExonerate<a name="cleanExonerate"></a>

**Input Files** <br>
`raw_exonerate_output.dir/GENE_*.tsv`<br>

**Output_Files**<br>
`clean_exonerate_output.dir/GENE_*_unfiltered.tsv`<br>
`clean_exonerate_output.dir/GENE_*_filtered.tsv`<br>
`clean_exonerate_output.dir/GENE_*.bed`<br>

**Parameters**<br>
`[exonerate] min_hit_length`<br>

Filters and cleans up the Exonerate output.
* Converts the raw Exonerate output files into dataframes - GENE_unfiltered.tsv
* Filters out any regions containing introns (as defined by Exonerate)
* Filters out regions less than `exonerate_min_hit_length` on the host sequence (in nucleotides).
* Outputs the filtered regions to GENE_filtered.tsv
* Converts this to bed format and outputs this to GENE.bed

### mergeOverlaps<a name="mergeOverlaps"></a>

**Input Files** <br>
`clean_exonerate_output.dir/GENE_*.bed`<br>

**Output_Files** <br>
`gene_bed_files.dir/GENE_all.bed`,<br>
`gene_bed_files.dir/GENE_merged.bed`<br>

**Parameters**<br>
`[exonerate] overlap`<br>

Merges the output bed files for individual sections of the input genome into a single bed file.

Overlapping regions or very close together regions of the genome detected by Exonerate with similarity to the same retroviral gene are then merged into single regions.  This is performed using [bedtools merge](https://bedtools.readthedocs.io/en/latest/content/tools/merge.html) on the bed files output by cleanExonerate.

If there is a gap of less than `exonerate_overlap` between the regions they will be merged.

### makeFastas<a name="makeFastas"></a>

**Input Files**<br>
`gene_bed_files.dir/GENE_merged.bed`<br>
`genome.fa`<br>

**Output Files** <br>
`gene_fasta_files.dir/GENE_merged.fasta`<br>

**Parameters**<br>
None<br>

Fasta files are generated containing the sequences of the merged regions of the genome identified using mergeOverlaps.
These are extracted from the host chromosomes using [bedtools getfasta](https://bedtools.readthedocs.io/en/latest/content/tools/getfasta.html).

### renameFastas<a name="renameFastas"></a>

**Input Files** <br>
`gene_fasta_files.dir/GENE_merged.fasta`<br>

**Output Files** <br>
`gene_fasta_files.dir/GENE_merged_renamed.fasta`<br>

**Parameters**<br>
None<br>

 Renames the sequences in the fasta files of ERV-like regions identified with Exonerate so each record has a numbered unique ID (gag1, gag2 etc). Also removes ":" from sequence names as this causes problems later.
 
### makeUBLASTDb<a name="makeUBLASTDb"></a>

**Input Files**<br>
 `gene_databases.dir/GENE.fasta`<br>

**Output Files** <br>
`UBLAST_db.dir/GENE_db.udb`<br>

**Parameters**<br>
`[paths] path_to_ublast`<br>

[USEARCH](https://www.drive5.com/usearch/) requires an indexed database of query sequences to run. This function generates this database for the three gene amino acid fasta files used to screen the genome.

### runUBLASTCheck<a name="runUBLASTCheck"></a>

**Input Files**<br>
`UBLAST_db.dir/GENE_db.udb`<br>
`gene_fasta_files.dir/GENE_merged.fasta`<br>

**Output Files**<br>
`ublast.dir/GENE_UBLAST_alignments.txt`<br>
`ublast.dir/GENE_UBLAST.tsv`<br>
`ublast.dir/GENE_filtered_UBLAST.fasta`<br>

**Parameters**<br>
`[paths] path_to_usearch`<br>
`[usearch] min_id`<br>
`[usearch] min_hit_length`<br>
`[usearch] min_coverage`<br>

 ERV regions in the fasta files generated by makeFasta are compared to the ERV amino acid database files for a second time, this time using USEARCH (https://www.drive5.com/usearch/). Using both of these tools reduces the number of false positives.

This allows sequences with low similarity to known ERVs to be filtered out.  Similarity thresholds can be set in the pipeline.ini file (`usearch_min_id,` - minimum identity between query and target -  `usearch_min_hit_length` - minimum length of hit on target sequence -  and `usearch_min_coverage` - minimum proportion of the query sequence the hit should cover).

The raw output of running UBLAST against the target sequences is saved in GENE_UBLAST_alignments.txt (equivalent to the BLAST default output) and GENE_UBLAST.tsv (equivalent to the BLAST -outfmt 6 tabular output) this is already filtered by passing the appropriate parameters to UBLAST. The regions which passed the filtering and are therefore in these output files are then output to a FASTA file GENE_filtered_UBLAST.fasta.

### classifyWithExonerate<a name="classifyWithExonerate"></a>

**Input Files**<br>
`ublast.dir/GENE_filtered_UBLAST.fasta`<br>
`ERVsearch/ERV_db/all_ERVs_nt.fasta`<br>

**Output Files**<br>
`exonerate_classification.dir/GENE_all_matches_exonerate.tsv`<br>
`exonerate_classification.dir/GENE_best_matches_exonerate.tsv`<br>
`exonerate_classification.dir/GENE_refiltered_matches_exonerate.fasta`<br>

**Parameters**<br>
`[paths] path_to_exonerate`<br>
`[exonerate] min_score`<br>

Runs the Exonerate ungapped algorithm with each ERV region in the fasta files generated by makeFasta as queries and the all_ERVs_nt.fasta fasta file as a target, to detect which known retrovirus is most similar to each newly identified ERV region. Regions which don't meet a minimum score threshold (`exonerate_min_score`) are filtered out.

all_ERVs_nt.fasta contains nucleic acid sequences for many known endogenous and exogenous retroviruses with known classifications.

First all seqeunces are compared to the database and the raw output is saved as exonerate_classification.dirGENE_all_matches_exonerate.tsv. Results need a score greater than `exonerate_min_score`
against one of the genes of the same type (*gag*, *pol* or *env*) in the database. The highest scoring result which meets these critera for each sequence is then identified and output to exonerate_classification.dir/GENE_best_matches_exonerate.tsv. The sequences which meet these critera are also output to a FASTA file exonerate_classification.dir/GENE_refiltered_exonerate.fasta.


### getORFs<a name="getORFs"></a>

**Input Files**<br>
`exonerate_classification.dir/GENE_refiltered_matches_exonerate.fasta`<br>

**Output Files**<br>
`ORFs.dir/GENE_orfs_raw.fasta`<br>
xs`ORFs.dir/GENE_orfs_nt.fasta`<br>
`ORFs.dir/GENE_orfs_aa.fasta`<br>

**Parameters**<br>
`[orfs] translation_table`<br>
`[orfs] min_orf_len`<br>

Finds the longest open reading frame in each of the ERV regions in the filtered output table.

This analysis is performed using [EMBOSS revseq](http://emboss.open-bio.org/rel/dev/apps/revseq.html) and [EMBOSS transeq](http://emboss.open-bio.org/rel/dev/apps/transeq.html).

The sequence is translated in all six frames using the user specified translation table. The longest ORF is then identified. ORFs shorter than orfs_min_orf_length are filtered out. 

The positions of the ORFs are also convered so that they can be extracted directly from the input sequence file, rather than using the co-ordinates relative to the original Exonerate regions.

The raw transeq output, the nucleotide sequences of the ORFs and the amino acid sequences of the ORFs are written to the output FASTA files.

### checkORFsUBLAST<a name="checkORFsUBLAST"></a>

**Input Files**<br>
`ORFs.dir/GENE_orfs_nt.fasta`<br>
`UBLAST_dbs.dir/GENE_db.udb`<br>

**Output Files**<br>
`ublast_orfs.dir/GENE_UBLAST_alignments.txt`<br>
`ublast_orfs.dir/GENE_UBLAST.tsv`<br>
`ublast_orfs.dir/GENE_filtered_UBLAST.fasta`<br>

**Parameters**<br>
`[paths] path_to_usearch`<br>
`[usearch] min_id`<br>
`[usearch] min_hit_length`<br>
`[usearch] min_coverage`<br>


ERV ORFs in the fasta files generated by the ORFs function are compared to the original ERV amino acid files using UBLAST. This allows any remaining sequences with poor similarity to known ERVs to be filtered out.

This allows ORFs with low similarity to known ERVs to be filtered out.   Similarity thresholds can be set in the pipeline.ini file (`usearch_min_id,` - minimum identity between query and target -  `usearch_min_hit_length` - minimum length of hit on target sequence -  and `usearch_min_coverage` - minimum proportion of the query sequence the hit should cover).

The raw output of running UBLAST against the target sequences is saved in GENE_UBLAST_alignments.txt (equivalent to the BLAST default output) and GENE_UBLAST.tsv (equivalent to the BLAST -outfmt 6 tabular output) this is already filtered by passing the appropriate parameters to UBLAST. The regions which passed the filtering and are therefore in these output files are then output to a FASTA file GENE_filtered_UBLAST.fasta.

### assignGroups<a name="assignGroups"></a>

**Input Files**<br>
`ublast_orfs.dir/GENE_UBLAST.tsv`<br>
`ERVsearch/ERV_db/convert.tsv`<br>

**Output Files**<br>
`grouped.dir/GENE_groups.tsv`<br>

**Parameters**<br>
`[paths] path_to_ERVsearch`<br>

Many of the retroviruses in the input database all_ERVs_nt.fasta have been classified into groups based on sequence similarity, prior knowledge and phylogenetic clustering.  Some sequences don't fall into any well defined group, in these cases they are just assigned to a genus, usually based on prior knowledge. The information about these groups is stored in the provided file ERVsearch/ERV_db/convert.tsv.

Each sequence in the filtered fasta file of newly identified ORFs is assigned to one of these groups based on the sequence identified as the most similar in the classifyWithExonerate step. 

The output table is also  tidied up to include the UBLAST output, chromosome, ORF start and end positions, genus and group.

### summariseScreen<a name="summariseScreen"></a>

**Input Files**<br>

**Output Files**<br>

**Parameters**<br>

### Screen<a name="Screen"></a>

**Input Files** <br>
None<br>

**Output Files**<br>
 None<br>

**Parameters** <br>
None<br>

Helper function to run all screening functions (all functions prior to this point).