A Python program that reads sequence files in FASTA or FASTQ format, computes essential statistics, and performs interactive operations such as extracting, filtering, or converting sequences. The project reinforces skills in file handling, data validation, data analysis, and user interaction within a bioinformatics context.
project1_sequence_file_manipulation/
├── README.md
├── .gitignore
├── main.py
├── bio/
│ ├── io.py # FASTA/FASTQ I/O + validators
│ ├── utils.py # GC-content and helper functions
│ └── stats.py # Statistical computations
├── tasks/
│ └── ops.py # extract / filter / convert operations
├── data/
│ ├── example.fasta
│ └── example.fastq
└── tests/
| Requirement | Implementation |
|---|---|
| Support both FASTA & FASTQ | Automatic format detection via first non-empty line (> or @). |
| Error handling | Handles missing, empty, and corrupted files with clear messages. |
| Validation rules | Enforces correct structure and header rules for both formats (see validators below). |
| Statistics output | Prints total count, average length, largest/smallest sequences (up to 10 names), average GC%, and average number of N’s per sequence. |
| Interactive operations | Prompts user for extract, filter, or convert (case-insensitive). |
| Extract | Randomly selects ≤ 25 sequences, writes in the same format. |
| Filter | Keeps sequences ≥ user-defined length, writes in the same format. |
| Convert | FASTQ → FASTA supported; FASTA → error with exact required message. |
| Soft warnings | For invalid extensions or unexpected alphabets (IUPAC non-standard). |
| Streaming & efficient | Linear runtime, minimal memory usage, clean modular design. |
# 1. Clone the repository
git clone https://github.com/Ishtiaque-h/bio-prog.git
cd bio-progpython main.py analyze -i file_path_or_nameExample output:
Wrning!!!: Detected FASTA content but extension '.fs' is uncommon for FASTA.
============================================================
File statistics
============================================================
Total sequences : 8
Average length : 514.13
Largest length : 1183
Largest sequence name(s): 1HF_002 length: 572 gc: 225 gcpercent: 0.393356643356643
Smallest length : 258
Smallest sequence name(s): 1DR_006 length: 258 gc: 104 gcpercent: 0.403100775193798
Average GC-content (%) : 63.79
Average # of Ns/sequence: 0.0
============================================================
Choose an operation:
1: Extract random sequences
2: Filter by minimum length
3: Convert FASTQ to FASTA
4: Exit menu
Enter a number (1-4): 1
Wrote random selection to: tests/rna.extract.fasta
- Header line begins with
>immediately followed by sequence name (no space). - Next non-empty line after a header must be sequence (not another header).
- Sequence names must be unique.
- Each record ≥ 2 lines (header + sequence).
- Sequence lines may include only DNA/RNA/Protein characters (IUPAC; case-insensitive).
- Records are always 4 lines.
- Line 1:
@immediately followed by name (no space). - Line 2: sequence (non-empty, DNA/RNA characters).
- Line 3: starts with
+. - Line 4: quality string same length as sequence.
- Sequence names must be unique.
Soft warnings:
- Extension mismatch produces a warning, not an error.
- Total number of sequences
- Average sequence length (2 decimal places)
- Largest & smallest sequence names (up to 10 ties)
- Average GC-content (per-sequence GC%, averaged, Ns excluded)
- Average number of N’s per sequence
| Operation | Description | Output |
|---|---|---|
| extract | Randomly selects ≤ 25 sequences, retains format. | input.extract.fasta / .fastq |
| filter | Keeps sequences ≥ user-provided min length. | input.filter_ge<MIN>.fasta / .fastq |
| convert | FASTQ → FASTA; FASTA → prints error message. | input.fasta |
(compiled from internal progress notes)
-
Format detection: checked first non-empty line for
>or@. -
File-not-found: tested with nonexistent file → error handled.
-
Empty document: tested empty
.txt→ correct “empty file” error. -
Corrupted content:
- FASTA or FASTQ validators enforced standard sequencing rules and exceptions were handles carefully.
- FASTA or FASTQ file with gibberish after “>” or "@" correctly flagged as invalid.
- Both file with unwanted empty lines and whitespaces were handled carefully.
- Both with illegal characters in sequence raised a line-specific error.
-
Truncated file tests: single
@or>line correctly reported as “truncated record.” -
Extension handling: invalid extensions trigger a soft warning; program proceeds.
- Header info ignored for calculations (some FASTA may lack headers).
- Used per-sequence stats for length, GC%, and N counts.
- Interactive menu uses simple number values as options to avoid typos.
- All possible exceptions were handled gracefully.
extract: usesrandom.sample()to pick 25 sequences.filter: user-defined min length → sequences ≥ threshold written to output; if threshold > largest length, outputs “0 sequences” and doesn’t write a file.convert: FASTQ → FASTA implemented by keeping first 2 lines; reverse conversion disallowed with proper message.
All tests confirmed graceful failure handling — no unhandled exceptions or crashes.
- Streaming validation: one-pass, constant memory even for large files.
- Strict structure + soft content validation: only structural corruption halts execution.
- Fail fast: detect obvious corruption early.
- Clear user feedback: all errors include line numbers and context.
- Git discipline: modular commits and meaningful PR reviews (implementation vs. review roles maintained).
| Operation | Time | Space |
|---|---|---|
| FASTA/FASTQ parsing | O(n) | O(1) |
| Validation | O(n) | O(1) |
| Stats aggregation | O(n) | O(1) |
| Extract (default) | O(n) | O(n) |
| Filter / Convert | O(n) | O(1) |
| Role | Name | Responsibility |
|---|---|---|
| Lead | Ishtiaque | Design, Implementation & testing |
| Developer | Zainab | Implementation & testing |
| Reviewer | Ishtiaque | Code review, documentation, validation strategy |
- Add reservoir sampling for very large FASTA/FASTQ datasets.
- Integrate unit tests with
pytestfor CI workflows.
AI tools (ChatGPT & Gemini) was used to design, test and improve code; to preapre README file.
Developed for BINF690:Programming in Bioinformatics graduate course. © 2025 Team <Ishtiaque & Zainab>. For academic use only.