| 1 |
import csv |
| 2 |
import pickle |
| 3 |
import collections |
| 4 |
from Bio import SeqIO |
| 5 |
from Bio.Align import MultipleSeqAlignment |
| 6 |
from Bio.Align.AlignInfo import SummaryInfo |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
def get_lineage_cns50_sites(lineage, lineage_taxa, sequences_index, reference_sequence): |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
lineage_seqs = MultipleSeqAlignment([]) |
| 15 |
for taxon in lineage_taxa: |
| 16 |
lineage_seqs.append(sequences_index[taxon]) |
| 17 |
|
| 18 |
info = SummaryInfo(lineage_seqs) |
| 19 |
consensus_sequence = info.gap_consensus( |
| 20 |
threshold=0.50, |
| 21 |
ambiguous='N') |
| 22 |
|
| 23 |
nuc_position = [] |
| 24 |
for i in range(len(consensus_sequence)): |
| 25 |
if reference_sequence[i] != "N": |
| 26 |
if consensus_sequence[i] != "N": |
| 27 |
if consensus_sequence[i] != reference_sequence[i]: |
| 28 |
nuc_position.append(i) |
| 29 |
return nuc_position |
| 30 |
|
| 31 |
def get_relevant_positions(designation_file,seq_file,ref_file,outfile): |
| 32 |
reference = SeqIO.read(ref_file, "fasta") |
| 33 |
sequences_index = SeqIO.index(seq_file, "fasta") |
| 34 |
lineage_designations = collections.defaultdict(list) |
| 35 |
lineage_set = set() |
| 36 |
|
| 37 |
with open(designation_file, "r") as f: |
| 38 |
reader = csv.DictReader(f) |
| 39 |
for row in reader: |
| 40 |
lineage_designations[row["lineage"]].append(row["sequence_name"]) |
| 41 |
lineage_set.add(row["lineage"]) |
| 42 |
|
| 43 |
final_positions = set() |
| 44 |
for lineage in lineage_set: |
| 45 |
print(f"Getting positions for lineage {lineage}") |
| 46 |
positions = get_lineage_cns50_sites(lineage, lineage_designations[lineage], sequences_index, reference) |
| 47 |
print(f"\tFound {len(positions)}") |
| 48 |
for i in positions: |
| 49 |
final_positions.add(i) |
| 50 |
|
| 51 |
with open(outfile, 'wb') as pickle_file: |
| 52 |
pickle.dump(final_positions, pickle_file) |