[Biopython]多重整列の一部を切り出す

# 動機
[Multiple sequence alignment (MSA)](https://ja.wikipedia.org/wiki/%E5%A4%9A%E9%87%8D%E6%95%B4%E5%88%97)の一部（たとえば、配列Xの100-200塩基目) を切り出したい。
# crop_alignment.py
https://github.com/satoshikawato/bio_small_scripts/blob/main/crop_alignment.py

# Requirements
- [Biopython](https://biopython.org/)

# Usage

```sh
$ ./crop_alignment.py
usage: crop_alignment.py [-h] --input FILE --output FILE -r REF -s START -e END [-g GAP]

Crop multiple sequence alignment gap-exclusively.

optional arguments:
  -h, --help            show this help message and exit
  --input FILE, -i FILE, --in FILE
                        Input FASTA file
  --output FILE, -o FILE, --out FILE, --output FILE
                        output FASTA file
  -r REF, --ref REF     reference entry name
  -s START, --start START
                        start position
  -e END, --end END     end position
  -g GAP, --gap GAP     gap character (default: "-")
```


```sh
$ less 16S.aligned.fasta

>NR_024570.1 Escherichia coli strain U 5/41 16S ribosomal RNA, partial sequence
---------AGTTTGATCATGGCTCAGATTGAACGCTGGCGGCAGGCCTAACACATGCAAGTCGAACGGTAACAGGAAGCAGCTTGCTGCTTTGCTGACGAGTGGCGGACGGGTGAGTAATGTCTGGG-AAACTGCCTGATGGAGGGGGATAACTACTGGAAACGGTAGCTAATACCGCATAACGTCGCAAG-CAC-AAAGAGGGGGACCTTAGGGC--------CTCTTGCCATCGGATGTGCCCAGATGGGATTAGCTAGTAGGTGGGGTAACGGCTCACCTAGGCGACGATCCCTAGCTGGTCTGAGAGGATGACCAGCAACACTGGAACTGAGACACGGTCCAGACTCCTACGGGAGGCAGCAGTGGGGAATATTGCACAATGGGCGCAAGCCTGATGCAGCCATGCNGCGTGTATGAAGAAGGCCTTC-GGGTTGTAAAGTACTTTCAGCGGGGAGGAAG-GGAGTAAAGTTAATACCTTTGCTCATTGACGTTACC-CGCAGAAGAAGCACCGGCTAACTCCGTGCCAGCAGCCGCGGTAATACGGAGGGTGCAAGCGTTAATCGGAATTACTGGGCGTAAAGCGCACGCAGGCGGTTTGTTAAGTCAGATGTGAAATCCCCGGGCTCAACCTGGGAACTGCATCTGATACTGGCAAGCTTGAGTCTCGTAGAGGGGGGTAGAATTCCAGGTGTAGCGGTGAAATGCGTAGAGATCTGGAGGAATACCGGTGGCGAAGGCGGCCCCCTGGACGAAGACTGACGCTCA-GGTGCGAAAGCGTGGGGAGCAAACAGGATTAGATACCCTGGTAGTCCACGCCGTAAACGATGTCGACTTGGAGGTTGTGCCCTT-GAGGCGTGGCTTCCGGANNTAACGCGTTAAGTCGACCGCCTGGGGAGTACGGCCGCAAGGTTAAAACTCAAA-TGAATTGACGGGGGCC-GCACAAGCGGTGGAGCATGTGGTTTAATTCGATGCAACGCGAAGAACCTTACCTGGTCTTGACATCCACGGAAGTTTT-CAGAGATGAGAATGTGCCT-----TCGGGAACCGTGAGACAGGTGCTGCATGGCTGTCGTCAGCTCGTGTTGTGAAATGTTGGGTTAAGTCCCGCAACGAGCGCAACCCTTATCCTTTGTTGCCAGC-GGTCCGGCCGGGAACTCAAAGGAGACTGCCAGTGATAAACTGGAGGAAGGTGGGGATGACGTCAAGTCATCATGGCCCTTACGACCAGGGCTACACACGTGCTACAATGGCGCATACAAAGAGAAGCGACCTCGCGAGAGCAAGCGGACCTCATAAAGTGCGTCGTAGTCCGGATTGGAGTCTGCAACTCGACTCCATGAAGTCGGAATCGCTAGTAATCGTGGATCAG-AATGCCACGGTGAATACGTTCCCGGGCCTTGTACACACCGCCCGTCACACCATGGGAGTGGGTTGCAAAAGAAGTAGGTAGCTTAACTTCGG-GAGGGCG----------------------------------------------------------------------------------
>NR_044682.2 Haemophilus influenzae strain 680 16S ribosomal RNA, partial sequence
A-ATTGAAGAGTTTGATCATGGCTCAGATTGAACGCTGGCGGCAGGCTTAACACATGCAAGTCGAACGGTAGCAGGAGAAAGCTTGCTTTCTTGCTGACGAGTGGCGGACGGGTGAGTAATGCTTGGG-AATCTGGCTTATGGAGGGGGATAACGACGGGAAACTGTCGCTAATACCGCGTATTATCGGAAG-ATG-AAAGTGCGGGACTGAGAGGC--------CGCATGCCATAGGATGAGCCCAAGTGGGATTAGGTAGTTGGTGGGGTAAATGCCTACCAAGCCTGCGATCTCTAGCTGGTCTGAGAGGATGACCAGCCACACTGGAACTGAGACACGGTCCAGACTCCTACGGGAGGCAGCAGTGGGGAATATTGCGCNATGGGGGGAACCCTGACGCAGCCATGCCGCGTGAATGAAGAAGGCCTTC-GGGTTGTAAAGTTCTTTCGGTATTGAGGAAG-GTTGATGTGTTAATAGCACATCAAATTGACGTTAAA-TACAGAAGAAGCACCGGCTAACTCCGTGCCAGCAGCCGCGGTAATACGGAGNGTGCGAGCGTTAATCGGAATAACTGGGCGTAAAGGGCACGCAGGCGGTTATTTAAGTGAGGTGTGAAAGCCCCGGGCTTAACCTGGGNATTGCATTTCAGACTGGGTAACTAGAGTACTTTAGGGAGGGGTAGAATTCCACGTGTAGCGGTGAAATGCGTAGAGATGTGGAGGAATACCGAAGGCGAAGGCAGCCCCTTGGGAATGTACTGACGCTCA-TGTGCGAAAGCGTGGGGAGCAAACAGGATTAGATACCCTGGTAGTCCACGCTGTAAACGCTGTCGATTTGGGGGTTGGGGTTT---AACTCTGGCACCCGTAGCTAACGTGATAAATCGACCGCCTGGGGAGTACGGCCGCAAGGTTAAAACTCAAA-TGAATTGACGGGGGCCNGCACAAGCGGTGGAGCATGTGGTTTAATTCGATGCAACGCGAAGAACCTTACCTACTCTTGACATCCTAAGAAGAGCT-CAGAGATGAGCTTGTGCCT-----TCGGGAACTTAGAGACAGGTGCTGCATGGCTGTCGTCAGCTCGTGTTGTGAAATGTTGGGTTAAGTCCCGCAACGAGCGCAACCCTTATCCTTTGTTGCCAGC-GACTTGGTCGGGAACTCAAAGGAGACTGCCAGTGATAAACTGGAGGAAGGTNGGGATGACGTCAAGTCATCATGGCCCTTACGAGTAGGGCTACACACGTGCTACAATGGCGTATACAGAGGGAAGCGAAGCTGCGAGGTGGAGCGAATCTCATAAAGTACGTCTAAGTCCGGATTGGAGTCTGCAACTCGACTCCATGAAGTCGGAATCGCTAGTAATCGCGAATCAG-AATGTCGCGGTGAATACGTTCCCGGGCNTTGTACACACCGCCCGTCACACCATGGGAGTGGGTTGTACCAGAAGTAGATAGCTTAACCTTTT-GGAGGGCGTTTACCACGGTATGATTCATGACTGGGG-----------------------------------------------------
>NR_112116.2 Bacillus subtilis strain IAM 12118 16S ribosomal RNA, complete sequence
TTATCGGAGAGTTTGATCCTGGCTCAGGACGAACGCTGGCGGCGTGCCTAATACATGCAAGTCGAGCGG--ACAGATGGGAGCTTGCTCCCTGAT--GTTAGCGGCGGACGGGTGAGTAACACGTGGGTAACCTGCCTGTAAGACTGGGATAACTCCGGGAAACCGGGGCTAATACCGGATGGTTGTTTGAA-CCGCATGGTTCAAACATAAAAGGTGGCTTCGGCTACCACTTACAGATGGACCCGCGGCGCATTAGCTAGTTGGTGAGGTAACGGCTCACCAAGGCAACGATGCGTAGCCGACCTGAGAGGGTGATCGGCCACACTGGGACTGAGACACGGCCCAGACTCCTACGGGAGGCAGCAGTAGGGAATCTTCCGCAATGGACGAAAGTCTGACGGAGCAACGCCGCGTGAGTGATGAAGGTTTTC-GGATCGTAAAGCTCTGTTGTTAGGGAAGAACAAGTACCGTTCGAATAGGGCGGTACCTTGACGGTACC-TAACCAGAAAGCCACGGCTAACTACGTGCCAGCAGCCGCGGTAATACGTAGGTGGCAAGCGTTGTCCGGAATTATTGGGCGTAAAGGGCTCGCAGGCGGTTTCTTAAGTCTGATGTGAAAGCCCCCGGCTCAACCGGGGAGGGTCATTGGAAACTGGGGAACTTGAGTGCAGAAGAGGAGAGTGGAATTCCACGTGTAGCGGTGAAATGCGTAGAGATGTGGAGGAACACCAGTGGCGAAGGCGACTCTCTGGTCTGTAACTGACGCTGA-GGAGCGAAAGCGTGGGGAGCGAACAGGATTAGATACCCTGGTAGTCCACGCCGTAAACGATGAGTGCTAAGTGTTAGGGGGTTTCCGCCCCTTAGTGCTGCAGCTAACGCATTAAGCACTCCGCCTGGGGAGTACGGTCGCAAGACTGAAACTCAAA-GGAATTGACGGGGGCCCGCACAAGCGGTGGAGCATGTGGTTTAATTCGAAGCAACGCGAAGAACCTTACCAGGTCTTGACATCCTCTGACAATCC-TAGAGATAGGACGTCCCCT-----TCGGGGGCAGAGTGACAGGTGGTGCATGGTTGTCGTCAGCTCGTGTCGTGAGATGTTGGGTTAAGTCCCGCAACGAGCGCAACCCTGGATCTTAGTTGCCAGC--ATTCAGTTGGGCACTCTAAGGTGACTGCCGGTGACAAACCGGAGGAAGGTGGGGATGACGTCAAATCATCATGCCCCTTATGACCTGGGCTACACACGTGCTACAATGGACAGAACAAAGGGCAGCGAAACCGCGAGGTTAAGCCAATCCCACAAATCTGTTCTCAGTTCGGATCGCAGTCTGCAACTCGACTGCGTGAAGCTGGAATCGCTAGTAATCGCGGATCAG-CATGCCGCGGTGAATACGTTCCCGGGCCTTGTACACACCGCCCGTCACACCACGAGAGTTTGTAACACCCGAAGTCGGTGAGGTAACCTTTTAGGAGCCAGCCGCCGAAGGTGGGACAGATGATTGGGGTGAAGTCGTAACAAGGTAGCCGTATCGGAAGGTGCGGCTGGATCACCTCCTTT
>NR_044761.1 Helicobacter pylori strain ATCC 43504 16S ribosomal RNA, partial sequence
TTTATGGAGAGTTTGATCCTGGCTCAGAGTGAACGCTGGCGGCGTGCCTAATACATGCAAGTCGAACGAT-GAAGCTTCTAGCTTGCTAGAGTGCTGATTAGTGGCGCACGGGTGAGTAACGCATAGGTCATGTGCCTCTTAGTTTGGGATAGCCATTGGAAACGATGATTAATACCAGATACTCCCTACGG-GGG---------------AAAGAT--------TTATCGCTAAGAGATCAGCCTATGTCCTATCAGCTTGTTGGTAAGGTAATGGCTTACCAAGGCTATGACGGGTATCCGGCCTGAGAGGGTGAACGGACACACTGGAACTGAGACACGGTCCAGACTCCTACGGGAGGCAGCAGTAGGGAATATTGCTCAATGGGGGAAACCCTGAAGCAGCAACGCCGCGTGGAGGATGAAGGTTTTA-GGATTGTAAACTCCTTTTGTTAGAGAAGATA--------------------------ATGACGGTATC-TAACGAATAAGCACCGGCTAACTCCGTGCCAGCAGCCGCGGTAATACGGAGGGTGCAAGCGTTACTCGGAATCACTGGGCGTAAAGAGCGCGTAGGCGGGATAGTCAGTCAGGTGTGAAATCCTATGGCTTAACCATAGAACTGCATTTGAAACTACTATTCTAGAGTGTGGGAGAGGTAGGTGGAATTCTTGGTGTAGGGGTAAAATCCGTAGAGATCAAGAGGAATACTCATTGCGAAGGCGACCTGCTGGAACATTACTGACGCTGATTGCGCGAAAGCGTGGGGAGCAAACAGGATTAGATACCCTGGTAGTCCACGCCCTAAACGATGGATGCTAGTTGTTGGAGGGCTTAGTCTCTCCAGTAATGCAGCTAACGCATTAAGCATCCCGCCTGGGGAGTACGGTCGCAAGATTAAAACTCAAA-GGAATAGACGGGGACCCGCACAAGCGGTGGAGCATGTGGTTTAATTCGAAGATACACGAAGAACCTTACCTAGGCTTGACATTGAGAGAATCCGC-TAGAAATAGTGGAGTGTCTAGCTTGCTAGACCTTGAAAACAGGTGCTGCACGGCTGTCGTCAGCTCGTGTCGTGAGATGTTGGGTTAAGTCCCGCAACGAGCGCAACCCCCTTTCTTAGTTGCTAACAGGTTATGCTGAGAACTCTAAGGATACTGCCTCCG-TAAGGAGGAGGAAGGTGGGGACGACGTCAAGTCATCATGGCCCTTACGCCTAGGGCTACACACGTGCTACAATGGGGTGCACAAAGAGAAGCAATACTGTGAAGTGGAGCCAATCTT-CAAAACACCTCTCAGTTCGGATTGTAGGCTGCAACTCGCCTGCATGAAGCTGGAATCGCTAGTAATCGCAAATCAGCCATGTTGCGGTGAATACGTTCCCGGGTCTTGTACTCACCGCCCGTCACACCATGGGAGTTGTGTTTGCCTTAAGTCAGGATGCTAAATT-------GGCTACTGCCCACGGCACACACAGCGACTGGGGTGAAGTCGTAACAAGGTAACCGTAGGTGAACCTGCGGCTGGATCACCTCCTT-
>NR_025900.1 Thermus aquaticus strain YT-1 16S ribosomal RNA, partial sequence
---------------------GCTCAGGGTGAACGCTGGCGGCGTGCCTAAGACATGCAAGTCGTGCGGG-CCGTGGGGTATCTCAC---------GGTCAGCGGCGGACGGGTGAGTAACGCGTGGGTGACCTACCCGGAAGAGGGGGACAACATGGGGAAACCCAGGCTAATCCCCCATGTGGACACATC-CTGTGGGGTGTGTTTAAAGGGTTT--------TGCCCGCTTCCGGATGGGCCCGCGTCCCATCAGCTAGTTGGTGGGGTAAGAGCCCACCAAGGCGACGACGGGTAGCCGGTCTGAGAGGACGGCCGGCCACAGGGGCACTGAGACACGGGCCCCACTCCTACGGGAGGCAGCAGTTAGGAATCTTCCGCAATGGGCGCAAGCCTGACGGAGCGACGCCGCTTGGAGGAGGAAGCCCTTC-GGGGTGTAAACTCCTGAACCCGGGACGAAAC--------CCCCGATGAGG----GGACTGACGGTACC--GGGGTAATAGCGCCGGCCAACTCCGTGCCAGCAGCCGCGGTAATACGGAGGGCGCGAGCGTTACCCGGATTTACTGGGCGTAAAGGGCGTGTAGGCGGCTTGGGGCGTCCCATGTGAAAGGCCACGGCTCAACCGTGGAGGAGCGTGGGATACGCTCAGGCTAGACGGTGGGAGAGGGTGGTGGAATTCCCGGAGTAGCGGTGAAATGCGCAGATACCGGGAGGAACGCCGATGGCGAAGGCAGCCACCTGGTCCACTCGTGACGCTGA-GGCGCGAAAGCGTGGGGAGCAAACCGGATTAGATACCCGGGTAGTCCACGCCCTAAACGATGCGCGCTAGGTCTCTGGG-------TTATCTGGGGGCCGAAGCTAACGCGTTAAGCGCGCCGCCTGGGGAGTACGGCCGCAAGGCTGAAACTCAAA-GGAATTGACGGGGGCCCGCACAAGCGGTGGAGCATGTGGTTTAATTCGAAGCAACGCGAAGAACCTTACCAGGCCTTGACATGCTAGGGAACCTGGGTGAAAGCCTGGGGTGCCCCGCG-AGGGGAGCCCTAGCACAGGTGCTGCATGGCCGTCGTCAGCTCGTGTCGTGAGATGTTGGGTTAAGTCCCGCAACGAGCGCAACCCCTGCCGTTAGTTGCCAGCGGGTGAAGCCGGGCACTCTAACGGGACTGCCTGCG-AAAGCAGGAGGAAGGCGGGGACGACGTCTGGTCATCATGGCCCTTACGGCCTGGGCGACACACGTGCTACAATGCCCACTACAGAGCGAGGCGACCTGGCAACAGGGAGCGAATCGCAAAAAGGTGGGCGTAGTTCGGATTGGGGTCTGCAACCCGACCCCATGAAGCCGGAATCGCTAGTAATCGCGGATCAGCCATGCCGCGGTGAATACGTTCCCGGGCCTTGTACACACCGCCCGTCACGCCATGGGAGCGGGTTCTACCCGAAGTCGCCGGG--AGCCT----TAGGGCAGGCGCCGAGGGTAGGGCCCGTGACTGGGGCGAAGTCGTAACAAGGTAGCTGTACCG--------------------------
>NR_041751.1 Mycoplasma pneumoniae FH strain ATCC 15531 16S ribosomal RNA, partial sequence
-----------------------------TTAACGCTGGCGGCATGCCTAATACATGCAAGTCGATCGAA-AGTAGTAATACT---------------TTAGAGGCGAACGGGTGAGTAACACGTATCCAATCTACCTTATAATGGGGGATAACTAGTTGAAAGACTAGCTAATACCGCATAAGAACTTTGGTTCGCATGAATCAAAGTTGAAAGGACCTGCAAGGGTTCGTTATTTGATGAGGGTGCGCCATATCAGCTAGTTGGTGGGGTAACGGCCTACCAAGGCAATGACGTGTAGCTATGCTGAGAAGTAGAATAGCCACAATGGGACTGAGACACGGCCCATACTCCTACGGGAGGCAGCAGTAGGGAATTTTTCACAATGAGCGAAAGCTTGATGGAGCAATGCCGCGTGAACGATGAAGGTCTTTAAGATTGTAAAGTTCTTTTATTTGGGAAGAAT-GACTTTAGCAGGTAATGGCTAGAGTTTGACTGTACCATTTTGAATAAGTGACGACTAACTATGTGCCAGCAGTCGCGGTAATACATAGGTCGCAAGCGTTATCCGGATTTATTGGGCGTAAAGCAAGCGCAGGCGGATTGAAAAGTCTGGTGTTAAAGGCAGCTGCTTAACAGTTGTA-TGCATTGGAAACTATTAATCTAGAGTGTGGTAGGGAGTTTTGGAATTTCATGTGGAGCGGTGAAATGCGTAGATATATGAAGGAACACCAGTGGCGAAGGCGAAAACTTAGGCCATTACTGACGCTTA-GGCTTGAAAGTGTGGGGAGCAAATAGGATTAGATACCCTAGTAGTCCACACCGTAAACGATAGATACTAGCTGTCGGGGCG----ATCCCCTCGGTAGTGAAGTTAACACATTAAGTATCTCGCCTGGGTAGTACATTCGCAAGAATGAAACTCAAACGGAATTGACGGGGACCCGCACAAGTGGTGGAGCATGTTGCTTAATTCGACGGTACACGAAAAACCTTACCTAGACTTGACATCCTTGGCAAAGTTATGGAAACATAATGGAGGTT----------AACCGAGTGACAGGTGGTGCATGGTTGTCGTCAGCTCGTGTCGTGAGATGTTGGGTTAAGTCCCGCAACGAGCGCAACCCTTATCGTTAGTTAC----------------ATTGTCTAGCGAGACTGCTAATG-CAAATTGGAGGAAGGAAGGGATGACGTCAAATCATCATGCCCCTTATGTCTAGGGCTGCAAACGTGCTACAATGGCCAATACAAACAGTCGCCAGCTTGTAAAAGTGAGCAAATCTG-TAAAGTTGGTCTCAGTTCGGATTGAGGGCTGCAATTCGTCCTCATGAAGTCGGAATCACTAGTAATCGCGAATCAGCTATGTCGCGGTGAATACGTTCTCGGGTCTTGTACACACCGCCCGTCAAACTATGAAAGCTGGTAATATTTAAAAACGTGTTGCTAACCATTA-GGAAGCGCATGTCAAGGATAGCACCGGTGATTGGAGTTAAGTCGTAACAAGGTACCCCTACGAGAACGTGGGGGTGGATCACCTCCTTT
```

```sh
$ ./crop_alignment.py -i 16S.aligned.fa  -s 101 -e 200 -r NR_024570.1 -o 16S.aligned.cropped.fa
```
```sh
$ less 16S.aligned.cropped.fa
>NR_024570.1 Escherichia coli strain U 5/41 16S ribosomal RNA, partial sequence
CGGGTGAGTAATGTCTGGG-AAACTGCCTGATGGAGGGGGATAACTACTGGAAACGGTAG
CTAATACCGCATAACGTCGCAAG-CAC-AAAGAGGGGGACCTT
>NR_044682.2 Haemophilus influenzae strain 680 16S ribosomal RNA, partial sequence
CGGGTGAGTAATGCTTGGG-AATCTGGCTTATGGAGGGGGATAACGACGGGAAACTGTCG
CTAATACCGCGTATTATCGGAAG-ATG-AAAGTGCGGGACTGA
>NR_112116.2 Bacillus subtilis strain IAM 12118 16S ribosomal RNA, complete sequence
CGGGTGAGTAACACGTGGGTAACCTGCCTGTAAGACTGGGATAACTCCGGGAAACCGGGG
CTAATACCGGATGGTTGTTTGAA-CCGCATGGTTCAAACATAA
>NR_044761.1 Helicobacter pylori strain ATCC 43504 16S ribosomal RNA, partial sequence
CGGGTGAGTAACGCATAGGTCATGTGCCTCTTAGTTTGGGATAGCCATTGGAAACGATGA
TTAATACCAGATACTCCCTACGG-GGG---------------A
>NR_025900.1 Thermus aquaticus strain YT-1 16S ribosomal RNA, partial sequence
CGGGTGAGTAACGCGTGGGTGACCTACCCGGAAGAGGGGGACAACATGGGGAAACCCAGG
CTAATCCCCCATGTGGACACATC-CTGTGGGGTGTGTTTAAAG
>NR_041751.1 Mycoplasma pneumoniae FH strain ATCC 15531 16S ribosomal RNA, partial sequence
CGGGTGAGTAACACGTATCCAATCTACCTTATAATGGGGGATAACTAGTTGAAAGACTAG
CTAATACCGCATAAGAACTTTGGTTCGCATGAATCAAAGTTGA

```