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)
                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'
            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
            annotate_tree(patient, tree,
                          fields=fields,
                          VERBOSE=VERBOSE)


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


            if use_save:
                if VERBOSE >= 2:
                    print 'Save tree (JSON)'
                fields.extend(['sequence', 'confidence'])
                fn = patient.get_consensi_tree_filename(region, format='json')
                tree_json = tree_to_json(tree.root, fields=fields)
                write_json(tree_json, fn)

            if use_plot:
                import matplotlib.pyplot as plt
                fig, ax = plt.subplots(figsize=(15, 12))
                Phylo.draw(tree, do_show=False, axes=ax)
                ax.set_title(pname+', '+region)

                x_max = max(tree.depths().itervalues())
                ax.set_xlim(0.995, 0.995 + (x_max - 0.995) * 1.4)
                ax.grid(True)
                
                plt.ion()
                plt.show()
                print 'Annotate tree'
            annotate_tree(patient, tree, ali, 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
            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:
Example #7
0
                print 'Annotate tree'
            fields = ['DSI', 'muts', 'VL', 'ntemplates', 'CD4', 'subtype']
            if region in regionsprot:
                fields.append('mutsprot')
            annotate_tree(patient, tree, fields=fields, VERBOSE=VERBOSE)

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

            if use_save:
                if VERBOSE >= 2:
                    print 'Save tree (JSON)'
                fields.extend(['sequence', 'confidence'])
                fn = patient.get_consensi_tree_filename(region, format='json')
                tree_json = tree_to_json(tree.root, fields=fields)
                write_json(tree_json, fn)

            if use_plot:
                import matplotlib.pyplot as plt
                fig, ax = plt.subplots(figsize=(15, 12))
                Phylo.draw(tree, do_show=False, axes=ax)
                ax.set_title(pname + ', ' + region)

                x_max = max(tree.depths().itervalues())
                ax.set_xlim(0.995, 0.995 + (x_max - 0.995) * 1.4)
                ax.grid(True)

                plt.ion()
                plt.show()