#!usr/bin/perl
######################################################################
#
#Ian Odell
#Read Hep C fasta
#Separate by types and write new .fasta files
#
######################################################################
use strict;
use warnings;
use Bio::AlignIO;
use Bio::SeqIO;
use Bio::Seq;

my $inputfilename = "type1sorted.fasta";
my $outputfilename = ">type1_compProfile.fasta";
my $profile = '--TA---G-T-A----T---G-A--G--TG-T---C----G-T--G-C-A-G-CC-T-CC-AGG---A-C-C-C--CCCC--T--CCC-GGG-A-G-A-GCCA-TA-GTGG-TC-T-GCGGAACC---GGTG-A-GT-ACA-C-CGG-AAT-T-GCC-AGGAC-G-A-C-CG-G-G-TCC--TTT--C--TT--GG---A---T------A---A--A---C-CC-GCT--CA-A-T-G-CCT-GG--AG-A--TTT-GGG-C-GTG-CCCCCGCAA-G-A-C-TG-CTAG-CCGAGTA-GT-GTT-GGG-T-CG--C--GAA---A-GG-C-C--T--T----G--T--GG--T---';

my $in = Bio::SeqIO->new(-file => $inputfilename);

my @seq_array;
while (my $seq = $in->next_seq()) {
  push @seq_array, $seq;
}

#for each sequence, count how many mismatches there are compared to $profile
#append that number to the end of the id
my $num_mismatches;
foreach my $seq (@seq_array) {
  $num_mismatches = 0;
  for my $nuc (1..$seq->length) {
    if ($seq->subseq($nuc, $nuc) ne substr ($profile, $nuc-1, 1)) {
      $num_mismatches++;
    }
  }
  $seq->id($seq->id . "_$num_mismatches");
  print "$num_mismatches\n";
}

#write new alignments to their own files
my $out = Bio::SeqIO->new(-file => $outputfilename);
foreach my $seq (@seq_array) {
  $out->write_seq($seq);
}

