def alignprotein(self, sample, target):
        """
        Create alignments of the sample nucleotide and amino acid sequences to the reference sequences
        """
        # Initialise dictionaries
        sample[self.analysistype].dnaseq = dict()
        sample[self.analysistype].protseq = dict()
        sample[self.analysistype].ntindex = dict()
        sample[self.analysistype].aaindex = dict()
        sample[self.analysistype].ntalign = dict()
        sample[self.analysistype].aaalign = dict()
        sample[self.analysistype].aaidentity = dict()
        # Remove any gaps incorporated into the sequence
        sample[self.analysistype].targetsequence[target] = \
            sample[self.analysistype].targetsequence[target].replace('-', '')
        # In order to properly translate the nucleotide sequence, BioPython requests that the sequence is a multiple of
        # three - not partial codons. Trim the sequence accordingly
        remainder = 0 - len(sample[self.analysistype].targetsequence[target]) % 3
        seq = sample[self.analysistype].targetsequence[target] if remainder == 0 \
            else sample[self.analysistype].targetsequence[target][:remainder]
        # Set the DNA and protein sequences of the target in the sample
        sample[self.analysistype].dnaseq[target] = Seq(seq, IUPAC.unambiguous_dna)
        # Translate the nucleotide sequence
        sample[self.analysistype].protseq[target] = str(sample[self.analysistype].dnaseq[target].translate())
        for targetfile in self.targetfiles:
            # Trim the reference sequence to multiples of three
            refremainder = 0 - len(self.records[targetfile][target].seq) % 3
            refseq = str(self.records[targetfile][target].seq) if refremainder % 3 == 0 \
                else str(self.records[targetfile][target].seq)[:refremainder]
            # Translate the nucleotide sequence of the reference sequence
            refdna = Seq(refseq, IUPAC.unambiguous_dna)
            refprot = str(refdna.translate())
            # Use pairwise2 to perform a local alignment with the following parameters:
            # x     No match parameters. Identical characters have score of 1, otherwise 0.
            # s     Same open (-1)  and extend (-.1) gap penalties for both sequences
            ntalignments = pairwise2.align.localxs(seq, refseq, -1, -.1)
            # Use format_alignment to create a formatted alignment that is subsequently split on newlines e.g.
            '''
            ACCGT
            | ||
            A-CG-
            Score=3
            '''
            ntformat = (str(format_alignment(*ntalignments[0])).split('\n'))
            # Align the nucleotide sequence of the reference (ntalignments[2]) to the sample (ntalignments[0]).
            # If the corresponding bases match, add a |, otherwise a space
            ntalignment = ''.join(map(lambda x: '|' if len(set(x)) == 1 else ' ',
                                      zip(ntformat[0], ntformat[2])))
            # Create the nucleotide alignment: the sample sequence, the (mis)matches, and the reference sequence
            sample[self.analysistype].ntalign[target] = self.interleaveblastresults(ntformat[0], ntformat[2])
            # Regex to determine location of mismatches in the sequences
            count = 0
            sample[self.analysistype].ntindex[target] = str()
            for snp in re.finditer(' ', ntalignment):
                # If there are many SNPs, then insert line breaks for every 10 SNPs
                if count <= 10:
                    sample[self.analysistype].ntindex[target] += str(snp.start()) + ';'
                else:
                    sample[self.analysistype].ntindex[target] += '\n' + str(snp.start()) + ';'
                    count = 0
                count += 1
            # Perform the same steps, except for the amino acid sequence
            aaalignments = pairwise2.align.localxs(sample[self.analysistype].protseq[target], refprot, -1, -.1)
            aaformat = (str(format_alignment(*aaalignments[0])).split('\n'))
            aaalignment = ''.join(map(lambda x: '|' if len(set(x)) == 1 else ' ',
                                      zip(aaformat[0], aaformat[2])))
            sample[self.analysistype].aaidentity[target] = '{:.2f}'\
                .format(float(aaalignment.count('|')) / float(len(aaalignment)) * 100)
            sample[self.analysistype].aaalign[target] = self.interleaveblastresults(aaformat[0], aaformat[2])
            count = 0
            sample[self.analysistype].aaindex[target] = str()
            for snp in re.finditer(' ', aaalignment):
                if count <= 10:
                    sample[self.analysistype].aaindex[target] += str(snp.start()) + ';'
                else:
                    sample[self.analysistype].aaindex[target] += '\n' + str(snp.start()) + ';'
                    count = 0
                count += 1
