| 1 |
import pyabpoa as pa |
| 2 |
|
| 3 |
# @parameters of msa_aligner: |
| 4 |
# aln_mode='g' # g: global, l: local, e: extension |
| 5 |
# is_aa=False # set as True if input is amino acid sequence |
| 6 |
# score_matrix='' # file of score matrix, e.g. HOXD70.mtx/BLOSUM62.mtx |
| 7 |
# match=2 |
| 8 |
# mismatch=4 |
| 9 |
# gap_open1=4 |
| 10 |
# gap_open2=24 |
| 11 |
# gap_ext1=2 |
| 12 |
# gap_ext2=1 |
| 13 |
# extra_b = 10 # 1st part of extra band, -1 to disable banded DP |
| 14 |
# extra_f = 0.01 # 2nd part of eatra band. w = extra_b+extra_f*L (L is sequence length) |
| 15 |
# max_n_cons=1 # to output at most N cons, set max_n_cons as N |
| 16 |
# min_freq=0.3 # minimum frequence of each consensus to output for diploid data |
| 17 |
|
| 18 |
# construct msa aligner |
| 19 |
a = pa.msa_aligner() |
| 20 |
|
| 21 |
print("==== First exmaple: 2 consensus sequences ====\n") |
| 22 |
# for multiple consensus |
| 23 |
seqs = [ |
| 24 |
"CGATCGATCGATCGATGCATGCATCGATGCATCGATCGATGCATGCAT", |
| 25 |
"CGATCGATCGATAAAAAAAAAAAAAAAAAAACGATGCATGCATCGATGCATCGATCGATGCATGCAT", |
| 26 |
"CGATCGATCGATCGATGCATGCATCGATGCATCGATCGATGCATGCAT", |
| 27 |
"CGATCGATCGATCGATGCATGCATCGATGCATCGATCGATGCATGCAT", |
| 28 |
"CGATCGATCGATAAAAAAAAAAAAAAAAAAACGATGCATGCATCGATGCATCGATCGATGCATGCAT", |
| 29 |
"CGATCGATCGATAAAAAAAAAAAAAAAAAAACGATGCATGCATCGATGCATCGATCGATGCATGCAT", |
| 30 |
"CGATCGATCGATAAAAAAAAAAAAAAAAAAACGATGCATGCATCGATGCATCGATCGATGCATGCAT", |
| 31 |
"CGATCGATCGATCGATGCATGCATCGATGCATCGATCGATGCATGCAT", |
| 32 |
"CGATCGATCGATCGATGCATGCATCGATGCATCGATCGATGCATGCAT", |
| 33 |
"CGATCGATCGATCGATGCATGCATCGATGCATCGATCGATGCATGCAT", |
| 34 |
] |
| 35 |
|
| 36 |
# @parameters of msa |
| 37 |
# seqs: multiple sequences |
| 38 |
out_cons = True # generate consensus sequence, set as False to disable |
| 39 |
out_msa = True # generate row-column multiple sequence alignment, set as False to disable |
| 40 |
# out_pog="example1.png" # generate plot of alignment graph, set None to disable, require `dot` to be installed |
| 41 |
max_n_cons = 2 |
| 42 |
|
| 43 |
# multiple sequence alignment for 'seqs' |
| 44 |
res = a.msa(seqs, out_cons=out_cons, out_msa=out_msa, max_n_cons=max_n_cons) # , out_pog=out_pog) |
| 45 |
|
| 46 |
# output result |
| 47 |
if out_cons: |
| 48 |
for i in range(res.n_cons): |
| 49 |
print(f">Consensus_sequence_{i + 1}") |
| 50 |
print(res.cons_seq[i]) |
| 51 |
if out_msa: |
| 52 |
res.print_msa() |
| 53 |
|
| 54 |
|
| 55 |
print("\n\n==== Second exmaple: 1 consensus sequence ====\n") |
| 56 |
seqs = [ |
| 57 |
"CGTCAATCTATCGAAGCATACGCGGGCAGAGCCGAAGACCTCGGCAATCCA", |
| 58 |
"CCACGTCAATCTATCGAAGCATACGCGGCAGCCGAACTCGACCTCGGCAATCAC", |
| 59 |
"CGTCAATCTATCGAAGCATACGCGGCAGAGCCCGGAAGACCTCGGCAATCAC", |
| 60 |
"CGTCAATGCTAGTCGAAGCAGCTGCGGCAGAGCCGAAGACCTCGGCAATCAC", |
| 61 |
"CGTCAATCTATCGAAGCATTCTACGCGGCAGAGCCGACCTCGGCAATCAC", |
| 62 |
"CGTCAATCTAGAAGCATACGCGGCAAGAGCCGAAGACCTCGGCCAATCAC", |
| 63 |
"CGTCAATCTATCGGTAAAGCATACGCTCTGTAGCCGAAGACCTCGGCAATCAC", |
| 64 |
"CGTCAATCTATCTTCAAGCATACGCGGCAGAGCCGAAGACCTCGGCAATC", |
| 65 |
"CGTCAATGGATCGAGTACGCGGCAGAGCCGAAGACCTCGGCAATCAC", |
| 66 |
"CGTCAATCTAATCGAAGCATACGCGGCAGAGCCGTCTACCTCGGCAATCACGT", |
| 67 |
] |
| 68 |
|
| 69 |
# @parameters of msa |
| 70 |
# seqs: multiple sequences |
| 71 |
out_cons = True # generate consensus sequence, set as False to disable |
| 72 |
out_msa = True # generate row-column multiple sequence alignment, set as False to disable |
| 73 |
# out_pog="example2.png" # generate plot of alignment graph, set None to disable |
| 74 |
max_n_cons = 1 |
| 75 |
|
| 76 |
# multiple sequence alignment for 'seqs' |
| 77 |
res = a.msa(seqs, out_cons=out_cons, out_msa=out_msa, max_n_cons=max_n_cons) # , out_pog=out_pog) |
| 78 |
|
| 79 |
# output result |
| 80 |
if out_cons: |
| 81 |
for i in range(res.n_cons): |
| 82 |
print(f">Consensus_sequence_{i + 1}") |
| 83 |
print(res.cons_seq[i]) |
| 84 |
if out_msa: |
| 85 |
for i in range(res.n_seq): |
| 86 |
print(f">Seq_{i + 1}") |
| 87 |
print(res.msa_seq[i]) |
| 88 |
for i in range(res.n_cons): |
| 89 |
print(f">Consensus_sequence_{i + 1}") |
| 90 |
print(res.msa_seq[res.n_seq + i]) |
| 91 |
|
| 92 |
|
| 93 |
print("\n\n==== Third exmaple: quality-weighted consensus ====\n") |
| 94 |
seqs = [ |
| 95 |
"ACGT", |
| 96 |
"ACGT", |
| 97 |
"ACGA", |
| 98 |
] |
| 99 |
|
| 100 |
# Per-base Phred qualities. This matches the shape returned by |
| 101 |
# Biopython's record.letter_annotations["phred_quality"] for FASTQ input. |
| 102 |
qscores = [ |
| 103 |
[40, 40, 40, 40], |
| 104 |
[35, 35, 35, 35], |
| 105 |
[30, 30, 30, 5], |
| 106 |
] |
| 107 |
|
| 108 |
res = a.msa(seqs, out_cons=True, out_msa=False, qscores=qscores) |
| 109 |
|
| 110 |
for i in range(res.n_cons): |
| 111 |
print(f"@Consensus_sequence_{i + 1}") |
| 112 |
print(res.cons_seq[i]) |
| 113 |
print("+") |
| 114 |
print(res.cons_qv[i]) |