print 'Annotate tree'
            annotate_tree_time_freq_count(tree, ali)
            annotate_tree(patient, tree, VERBOSE=VERBOSE)

            if VERBOSE >= 2:
                print 'Ladderize tree'
            tree.ladderize()

            if use_save:
                if VERBOSE >= 2:
                    print 'Save tree (JSON)'
                rname = 'scan_'+str(win_start)+'-'+str(win_end)
                fn = patient.get_local_tree_filename(rname, format='json')
                mkdirs(os.path.dirname(fn))
                tree_json = tree_to_json(tree.root,
                                         fields=('DSI', 'sequence',
                                                 'muts',
                                                 'VL', 'CD4',
                                                 'frequency',
                                                 'count',
                                                 'confidence'),
                                        )
                write_json(tree_json, fn)

            if use_plot:
                if VERBOSE >= 2:
                    print 'Plot'
                plot_tree(tree, title=patient.code+', '+str(win_start)+'-'+str(win_end))

            win_start += gap
            if VERBOSE >= 2:
                print 'Annotate tree (for JSON format)'
            from hivwholeseq.utils.nehercook.ancestral import ancestral_sequences
            a = ancestral_sequences(tree, ali, alphabet='ACGT-N', copy_tree=False,
                                    attrname='sequence', seqtype='str')
            a.calc_ancestral_sequences()
            del a


            if VERBOSE >= 2:
                print 'Save to file, JSON'
            fn_out = get_subtype_reference_alignment_tree_filename(region,
                                                               subtype=subtype,
                                                               refname=refname,
                                                               type=alitype,
                                                               VERBOSE=VERBOSE,
                                                               format='json')
            from hivwholeseq.utils.tree import tree_to_json
            from hivwholeseq.utils.generic import write_json
            tree_json = tree_to_json(tree.root, fields=('sequence', 'confidence'))
            write_json(tree_json, fn_out)


        else:
            consrec = get_subtype_reference_alignment_tree(region,
                                                           subtype=subtype,
                                                           refname=refname,
                                                           type=alitype,
                                                           VERBOSE=VERBOSE)
            if VERBOSE >= 2:
                print 'Ladderize tree'
            tree.ladderize()

            correct_minimal_branches(tree)

            if use_save:
                if VERBOSE >= 2:
                    print 'Save tree (JSON)'
                fn = patient.get_consensi_tree_filename(region, format='json')
                tree_json = tree_to_json(
                    tree.root,
                    fields=('DSI', 'patient', 'sequence', 'muts', 'VL', 'CD4',
                            'subtype', 'confidence'),
                )
                write_json(tree_json, fn, indent=1)

        if use_joint:
            if VERBOSE >= 2:
                print 'Align all patients',
            ali_all = align_muscle(*seqs_all, sort=True)
            if VERBOSE >= 2:
                print 'OK'

            if use_save:
                if VERBOSE >= 2:
                    print 'Save all patients',
                reg_tmp = '_'.join(pcodes) + '_' + region
                fn_out = get_consensi_alignment_filename('all', reg_tmp)
                mkdirs(os.path.dirname(fn_out))
                AlignIO.write(ali, fn_out, 'fasta')
            if VERBOSE >= 2:
                print 'Annotate tree'
            annotate_tree_time_freq_count(tree, ali)
            annotate_tree(patient, tree, VERBOSE=VERBOSE)

            if VERBOSE >= 2:
                print 'Ladderize tree'
            tree.ladderize()

            if use_save:
                if VERBOSE >= 2:
                    print 'Save tree (JSON)'
                rname = 'scan_' + str(win_start) + '-' + str(win_end)
                fn = patient.get_local_tree_filename(rname, format='json')
                mkdirs(os.path.dirname(fn))
                tree_json = tree_to_json(
                    tree.root,
                    fields=('DSI', 'sequence', 'muts', 'VL', 'CD4',
                            'frequency', 'count', 'confidence'),
                )
                write_json(tree_json, fn)

            if use_plot:
                if VERBOSE >= 2:
                    print 'Plot'
                plot_tree(tree,
                          title=patient.code + ', ' + str(win_start) + '-' +
                          str(win_end))

            win_start += gap
            if use_save:
                if VERBOSE >= 2:
                    print 'Save tree (JSON)'
                fn = patient.get_local_tree_filename(region, format='json')
                tree_json = tree_to_json(tree.root,
                                         fields=('DSI',
                                                 'sequence',
                                                 'muts',
                                                 'VL',
                                                 'CD4',
                                                 'frequency',
                                                 'count',
                                                 'confidence'),
                                        )
                write_json(tree_json, fn, indent=1)

            if VERBOSE >= 2:
                print 'Extract alignment from tree (so with duplicates)'
            ali_tree = extract_alignment(tree, VERBOSE=VERBOSE)

            if use_save:
                if VERBOSE >= 2:
                    print 'Save alignment from tree'
                fn = patient.get_haplotype_alignment_filename(region, format='fasta')
                AlignIO.write(ali_tree, fn, 'fasta')



    if use_plot:
        plt.ion()