def alignprotein ( self , sample , target ) : sample [ self . analysistype ] . dnaseq = dict ( ) sample [ self . analysistype ] . protseq = dict ( ) sample [ self . analysistype ] . ntindex = dict ( ) sample [ self . analysistype ] . aaindex = dict ( ) sample [ self . analysistype ] . ntalign = dict ( ) sample [ self . analysistype ] . aaalign = dict ( ) sample [ self . analysistype ] . aaidentity = dict ( ) sample [ self . analysistype ] . targetsequence [ target ] = sample [ self . analysistype ] . targetsequence [ target ] . replace ( '-' , '' ) remainder = 0 - len ( sample [ self . analysistype ] . targetsequence [ target ] ) % 3 seq = sample [ self . analysistype ] . targetsequence [ target ] if remainder == 0 else sample [ self . analysistype ] . targetsequence [ target ] [ : remainder ] sample [ self . analysistype ] . dnaseq [ target ] = Seq ( seq , IUPAC . unambiguous_dna ) sample [ self . analysistype ] . protseq [ target ] = str ( sample [ self . analysistype ] . dnaseq [ target ] . translate ( ) ) for targetfile in self . targetfiles : refremainder = 0 - len ( self . records [ targetfile ] [ target ] . seq ) % 3 refseq = str ( self . records [ targetfile ] [ target ] . seq ) if refremainder % 3 == 0 else str ( self . records [ targetfile ] [ target ] . seq ) [ : refremainder ] refdna = Seq ( refseq , IUPAC . unambiguous_dna ) refprot = str ( refdna . translate ( ) ) ntalignments = pairwise2 . align . localxs ( seq , refseq , - 1 , - .1 ) '''
            ACCGT
            | ||
            A-CG-
            Score=3
            ''' ntformat = ( str ( format_alignment ( * ntalignments [ 0 ] ) ) . split ( '\n' ) ) ntalignment = '' . join ( map ( lambda x : '|' if len ( set ( x ) ) == 1 else ' ' , zip ( ntformat [ 0 ] , ntformat [ 2 ] ) ) ) sample [ self . analysistype ] . ntalign [ target ] = self . interleaveblastresults ( ntformat [ 0 ] , ntformat [ 2 ] ) count = 0 sample [ self . analysistype ] . ntindex [ target ] = str ( ) for snp in re . finditer ( ' ' , ntalignment ) : if count <= 10 : sample [ self . analysistype ] . ntindex [ target ] += str ( snp . start ( ) ) + ';' else : sample [ self . analysistype ] . ntindex [ target ] += '\n' + str ( snp . start ( ) ) + ';' count = 0 count += 1 aaalignments = pairwise2 . align . localxs ( sample [ self . analysistype ] . protseq [ target ] , refprot , - 1 , - .1 ) aaformat = ( str ( format_alignment ( * aaalignments [ 0 ] ) ) . split ( '\n' ) ) aaalignment = '' . join ( map ( lambda x : '|' if len ( set ( x ) ) == 1 else ' ' , zip ( aaformat [ 0 ] , aaformat [ 2 ] ) ) ) sample [ self . analysistype ] . aaidentity [ target ] = '{:.2f}' . format ( float ( aaalignment . count ( '|' ) ) / float ( len ( aaalignment ) ) * 100 ) sample [ self . analysistype ] . aaalign [ target ] = self . interleaveblastresults ( aaformat [ 0 ] , aaformat [ 2 ] ) count = 0 sample [ self . analysistype ] . aaindex [ target ] = str ( ) for snp in re . finditer ( ' ' , aaalignment ) : if count <= 10 : sample [ self . analysistype ] . aaindex [ target ] += str ( snp . start ( ) ) + ';' else : sample [ self . analysistype ] . aaindex [ target ] += '\n' + str ( snp . start ( ) ) + ';' count = 0 count += 1
Create alignments of the sample nucleotide and amino acid sequences to the reference sequences