# About ALS-Binary

ALS-Binary is a Shiny application that converts allele size data into binary matrices for genetic analysis.  
It supports multiple file formats (.csv, .txt, .xlsx), handles both headered and headerless data, and produces Excel outputs with each marker on a separate sheet.  
The tool is intended for researchers working with microsatellite data, providing a reproducible and user-friendly workflow.

## Authors

**Prasanth VP**  
PhD Scholar in Statistics at CUSAT and Associate Director of Biostatistics at IQVIA.  
He has over 20 years of international experience in biostatistics, data science, and biometrics, spanning clinical research, pathological evaluation, advanced statistical modeling, and integrative treatments for chronic diseases.  
📧 [prasanth.stat@gmail.com](mailto:prasanth.stat@gmail.com)  
🔗 [Google Scholar](https://scholar.google.com/citations?user=OZlrnGwAAAAJ&hl=en)


**Dr. Subhash Chandra**  
Applied statistician with more than 50 years of professional experience in teaching, training, consulting, and leadership across academic and research environments worldwide.  
His expertise lies in applying statistical and computational science to agricultural, biological, environmental, ecological, and sociological research.  
He is a member of the International Biometric Society, Statistical Society of Australia, Crop Science Society of America, Soil Science Society of America, American Society of Agronomy, and Indian Society of Agricultural Statistics.  
🔗 [Google Scholar](https://scholar.google.com/citations?user=tVXMZkoAAAAJ&hl=en)

---

## Purpose

The app converts allele size data into binary matrices for genetic analysis.  
Users can upload raw data and repeat length information, preview them, and generate binary outputs for each marker.

---

## Rationale

Microsatellite markers (SSRs) are short tandem repeats in DNA, typically represented by allele sizes in base pairs. Raw allele sizes are not directly comparable across datasets or markers, so ALS‑Binary standardizes them into two complementary outputs:

- **Binary Output**  
  - Each marker’s allele sizes are sorted into a reference list.  
  - Genotypes are coded as `1` if the allele is present, `0` otherwise.  
  - Missing alleles are coded as `?`.  
  - This produces a presence/absence matrix suitable for statistical analyses such as PCA, AMOVA, clustering, or regression.

- **STRUCTURE Output**  
  - Uses the same sorted allele list per marker.  
  - Each allele is coded by its positional index in that list (1, 2, …).  
  - Missing alleles are coded as `-9`.  
  - This format is required by population genetics software like STRUCTURE, which expects integer allele codes.

By generating both outputs simultaneously, ALS‑Binary ensures compatibility with diverse downstream analyses while maintaining reproducibility and transparency.

---

## Design Principles

- **Consistency:** Both outputs are derived from the same sorted allele list per marker.  
- **Transparency:** Missing data is explicitly coded (`?` in binary, `-9` in STRUCTURE).  
- **Flexibility:** The Shiny app allows inspection of raw data, repeat lengths, binary matrices, and STRUCTURE positions side by side.  
- **Reproducibility:** Export functions produce standardized Excel sheets, with each marker written to a separate sheet.

---

## Example Outputs

- **Binary Output:**  
  Genotype rows × allele size columns, with 1/0 indicating presence/absence.  

- **STRUCTURE Output:**  
  Genotype rows × allele position columns, with integer indices or `-9` for missing.
  
---

## Steps to Use

1. **Upload Data File**  
   - Formats: `.csv`, `.txt`, `.xlsx`  
   - Must contain Marker, Genotype, and allele sizes.  
   - If no headers, select "No" under *Data contains column headers*.  
     The app will assign: Marker, Genotype, Allele_1, Allele_2, etc.

2. **Upload Repeat Length File**  
   - Formats: `.csv`, `.txt`, `.xlsx`  
   - Contains repeat length information for each marker.  
   - Preview available under *Repeat Length Preview*.

3. **Delimiter Selection**  
   - Choose Comma, Tab, or Pipe.

4. **Convert to Binary**  
   - Click *Convert to Binary*.  
   - A progress bar shows the process.  
   - Once complete, a marker selector appears.  
   - Use the dropdown to view binary matrices.

5. **Download Results**  
   - Click *Download Results* to save all binary matrices into an Excel file.  
   - Each marker is written to a separate sheet.  
   - The filename includes today’s date (e.g., `binary_output_2026-07-29.xlsx`).

---

## Output Files

- **Binary matrices** are exported with each marker on a separate sheet.  
- **STRUCTURE matrices** are exported in parallel, also marker‑wise.  
- This dual output mirrors the original C implementation and ensures compatibility with both statistical and population genetics workflows.

---

## Notes

- Data file must have at least two columns: Marker and Genotype.  
- Missing alleles should be coded as `0`.  
- Zeros are automatically removed before processing.  
- Binary matrices show presence (1) or absence (0) of alleles per genotype.

---

## Missing Data Handling

- Input files should code missing alleles as `0`.  
- In **Binary Output**, missing alleles are shown as `?`.  
- In **STRUCTURE Output**, missing alleles are coded as `-9`.  
- This explicit coding ensures downstream tools can correctly interpret absence vs missingness.

---

## Examples

### Data File
Example input file with column headers:

| Marker&nbsp;&nbsp; | Genotype&nbsp;&nbsp; | Allele_1&nbsp;&nbsp; | Allele_2 |
|--------------------|----------------------|----------------------|----------|
| 18C5               | GG2                  | 328.71               | 333.01   |
| 18C5               | ICC1000              | 328.71               | 0        |


### Repeat Length File
Example repeat length file:

| Marker&nbsp;&nbsp; | RepeatLength |
|--------------------|--------------|
| 18C5               | 2            |
