Beispiel #1
0
    def __init__(self, tree):
        self.itol = Itol()
        assert tree.endswith(
            ".tree.txt"), "Your input tree must end in .tree.txt"
        self.itol.add_file(tree)
        self._datasets = 0
        self.status = None

        # datasets_visible should not be used if there is no datasets.
        #
        self.params = {
            "display_mode": 2,  # rotation
            "ignore_branch_length": 1,
            "line_width": 5,
            #"datasets_visible": "0,1",
            'bootstrap_display': 1,
            'bootstrap_type': 2,
            'bootstrap_label_size': 32
        }
 def create_itol_link(process_ID, newick):
     with open(
             "/home/ubuntu/coxbase/tools/grapeTree/tre/{}.tree".format(
                 process_ID),
             "w",
             encoding="utf-8",
     ) as open_file:
         open_file.write(newick)
     itol_uploader = Itol()
     itol_uploader.add_file(
         "/home/ubuntu/coxbase/tools/grapeTree/tre/{}.tree".format(
             process_ID))
     itol_uploader.params["treeName"] = process_ID
     itol_uploader.upload()
     return itol_uploader.get_webpage()
def GetTreeListOrder(treefile, outpath):
    #Create the Itol class
    itl = Itol()
    #Set the tree file
    tree = treefile

    dirname = os.path.dirname(treefile)
    if dirname == "":
        dirname = "."
    if outpath == "":
        outpath = dirname
    elif not os.path.exists(outpath):
        os.system("mkdir -p %s" % (outpath))

    rootname = os.path.basename(os.path.splitext(treefile)[0])

    #===================================
    itl.add_file(tree)
    itl.params['treeName'] = rootname
    itl.params['treeFormat'] = 'newick'
    #===================================
    # Check parameters
    # itl.print_variables()
    #Submit the tree
    print('')
    good_upload = itl.upload()
    if good_upload == False:
        print('There was an error:' + itl.comm.upload_output)
        return 1


#Export to pdf
    tree_id = itl.comm.tree_id

    itol_exporter = itl.get_itol_export()
    itol_exporter.set_export_param_value('format', "pdf")
    itol_exporter.set_export_param_value(
        'display_mode', "1")  #(1=normal, 2=circular, 3=unrooted)
    itol_exporter.set_export_param_value('line_width', "1")
    #epsfile = outpath + os.sep + rootname + '.itolnormal.eps'
    pdffile = outpath + os.sep + rootname + '.itolnormal.pdf'
    print('Exporting to pdffile %s' % (pdffile))
    itol_exporter.export(pdffile)
    orderfile = outpath + os.sep + rootname + '.listorder.txt'

    os.system("pdftotext -nopgbrk %s %s" % (pdffile, orderfile))
    os.system("rm -f %s" % pdffile)
    print('list order output to ', orderfile)
Beispiel #4
0
    def test_upload_download(self):
        current_dir = os.path.dirname(os.path.realpath(__file__))
        examples_dir = os.path.join(current_dir, '..', '..', 'examples')

        test = Itol()
        tree = os.path.join(examples_dir, 'tree_of_life.tree.txt')
        test.add_file(tree)
        dataset = os.path.join(examples_dir, 'colors_tol.txt')
        test.add_file(dataset)
        upload_status = test.upload()

        self.assertTrue(upload_status)

        exporter = test.get_itol_export()
        exporter.set_export_param_value('format', 'pdf')
        with tempfile.NamedTemporaryFile() as temp:
            exporter.export(temp.name)
Beispiel #5
0
def plot(output_dir: str, table: biom.Table, tree: NewickFormat,
         feature_metadata: pd.DataFrame, category: str,
         color_palette: str = 'Dark2', ms2_label: bool = False,
         parent_mz: str = None) -> None:

    if category not in feature_metadata.columns:
        raise ValueError('Could not find %s in the feature data, the available'
                         ' columns are: %s.' %
                         (category,
                          ', '.join(feature_metadata.columns.astype())))

    color_fp = join(output_dir, 'colors.tsv')
    with open(color_fp, 'w') as fh:
        fh.write(format_colors(feature_metadata, category, color_palette))

    label_fp = join(output_dir, 'labels.tsv')
    with open(label_fp, 'w') as fh:
        fh.write(format_labels(feature_metadata, category, ms2_label,
                               parent_mz))

    # itol won't accept a file unless it has a .tree or .txt extension
    target = join(output_dir, 'qemistree.tree')
    copyfile(str(tree), target)

    # upload the tree, labels and tip coloring files
    itol_uploader = Itol()
    itol_uploader.add_file(target)
    itol_uploader.add_file(label_fp)
    itol_uploader.add_file(color_fp)

    status = itol_uploader.upload()

    if not status:
        raise ValueError(itol_uploader.comm.upload_output)

    url = itol_uploader.get_webpage()
    print(url)

    index_path = join(TEMPLATES, 'index.html')
    q2templates.render(index_path, output_dir, context={'url': url})
Beispiel #6
0
def visualize_itol(request, taxonomy_id):
  newick_tree = services.newick_tree(taxonomy_id)
  tree_fh = NamedTemporaryFile(mode='w', suffix='.tree')
  tree_fh.write(newick_tree)
  tree_fh.flush()

  try:
    from itolapi import Itol
    itol_uploader = Itol()
    itol_uploader.add_file(tree_fh.name)
    status = itol_uploader.upload()
    assert status != False
    itol_page = itol_uploader.get_webpage()
    return redirect(itol_uploader.get_webpage())
  except:
    return HttpResponse('Something went wrong with the iTOL API. Save the following Newick tree to a file and upload it to iTOL yourself to visualize the tree:\n{}'.format(newick_tree), content_type = "text/plain")

  '''return HttpResponse(newick_tree)'''
Beispiel #7
0
def Itol_Tree_m1(pfamid, datapath, outpath):#{{{
# TM helices are treated as domains
#Create the Itol class
    itl = Itol()
#Set the tree file
    tree = datapath + os.sep + pfamid + '.tree'
    (datafile1, datafile2, datafile3, datafile4) = ("", "", "", "")
    if not os.path.exists(tree):
        print("tree file %s does not exist. Ignore" %(tree), file=sys.stderr)
        return 1
    t = Tree(tree)
    leaves = t.get_leaves()
    numLeave = len(leaves)

    fontsize = GetFontSize(numLeave)

    datafile1 = datapath + os.sep + pfamid + '.numTM_and_io.txt'
    datafile2 = datapath + os.sep + pfamid + '.cmpclass.colordef.txt'
#    datafile3 = datapath + os.sep + pfamid + '.ntermstate.colordef.txt'
    datafile4 = datapath + os.sep + pfamid + '.cluster.colordef.txt'

    colordeffile = datapath + os.sep + pfamid + '.pfam.colordef.txt'
    branchlabelfile = datapath + os.sep + pfamid + '.branchlabel.txt'

#===================================
    itl.add_variable('treeFile',tree)
    itl.add_variable('treeName','PF00854')
    itl.add_variable('treeFormat','newick')
    if os.path.exists(colordeffile):
        itl.add_variable('colorDefinitionFile', colordeffile)
    if os.path.exists(branchlabelfile):
        itl.add_variable('branchLabelsFile', branchlabelfile)

#===================================
    if os.path.exists(datafile1):
        itl.add_variable('datafile1File',datafile1)
        itl.add_variable('datafile1Label','numTM_and_io')
        itl.add_variable('datafile1Separator','comma')
        itl.add_variable('datafile1Type','multibar')
        itl.add_variable('datafile1PreventOverlap','1')
        itl.add_variable('datafile1Color','#FF0000')

#===================================
    if os.path.exists(datafile2):
        itl.add_variable('datafile2File', datafile2)
        itl.add_variable('datafile2Label', 'cmpclass')
        itl.add_variable('datafile2Separator','comma')
        itl.add_variable('datafile2Type','colorstrip')
        itl.add_variable('datafile2StripWidth','200')
        itl.add_variable('datafile2PreventOverlap','1')
        itl.add_variable('datafile2ColoringType','both')

#===================================
    if os.path.exists(datafile3):
        itl.add_variable('datafile3File', datafile3)
        itl.add_variable('datafile3Label', 'NtermState')
        itl.add_variable('datafile3Separator','comma')
        itl.add_variable('datafile3Type','colorstrip')
        itl.add_variable('datafile3StripWidth','200')
        itl.add_variable('datafile3PreventOverlap','1')
        itl.add_variable('datafile3ColoringType','both')

#===================================
    if os.path.exists(datafile4):
        itl.add_variable('datafile4File', datafile4)
        itl.add_variable('datafile4Label', 'cluster')
        itl.add_variable('datafile4Separator','comma')
        itl.add_variable('datafile4Type','colorstrip')
        itl.add_variable('datafile4StripWidth','200')
        itl.add_variable('datafile4PreventOverlap','1')
        itl.add_variable('datafile4ColoringType','both')
        itl.add_variable('datafile4BranchColoringType','both')
        
#itl.add_variable('datafile1BarSizeMax','1')

#===================================
# Check parameters
# itl.print_variables()


#Submit the tree
    print('')
    print('Uploading the tree.  This may take some time depending on how large the tree is and how much load there is on the itol server')
    good_upload = itl.upload()
    if good_upload == False:
        print('There was an error:'+itl.comm.upload_output)
        sys.exit(1)

#Read the tree ID
    print('Tree ID: '+str(itl.comm.tree_id))

#Read the iTOL API return statement
    print('iTOL output: '+str(itl.comm.upload_output))

#Website to be redirected to iTOL tree
    print('Tree Web Page URL: '+itl.get_webpage())

# Warnings associated with the upload
    print('Warnings: '+str(itl.comm.warnings))

#Export to pdf
    print('Exporting to pdf')
    itol_exporter = itl.get_itol_export()
#itol_exporter = itolexport.ItolExport()
#itol_exporter.set_export_param_value('tree','18793532031912684633930')
    itol_exporter.set_export_param_value('format', 'eps')
    itol_exporter.set_export_param_value('display_mode',"2")
    itol_exporter.set_export_param_value('current_font_size',fontsize)
    itol_exporter.set_export_param_value('align_labels',"1")
    itol_exporter.set_export_param_value('datafileList','dataset1')
    epsfile = outpath + os.sep + pfamid + '-itol.eps'
    pdffile = outpath + os.sep + pfamid + '-itol.pdf'
    jpgfile = outpath + os.sep + pfamid + '-itol.jpg'
    thumbfile = outpath + os.sep + "thumb." + pfamid + '-itol.jpg'
    itol_exporter.export(epsfile)
    os.system("epstopdf %s" % epsfile )
    os.system("convert %s %s" % (epsfile, jpgfile) )
    os.system("convert -thumbnail 200 %s %s" % (jpgfile, thumbfile))
    print('exported tree to ',pdffile)
Beispiel #8
0
import os
import sys

current_dir = os.path.dirname(os.path.realpath(__file__))
root_path = os.path.join(current_dir, '..')
sys.path.append(root_path)

from itolapi import Itol, ItolExport  # NOQA

print('Running example itol and itolexport script')
print('')
print('Creating the upload params')

# Create the Itol class
test = Itol()

# Set the tree file
tree = os.path.join(current_dir, 'tree_of_life.tree.txt')
test.add_file(tree)
test.add_file(os.path.join(current_dir, 'colors_tol.txt'))
test.add_file(os.path.join(current_dir, 'labels.txt'))
test.add_file(os.path.join(current_dir, 'ranges.txt'))
# Add parameters
test.params['treeName'] = 'Tree of Life Example'
# Check parameters
test.print_variables()
# Submit the tree
print('')
print((
    'Uploading the tree.  This may take some time depending on how large the '
Beispiel #9
0
import os
import sys

pathname = os.path.dirname(sys.argv[0])
fullpath = os.path.abspath(pathname)
parent_path = fullpath + "/../../"
sys.path.append(parent_path)

from itolapi import Itol

parse_fn = 'huge.matrix.tree.parse'
color_fn = 'huge.matrix.tree.parse.color'
abun_fn = 'huge.matrix.tree.parse.abun'

itol_o = Itol.Itol()
itol_o.add_variable('treeFile', parse_fn)
itol_o.add_variable('treeName', 'HuGE Tree')
itol_o.add_variable('treeFormat', 'newick')
itol_o.add_variable('colorDefinitionFile', color_fn)
itol_o.add_variable('dataset1File', abun_fn)
itol_o.add_variable('dataset1Label', 'Abundance')
itol_o.add_variable('dataset1Separator', 'comma')
itol_o.add_variable('dataset1Type', 'multibar')
itol_o.add_variable('dataset1Color', '#ff0000')
itol_o.add_variable('dataset1MultiBarAlign', '1')
itol_o.add_variable('dataset1PieTransparent', '0')
itol_o.add_variable('dataset1PieRadiusMax', '100')
itol_o.add_variable('dataset1PieRadiusMin', '20')
itol_o.add_variable('dataset1BarSizeMax', '20')
itol_o.print_variables()
Beispiel #10
0
def Itol_Tree_m0(pfamid, datapath, outpath):#{{{
#Create the Itol class
    itl = Itol()
#Set the tree file
    tree = datapath + os.sep + pfamid + '.tree'
    (datafile1, datafile2, datafile3, datafile4) = ("", "", "", "")
    if not os.path.exists(tree):
        print("tree file %s does not exist. Ignore" %(tree), file=sys.stderr)
        return 1
    t = Tree(tree)
    leaves = t.get_leaves()
    numLeave = len(leaves)

    fontsize = GetFontSize(numLeave)

    datafile1 = datapath + os.sep + pfamid + '.numTM_and_io.txt'
#    datafile2 = datapath + os.sep + pfamid + '.cmpclass.colordef.txt'
#    datafile3 = datapath + os.sep + pfamid + '.ntermstate.colordef.txt'
    datafile4 = datapath + os.sep + pfamid + '.cluster.colordef.txt'

    colordeffile = datapath + os.sep + pfamid + '.pfam.colordef.txt'
    branchlabelfile = datapath + os.sep + pfamid + '.branchlabel.txt'

    datafileList = [datafile1, datafile2, datafile3, datafile4, colordeffile, branchlabelfile]

    rootname = os.path.basename(os.path.splitext(tree)[0])

#===================================
    itl.add_file(tree)
    itl.params['treeName'] = rootname
    itl.params['treeFormat'] = 'newick'

    valid_datafileList = []
    for datafile in datafileList:
        if os.path.exists(datafile):
            itl.add_file(datafile)
            valid_datafileList.append(datafile)
    datasets_list = [str(x) for x in range(len(valid_datafileList))]
# Check parameters
# itl.print_variables()

#Submit the tree
    print('')
    print('Uploading the tree.  This may take some time depending on how large the tree is and how much load there is on the itol server')
    good_upload = itl.upload()
    if good_upload == False:
        print('There was an error:'+itl.comm.upload_output)
        sys.exit(1)

#Read the tree ID
    print('Tree ID: '+str(itl.comm.tree_id))

#Read the iTOL API return statement
    print('iTOL output: '+str(itl.comm.upload_output))

#Website to be redirected to iTOL tree
    print('Tree Web Page URL: '+itl.get_webpage())

# Warnings associated with the upload
    print('Warnings: '+str(itl.comm.warnings))

#Export to pdf
    print('Exporting to pdf')
    itol_exporter = itl.get_itol_export()
#itol_exporter = itolexport.ItolExport()
#itol_exporter.set_export_param_value('tree','18793532031912684633930')
    itol_exporter.set_export_param_value('format', 'pdf')
    itol_exporter.set_export_param_value('display_mode',"2")
    #itol_exporter.set_export_param_value('current_font_size',fontsize)
    itol_exporter.set_export_param_value('align_labels',"1")
    itol_exporter.set_export_param_value('datasets_visible',",".join(datasets_list))

    epsfile = outpath + os.sep + pfamid + '-itol.eps'
    pdffile = outpath + os.sep + pfamid + '-itol.pdf'
    jpgfile = outpath + os.sep + pfamid + '-itol.jpg'
    thumbfile = outpath + os.sep + "thumb." + pfamid + '-itol.jpg'
    itol_exporter.export(pdffile)
    #os.system("epstopdf %s" % epsfile )
    os.system("convert %s %s" % (pdffile, jpgfile) )
    os.system("convert -thumbnail 200 %s %s" % (jpgfile, thumbfile))
    print('exported tree to ',pdffile)
Beispiel #11
0
def Itol_Tree_m_sd3(pfamid, datapath, outpath):#{{{
    """Phylogenetic tree with species definition
    the Kindom use branch colordefinition, and others using color strips
    """
    tree = datapath + os.sep + pfamid + '.tree'
    t = Tree(tree)
    leaves = t.get_leaves()
    lst_leaves_name = []
    for leaf in leaves:
        lst_leaves_name.append(leaf.name)
    numLeave = len(lst_leaves_name)
    leaves_name_set = set(lst_leaves_name)

# read species definition
    speciesfile = "%s/%s.species"%(datapath, pfamid)
    speciesDict = myfunc.Read_species_sd(speciesfile)

# create branch color definition file for kingdom
    lst_kingdom = ["Archaea","Bacteria", "Eukaryota" ]
    lst_color_kingdom = ["#ff0000", "#0066ff","#cc6600"]
    species_colordef_file = "%s/%s.kingdom.colordef.txt"%(outpath, pfamid)
    color_dict_kingdom = {}
    this_speciesDict = {}
    for seqid in speciesDict:
        speciesname = speciesDict[seqid][0]
        this_speciesDict[seqid] = speciesname
    for i in range(len(lst_kingdom)):
        idd = lst_kingdom[i]
        color_dict_kingdom[idd] = lst_color_kingdom[i]
    myfunc.WriteKingdomColorDefFile(species_colordef_file, this_speciesDict, leaves_name_set, color_dict_kingdom)

# generate the next three levels of classification
    for level in [1,2,3]:
        outfile = "%s/%s.species.level_%d.txt"%(outpath, pfamid, level)
        this_speciesDict = {}
        speciesIDSet = set([])
        for seqid in speciesDict:
            try:
                speciesname = speciesDict[seqid][level]
                speciesIDSet.add(speciesname)
                this_speciesDict[seqid] = speciesname
            except IndexError as KeyError:
                pass
        color_dict = {}
        lst_color = list(blue.range_to(red,len(speciesIDSet)))
        lst_speciesID = list(speciesIDSet)
        for i in range(len(lst_speciesID)):
            idd = lst_speciesID[i]
            color_dict[idd] = lst_color[i].get_hex_l()
        myfunc.WriteSpeciesColorStripDefFile(outfile, this_speciesDict, leaves_name_set, color_dict)

#Create the Itol class
    itl = Itol()
#Set the tree file
    (datafile1, datafile2, datafile3, datafile4) = ("", "", "", "")
    if not os.path.exists(tree):
        print("tree file %s does not exist. Ignore" %(tree), file=sys.stderr)
        return 1

    fontsize = GetFontSize(numLeave)

    datafile1 = "%s/%s.species.level_%d.txt"%(outpath, pfamid, 1)
    datafile2 = "%s/%s.species.level_%d.txt"%(outpath, pfamid, 2)
    datafile3 = "%s/%s.species.level_%d.txt"%(outpath, pfamid, 3)
    colordeffile = species_colordef_file

    if os.path.exists(colordeffile):
        itl.add_variable('colorDefinitionFile', colordeffile)
        itl.add_variable('colorDefinitionLabel', "Kingdom")

#===================================
    itl.add_variable('treeFile',tree)
    itl.add_variable('treeName','SD3')
    itl.add_variable('treeFormat','newick')
#===================================
    if os.path.exists(datafile1):
        itl.add_variable('datafile1File',datafile1)
        itl.add_variable('datafile1Label','Phylum')
        itl.add_variable('datafile1Separator','comma')
        itl.add_variable('datafile1Type','colorstrip')
        itl.add_variable('datafile1StripWidth','100')
        itl.add_variable('datafile1ColoringType','both')
        itl.add_variable('datafile1PreventOverlap','1')
#===================================
    if os.path.exists(datafile2):
        itl.add_variable('datafile2File',datafile2)
        itl.add_variable('datafile2Label','Class')
        itl.add_variable('datafile2Separator','comma')
        itl.add_variable('datafile2Type','colorstrip')
        itl.add_variable('datafile2StripWidth','100')
        itl.add_variable('datafile2ColoringType','both')
        itl.add_variable('datafile2PreventOverlap','1')
#===================================
    if os.path.exists(datafile3):
        itl.add_variable('datafile3File',datafile3)
        itl.add_variable('datafile3Label','Order')
        itl.add_variable('datafile3Separator','comma')
        itl.add_variable('datafile3Type','colorstrip')
        itl.add_variable('datafile3StripWidth','100')
        itl.add_variable('datafile3ColoringType','both')
        itl.add_variable('datafile3PreventOverlap','1')
#===================================
# Check parameters
# itl.print_variables()

#Submit the tree
    print('')
    print('Uploading the tree.  This may take some time depending on how large the tree is and how much load there is on the itol server')
    good_upload = itl.upload()
    if good_upload == False:
        print('There was an error:'+itl.comm.upload_output)
        sys.exit(1)

#Read the tree ID
    print('Tree ID: '+str(itl.comm.tree_id))

#Read the iTOL API return statement
    print('iTOL output: '+str(itl.comm.upload_output))

#Website to be redirected to iTOL tree
    print('Tree Web Page URL: '+itl.get_webpage())

# Warnings associated with the upload
    print('Warnings: '+str(itl.comm.warnings))

#Export to pdf
    itol_exporter = itl.get_itol_export()
#itol_exporter = itolexport.ItolExport()
#itol_exporter.set_export_param_value('tree','18793532031912684633930')
    itol_exporter.set_export_param_value('format', 'eps')
    itol_exporter.set_export_param_value('display_mode',"2")
    itol_exporter.set_export_param_value('current_font_size',fontsize)
    itol_exporter.set_export_param_value('align_labels',"1")
    itol_exporter.set_export_param_value('datafileList','dataset1')
    extname = "-itol-sd3"
    epsfile = outpath + os.sep + pfamid + extname + '.eps'
    pdffile = outpath + os.sep + pfamid + extname + '.pdf'
    jpgfile = outpath + os.sep + pfamid + extname + '.jpg'
    pngfile = outpath + os.sep + pfamid + extname + '.png'
    thumbfile = outpath + os.sep + "thumb." + pfamid + extname + '.jpg'
    itol_exporter.export(epsfile)
    os.system("epstopdf %s" % epsfile )
    os.system("convert %s %s" % (epsfile, jpgfile) )
    os.system("convert -thumbnail 200 %s %s" % (jpgfile, thumbfile))
    print('exported tree to ',pdffile)
Beispiel #12
0
 def setUp(self):
     self.itol = Itol()
Beispiel #13
0
class ItolTest(unittest.TestCase):

    def setUp(self):
        self.itol = Itol()

    def test_initializes(self):
        self.assertEqual(type(self.itol.params), dict)
        self.assertEqual(self.itol.files, [])
        self.assertIsNotNone(self.itol.comm)

    def test_add_file(self):
        with tempfile.NamedTemporaryFile() as temp:
            self.itol.add_file(temp.name)
        with self.assertRaises(IOError):
            self.itol.add_file(' ')

    def test_good_upload(self):
        with patch('itolapi.Comm.upload_tree') as mock_upload:
            mock_upload.return_value = True
            self.itol.comm.tree_id = 1234
            self.assertEqual(self.itol.upload(), 1234)

    def test_bad_upload(self):
        with patch('itolapi.Comm.upload_tree') as mock_upload:
            mock_upload.return_value = False
            self.itol.comm.tree_id = 1234
            self.assertEqual(self.itol.upload(), 0)

    def test_get_webpage(self):
        self.itol.comm.tree_id = 'asdf'
        webpage = self.itol.get_webpage()
        self.assertIn('itol.embl.de', webpage)
        self.assertIn('asdf', webpage)

    def test_get_itol_export(self):
        self.itol.comm.tree_id = 1234
        export = self.itol.get_itol_export()
        self.assertEqual(export.params['tree'], 1234)

    @patch('itolapi.itol.pprint.pprint')
    def test_print_variables(self, mock_print):
        self.itol.params['treeName'] = 'test'
        with tempfile.NamedTemporaryFile() as temp:
            self.itol.add_file(temp.name)
            self.itol.print_variables()
        self.assertEqual(mock_print.call_args_list[0][0][0], self.itol.files)
        self.assertEqual(mock_print.call_args_list[1][0][0], self.itol.params)
Beispiel #14
0
if args.root == 'midpoint':
    root = '1'
else:
    root = args.root

# Modify tree
cmd = 'java -Xmx16m -jar ~/bin/TreeCollapseCL4.jar -f %s -b %s -r %s' %(args.tree, args.boot, root)
os.system(cmd)
prefix, suffix = re.search('(.*?)\.(.*?)$', args.tree).groups()
tree = '%s_%scoll.%s' %(prefix, args.boot, suffix)
args.tree = '%s.tmp' %(args.tree)
cmd = 'mv %s %s' %(tree, args.tree)
os.system(cmd)

# Upload tree
uploader = Itol.Itol()
uploader.add_variable('treeFile', args.tree)
uploader.add_variable('treeFormat', 'newick')
uploader.add_variable('treeName', args.out)
if args.root == 'midpoint':
    uploader.add_variable('midpointRoot', '1')
uploader.upload()
tree_id = uploader.comm.tree_id

# Export tree
exporter = ItolExport.ItolExport()
exporter.set_export_param_value('tree', tree_id)
exporter.set_export_param_value('format', 'pdf')
exporter.set_export_param_value('displayMode', args.mode)
exporter.export(args.out)
Beispiel #15
0
def upload_to_itol(
    tree,
    apiKey,
    projectName,
    tree_name="test",
    files=[],
    outfp="test.pdf",
    fformat=None,
    rect=False,
    **kwargs,
):

    _leaves = tree.get_leaf_names()
    tree.write(outfile="tree_to_plot.tree", format=1)

    if fformat is None:
        fformat = outfp.split(".")[-1]

    itol_uploader = Itol()
    itol_uploader.add_file("tree_to_plot.tree")

    for file in files:
        itol_uploader.add_file(file)

    itol_uploader.params["treeName"] = tree_name
    itol_uploader.params["APIkey"] = apiKey
    itol_uploader.params["projectName"] = projectName

    good_upload = itol_uploader.upload()
    if not good_upload:
        print("There was an error:" + itol_uploader.comm.upload_output)
    print("iTOL output: " + str(itol_uploader.comm.upload_output))
    print("Tree Web Page URL: " + itol_uploader.get_webpage())
    print("Warnings: " + str(itol_uploader.comm.warnings))

    tree_id = itol_uploader.comm.tree_id

    itol_exporter = ItolExport()

    # set parameters:
    itol_exporter.set_export_param_value("tree", tree_id)
    itol_exporter.set_export_param_value(
        "format",
        outfp.split(
            ".")[-1])  # ['png', 'svg', 'eps', 'ps', 'pdf', 'nexus', 'newick']
    if rect:
        itol_exporter.set_export_param_value("display_mode",
                                             1)  # rectangular tree
    else:
        itol_exporter.set_export_param_value("display_mode",
                                             2)  # circular tree
        itol_exporter.set_export_param_value("arc", 359)
        itol_exporter.set_export_param_value("rotation", 270)

    itol_exporter.set_export_param_value("leaf_sorting", 1)
    itol_exporter.set_export_param_value("label_display", 1)
    itol_exporter.set_export_param_value("internal_marks", 1)
    itol_exporter.set_export_param_value("ignore_branch_length", 1)

    itol_exporter.set_export_param_value(
        "datasets_visible", ",".join([str(i) for i in range(len(files))]))

    itol_exporter.set_export_param_value(
        "horizontal_scale_factor", 1)  # doesnt actually scale the artboard

    # export!
    itol_exporter.export(outfp)

    os.remove("tree_to_plot.tree")
Beispiel #16
0
def Itol_Tree_m_sd1(pfamid, datapath, outpath):#{{{
    """Phylogenetic tree with numTM_io and subfamilies branch coloring
    """
    tree = datapath + os.sep + pfamid + '.tree'
    t = Tree(tree)
    leaves = t.get_leaves()
    lst_leaves_name = []
    for leaf in leaves:
        lst_leaves_name.append(leaf.name)
    numLeave = len(lst_leaves_name)
# read subfamily definition
    subfamfile = "%s/%s.subfamilies"%(datapath, pfamid)
    subfam_idlist = []
    subfamDict = myfunc.Read_subfamily(subfamfile, subfam_idlist)
    numSubFam = len(subfam_idlist)

# create subfamily branch color definition file
    subfam_colordef_file = "%s/%s.subfamilies.colordef.txt"%(outpath, pfamid)
    lst_color = list(blue.range_to(red,numSubFam))
    color_dict = {}
    for i in range(numSubFam):
        famid = subfam_idlist[i]
        color = lst_color[i].get_hex_l()
        color_dict[famid] = lst_color[i].get_hex_l()
    myfunc.WriteSubFamColorDef(subfam_colordef_file, subfamDict, lst_leaves_name, color_dict)

#Create the Itol class
    itl = Itol()
#Set the tree file
    (datafile1, datafile2, datafile3, datafile4) = ("", "", "", "")
    if not os.path.exists(tree):
        print("tree file %s does not exist. Ignore" %(tree), file=sys.stderr)
        return 1

    fontsize = GetFontSize(numLeave)

    datafile1 = datapath + os.sep + pfamid + '.numTM_and_io.txt'
    colordeffile = subfam_colordef_file

    if os.path.exists(colordeffile):
        itl.add_variable('colorDefinitionFile', colordeffile)
        itl.add_variable('colorDefinitionLabel', "Subfamilies")

#===================================
    itl.add_variable('treeFile',tree)
    itl.add_variable('treeName','SD1')
    itl.add_variable('treeFormat','newick')
#===================================
    if os.path.exists(datafile1):
        itl.add_variable('datafile1File',datafile1)
        itl.add_variable('datafile1Label','numTM_and_io')
        itl.add_variable('datafile1Separator','comma')
        itl.add_variable('datafile1Type','multibar')
        itl.add_variable('datafile1PreventOverlap','1')
        itl.add_variable('datafile1Color','#FF0000')
#===================================
# Check parameters
# itl.print_variables()

#Submit the tree
    print('')
    print('Uploading the tree.  This may take some time depending on how large the tree is and how much load there is on the itol server')
    good_upload = itl.upload()
    if good_upload == False:
        print('There was an error:'+itl.comm.upload_output)
        sys.exit(1)

#Read the tree ID
    print('Tree ID: '+str(itl.comm.tree_id))

#Read the iTOL API return statement
    print('iTOL output: '+str(itl.comm.upload_output))

#Website to be redirected to iTOL tree
    print('Tree Web Page URL: '+itl.get_webpage())

# Warnings associated with the upload
    print('Warnings: '+str(itl.comm.warnings))

#Export to pdf
    itol_exporter = itl.get_itol_export()
#itol_exporter = itolexport.ItolExport()
#itol_exporter.set_export_param_value('tree','18793532031912684633930')
    itol_exporter.set_export_param_value('format', 'eps')
    itol_exporter.set_export_param_value('display_mode',"2")
    itol_exporter.set_export_param_value('current_font_size',fontsize)
    itol_exporter.set_export_param_value('align_labels',"1")
    itol_exporter.set_export_param_value('datafileList','dataset1')
    extname = "-itol-sd1"
    epsfile = outpath + os.sep + pfamid + extname + '.eps'
    pdffile = outpath + os.sep + pfamid + extname + '.pdf'
    jpgfile = outpath + os.sep + pfamid + extname + '.jpg'
    pngfile = outpath + os.sep + pfamid + extname + '.png'
    thumbfile = outpath + os.sep + "thumb." + pfamid + extname + '.jpg'
    itol_exporter.export(epsfile)
    os.system("epstopdf %s" % epsfile )
    os.system("convert %s %s" % (epsfile, jpgfile) )
    os.system("convert -thumbnail 200 %s %s" % (jpgfile, thumbfile))
    print('exported tree to ',pdffile)
Beispiel #17
0
def Itol_Tree_m_sd2(pfamid, datapath, outpath):#{{{
    tree = datapath + os.sep + pfamid + '.tree'
    t = Tree(tree)
    leaves = t.get_leaves()
    lst_leaves_name = []
    for leaf in leaves:
        lst_leaves_name.append(leaf.name)
    numLeave = len(lst_leaves_name)
    leaves_name_set = set(lst_leaves_name)
# read seqlen file
    seqlenfile = "%s/%s.seqlen.txt"%(datapath, pfamid)
    seqlen_dict = myfunc.ReadSeqLengthDict(seqlenfile)

# read subfamily definition
    domain_idlist = []
    domainfile = "%s/%s.mdp"%(datapath, pfamid)
    domain_dict = myfunc.Read_domain_sd(domainfile, domain_idlist)
    domain_colordef_file = "%s/%s.mdp.colordef.txt"%(datapath, pfamid)
    WriteDomainColorDefFile(domain_colordef_file, domain_dict, domain_idlist, seqlen_dict, leaves_name_set)

#Create the Itol class
    itl = Itol()
#Set the tree file
    (datafile1, datafile2, datafile3, datafile4) = ("", "", "", "")
    if not os.path.exists(tree):
        print("tree file %s does not exist. Ignore" %(tree), file=sys.stderr)
        return 1

    fontsize = GetFontSize(numLeave)

    datafile1 = domain_colordef_file
#     colordeffile = subfam_colordef_file
#     if os.path.exists(colordeffile):
#         itl.add_variable('colorDefinitionFile', colordeffile)
#         itl.add_variable('colorDefinitionLabel', "Subfamilies")
#===================================
    itl.add_variable('treeFile',tree)
    itl.add_variable('treeName','SD2')
    itl.add_variable('treeFormat','newick')
#===================================
    if os.path.exists(datafile1):
        itl.add_variable('datafile1File',datafile1)
        itl.add_variable('datafile1Label','Domain architecture')
        itl.add_variable('datafile1Separator','comma')
        itl.add_variable('datafile1Type','domains')
        itl.add_variable('datafile1ProtSizeMax','1000')
        itl.add_variable('datafile1PreventOverlap','1')
        itl.add_variable('datafile1CirclesSpacing','100')
#===================================
# Check parameters
# itl.print_variables()

#Submit the tree
    print('')
    print('Uploading the tree.  This may take some time depending on how large the tree is and how much load there is on the itol server')
    good_upload = itl.upload()
    if good_upload == False:
        print('There was an error:'+itl.comm.upload_output)
        sys.exit(1)

#Read the tree ID
    print('Tree ID: '+str(itl.comm.tree_id))

#Read the iTOL API return statement
    print('iTOL output: '+str(itl.comm.upload_output))

#Website to be redirected to iTOL tree
    print('Tree Web Page URL: '+itl.get_webpage())

# Warnings associated with the upload
    print('Warnings: '+str(itl.comm.warnings))

#Export to pdf
    itol_exporter = itl.get_itol_export()
#itol_exporter = itolexport.ItolExport()
#itol_exporter.set_export_param_value('tree','18793532031912684633930')
    itol_exporter.set_export_param_value('format', 'eps')
    itol_exporter.set_export_param_value('display_mode',"2")
    itol_exporter.set_export_param_value('current_font_size',fontsize)
    itol_exporter.set_export_param_value('align_labels',"1")
    itol_exporter.set_export_param_value('datafileList','dataset1')
    extname = "-itol-sd2"
    epsfile = outpath + os.sep + pfamid + extname + '.eps'
    pdffile = outpath + os.sep + pfamid + extname + '.pdf'
    jpgfile = outpath + os.sep + pfamid + extname + '.jpg'
    pngfile = outpath + os.sep + pfamid + extname + '.png'
    thumbfile = outpath + os.sep + "thumb." + pfamid + extname + '.jpg'
    itol_exporter.export(epsfile)
    os.system("epstopdf %s" % epsfile )
    os.system("convert %s %s" % (epsfile, jpgfile) )
    os.system("convert -thumbnail 200 %s %s" % (jpgfile, thumbfile))
    print('exported tree to ',pdffile)
Beispiel #18
0
 def setUp(self):
     self.itol = Itol()
Beispiel #19
0
def Itol_Tree_linear(treefile, fastafile, outpath):# {{{
    """
    Generate itol tree linear 
    """
    if not treefile.endswith(".tree") or treefile.endswith(".tree.txt"):
        newfile = treefile + ".tree"
        shutil.copy2(treefile, newfile)
        treefile = newfile

#Create the Itol class
    itl = Itol()
#Set the tree file
    tree = treefile

    dirname = os.path.dirname(treefile)
    if dirname == "":
        dirname = "."
    if outpath == "":
        outpath = dirname
    elif not os.path.exists(outpath):
        os.system("mkdir -p %s"%(outpath))

    rootname = os.path.basename(os.path.splitext(treefile)[0])
    rtname_fastafile = os.path.basename(os.path.splitext(fastafile)[0])
    colorragefile = dirname + os.sep + rtname_fastafile + ".colorrage.txt"

    datafileList = [colorragefile]

    if os.path.exists(fastafile):
        cmd = "python %s/fasta2colorrange.py %s %s > %s"%(rundir, fastafile, treefile,colorragefile)
        os.system(cmd)

#===================================
    itl.add_file(tree)
    itl.params['treeName'] = rootname
    itl.params['treeFormat'] = 'newick'

    valid_datafileList = []
    for datafile in datafileList:
        if os.path.exists(datafile):
            itl.add_file(datafile)
            valid_datafileList.append(datafile)
    datasets_list = [str(x) for x in range(len(valid_datafileList))]
#===================================
# Check parameters
# itl.print_variables()
#Submit the tree
    print('')
    good_upload = itl.upload()
    if good_upload == False:
        print('There was an error:'+itl.comm.upload_output)
        return 1

#Export to pdf
    tree_id = itl.comm.tree_id

    itol_exporter = itl.get_itol_export()
    itol_exporter.set_export_param_value('format',"pdf")
    itol_exporter.set_export_param_value('display_mode',"1") #(1=normal, 2=circular, 3=unrooted)
    itol_exporter.set_export_param_value('line_width',"1")
    #itol_exporter.set_export_param_value('align_labels',"1")
    print(('datasets_visible',",".join(datasets_list)))
    itol_exporter.set_export_param_value('datasets_visible',",".join(datasets_list))
    #epsfile = outpath + os.sep + rootname + '.itolnormal.eps'
    pdffile = outpath + os.sep + rootname + '.itol_linear.pdf'
    print('Exporting to pdffile %s'%(pdffile))
    itol_exporter.export(pdffile)

    print(("Phylogenetic tree has been output to %s"%(pdffile)))
Beispiel #20
0
class ItolTest(unittest.TestCase):

    def setUp(self):
        self.itol = Itol()

    def test_initializes(self):
        self.assertEqual(type(self.itol.params), dict)
        self.assertEqual(self.itol.files, [])
        self.assertIsNotNone(self.itol.comm)

    def test_add_file(self):
        with tempfile.NamedTemporaryFile() as temp:
            self.itol.add_file(temp.name)
        with self.assertRaises(IOError):
            self.itol.add_file(' ')

    def test_good_upload(self):
        with patch('itolapi.Comm.upload_tree') as mock_upload:
            mock_upload.return_value = True
            self.itol.comm.tree_id = 1234
            self.assertEqual(self.itol.upload(), 1234)

    def test_bad_upload(self):
        with patch('itolapi.Comm.upload_tree') as mock_upload:
            mock_upload.return_value = False
            self.itol.comm.tree_id = 1234
            self.assertEqual(self.itol.upload(), 0)

    def test_get_webpage(self):
        self.itol.comm.tree_id = 'asdf'
        webpage = self.itol.get_webpage()
        self.assertIn('itol.embl.de', webpage)
        self.assertIn('asdf', webpage)

    def test_get_itol_export(self):
        self.itol.comm.tree_id = 1234
        export = self.itol.get_itol_export()
        self.assertEqual(export.params['tree'], 1234)

    @patch('itolapi.itol.pprint.pprint')
    def test_print_variables(self, mock_print):
        self.itol.params['treeName'] = 'test'
        with tempfile.NamedTemporaryFile() as temp:
            self.itol.add_file(temp.name)
            self.itol.print_variables()
        self.assertEqual(mock_print.call_args_list[0][0][0], self.itol.files)
        self.assertEqual(mock_print.call_args_list[1][0][0], self.itol.params)
Beispiel #21
0
class ITOL():
    """

    Tree with branch lengths::

       (A:0.1,(B:0.1,C:0.1)));

    Tree with bootstrap and branch lengths::

       (A:0.1,(B:0.1,C:0.1)90:0.1)98:0.3);

    .. plot::

        from pylab import imshow, imread
        from easydev import TempFile
        from sequana import ITOL, sequana_data

        itol = ITOL(sequana_data("test_itol_basic.tree.txt"))
        itol.upload()
        # You can change the parameters in itol.params
        itol.params["display_mode"] = 1  # use linear layout instead of circular

        # finally export your image locally:
        with TempFile(suffix=".png") as fout:
            itol.export(fout.name)
            imshow(imread(fout.name))

    """
    def __init__(self, tree):
        self.itol = Itol()
        assert tree.endswith(
            ".tree.txt"), "Your input tree must end in .tree.txt"
        self.itol.add_file(tree)
        self._datasets = 0
        self.status = None

        # datasets_visible should not be used if there is no datasets.
        #
        self.params = {
            "display_mode": 2,  # rotation
            "ignore_branch_length": 1,
            "line_width": 5,
            #"datasets_visible": "0,1",
            'bootstrap_display': 1,
            'bootstrap_type': 2,
            'bootstrap_label_size': 32
        }

    def add_file(self, filename):
        self.itol.add_file(filename)
        N = len(self.itol.files) - 1  # remove the input tree file
        self.params['datasets_visible'] = ",".join(
            [str(x) for x in range(0, N)])

    def upload(self):
        self.status = self.itol.upload()
        assert self.status, "Something is wrong with your input tree"

    def export(self, filename='test.png'):
        if self.status is None:
            logger.error("Upload the tree first with upload() method")

        export = self.itol.get_itol_export()

        # Set the format
        if filename.endswith(".png"):
            logger.info("Exporting in {} format".format("png"))
            export.params['format'] = "png"
        elif filename.endswith(".svg"):
            logger.info("Exporting in {} format".format("svg"))
            export.params['format'] = "svg"
        elif filename.endswith(".pdf"):
            logger.info("Exporting in {} format".format("pdf"))
            export.params['format'] = "pdf"
        elif filename.endswith(".eps"):
            logger.info("Exporting in {} format".format("eps"))
            export.params['format'] = "eps"
        else:
            raise ValueError("filename must end in pdf, png, svg or eps")

        export.params.update(**self.params)

        export.export(filename)
Beispiel #22
0
def upload_and_export_itol(
    cassiopeia_tree: CassiopeiaTree,
    tree_name: str,
    export_filepath: str,
    itol_config: str = "~/.itolconfig",
    api_key: Optional[str] = None,
    project_name: Optional[str] = None,
    meta_data: List[str] = [],
    allele_table: Optional[pd.DataFrame] = None,
    indel_colors: Optional[pd.DataFrame] = None,
    indel_priors: Optional[pd.DataFrame] = None,
    rect: bool = False,
    include_legend: bool = False,
    use_branch_lengths: bool = False,
    palette: List[str] = palettes.Category20[20],
    random_state: Optional[np.random.RandomState] = None,
    user_dataset_files: Optional[List[str]] = None,
    verbose: bool = True,
    **kwargs,
):
    """Uploads a tree to iTOL and exports it.

    This function takes in a tree, plots it with iTOL, and exports it locally.
    A user can also specify meta data and allele tables to visualize alongside
    their tree. The function requires a user to have an account with iTOL and
    can pass these credentials to the function in one of two ways: first, the
    user can specify an `api_key` and a `project_name`, which corresponds to one
    in the user's iTOL account' second, the user can have a hidden `itolconfig`
    file that can store these credentials (the default location we will check
    is in ~/.itolconfig, though this can be overridden by the user). We
    preferentially take values passed in explicitly to the function in the
    `api_key` and `project_name` arguments.

    TODO(mgjones): Add the ability to pass in min/max colors for specific
        numeric meta data to use when creating the gradient files.

    Args:
        cassiopeia_tree: A CassiopeiaTree instance, populated with a tree.
        tree_name: Name of the tree. This is what the tree will be called
            within your project directory on iTOL
        export_filepath: Output file path to save your tree. Must end with
            one of the following suffixes: ['png', 'svg', 'eps', 'ps', 'pdf'].
        itol_config: A configuration file that a user can maintain for storing
            iTOL account details (specifically, an API key and a project name
            for uploading trees). We assume that the information is stored under
            the [DEFAULT] header. We also will be default check the path
            `~/itolconfig` but the user can pass in another path should they
            wish. If an `api_key` and `project_name` are also passed in, we
            will preferentially take those values.
        api_key: API key linking to your iTOL account
        project_name: Project name to upload to.
        meta_data: Meta data to plot alongside the tree, which must be columns
            in the CassiopeiaTree.cell_meta variable.
        allele_table: Alleletable to plot alongside the tree.
        indel_colors: Color mapping to use for plotting the alleles for each
            cell. Only necessary if `allele_table` is specified.
        indel_priors: Prior probabilities for each indel. Only useful if an
            allele table is to be plotted and `indel_colors` is None.
        rect: Boolean indicating whether or not to save your tree as a circle
            or rectangle.
        use_branch_lengths: Whether or not to use branch lengths when exporting
            the tree.
        include_legend: Plot legend along with meta data.
        palette: A palette of colors in hex format.
        random_state: A random state for reproducibility
        user_dataset_files: List of paths to additional dataset files to upload.
            See https://itol.embl.de/help.cgi#dsType for available dataset types
            and their definitions.
        verbose: Include extra print statements.
        **kwargs: additional keyword arguments are passed as iTOL export parameters.
            See https://itol.embl.de/help.cgi#batch for a full list.

    Raises:
        iTOLError if iTOL credentials cannot be found, if the output format is
            not supported, if meta data to be plotted cannot be found, or if
            an error with iTOL is encountered.
    """

    # create temporary directory for storing files we'll upload to iTOL
    temporary_directory = tempfile.mkdtemp()

    if api_key is None or project_name is None:
        if os.path.exists(os.path.expanduser(itol_config)):

            config = configparser.ConfigParser()
            with open(os.path.expanduser(itol_config), "r") as f:
                config_string = f.read()
            config.read_string(config_string)

            try:
                api_key = config["DEFAULT"]["api_key"]
                project_name = config["DEFAULT"]["project_name"]
            except KeyError:
                raise iTOLError(
                    "Error reading the itol config file passed in.")

        else:
            raise iTOLError(
                "Specify an api_key and project_name, or a valid iTOL "
                "config file.")

    with open(os.path.join(temporary_directory, "tree_to_plot.tree"),
              "w") as f:
        f.write(cassiopeia_tree.get_newick(record_branch_lengths=True))

    file_format = export_filepath.split("/")[-1].split(".")[-1]

    if file_format not in ["png", "svg", "eps", "ps", "pdf"]:
        raise iTOLError("File format must be one of "
                        "'png', 'svg', 'eps', 'ps', 'pdf']")

    itol_uploader = Itol()
    itol_uploader.add_file(
        os.path.join(temporary_directory, "tree_to_plot.tree"))

    files = user_dataset_files.copy() if user_dataset_files else []
    if allele_table is not None:
        files += create_indel_heatmap(
            allele_table,
            cassiopeia_tree,
            f"{tree_name}.allele",
            temporary_directory,
            indel_colors,
            indel_priors,
            random_state,
        )

    for meta_item in meta_data:
        if meta_item not in cassiopeia_tree.cell_meta.columns:
            raise iTOLError("Meta data item not in CassiopeiaTree cell meta.")

        values = cassiopeia_tree.cell_meta[meta_item]

        if pd.api.types.is_numeric_dtype(values):
            files.append(
                create_gradient_from_df(
                    values,
                    cassiopeia_tree,
                    f"{tree_name}.{meta_item}",
                    temporary_directory,
                ))

        if pd.api.types.is_string_dtype(values):
            colors = palette[:len(values.unique())]
            colors = [utilities.hex_to_rgb(color) for color in colors]
            colormap = dict(zip(np.unique(values), colors))

            files.append(
                create_colorbar(
                    values,
                    cassiopeia_tree,
                    colormap,
                    f"{tree_name}.{meta_item}",
                    temporary_directory,
                    create_legend=include_legend,
                ))

    for _file in files:
        itol_uploader.add_file(_file)

    itol_uploader.params["treeName"] = tree_name
    itol_uploader.params["APIkey"] = api_key
    itol_uploader.params["projectName"] = project_name

    good_upload = itol_uploader.upload()
    if not good_upload:
        raise iTOLError(itol_uploader.comm.upload_output)

    if verbose:
        print("iTOL output: " + str(itol_uploader.comm.upload_output))
        print("Tree Web Page URL: " + itol_uploader.get_webpage())
        print("Warnings: " + str(itol_uploader.comm.warnings))

    tree_id = itol_uploader.comm.tree_id

    itol_exporter = ItolExport()

    # set parameters:
    itol_exporter.set_export_param_value("tree", tree_id)
    itol_exporter.set_export_param_value("format", file_format)
    if rect:
        # rectangular tree settings
        itol_exporter.set_export_param_value("display_mode", 1)
    else:
        # circular tree settings
        itol_exporter.set_export_param_value("display_mode", 2)
        itol_exporter.set_export_param_value("arc", 359)
        itol_exporter.set_export_param_value("rotation", 270)

    itol_exporter.set_export_param_value("leaf_sorting", 1)
    itol_exporter.set_export_param_value("label_display", 0)
    itol_exporter.set_export_param_value("internal_marks", 0)
    itol_exporter.set_export_param_value("ignore_branch_length",
                                         1 - int(use_branch_lengths))

    itol_exporter.set_export_param_value(
        "datasets_visible", ",".join([str(i) for i in range(len(files))]))

    itol_exporter.set_export_param_value("horizontal_scale_factor", 1)

    for key, value in kwargs.items():
        itol_exporter.set_export_param_value(key, value)

    # export!
    itol_exporter.export(export_filepath)

    # remove intermediate files
    shutil.rmtree(temporary_directory)
Beispiel #23
0
chosentree = args.tree

family_domain_ID = args.tree.split(".")[0]

current_dir = os.path.dirname(os.path.realpath(__file__))
root_path = os.path.join(current_dir, '..')
sys.path.append(root_path)

from itolapi import Itol, ItolExport  # NOQA

print('Running example itol and itolexport script')
print('')
print('Creating the upload params')

# Create the Itol class
test = Itol()

# Set the tree file
tree = os.path.join(current_dir, args.tree)
test.add_file(tree)
test.add_file(os.path.join(current_dir, args.colour))
#test.add_file(os.path.join(current_dir, 'labels.txt'))
#test.add_file(os.path.join(current_dir, 'ranges.txt'))
# Add parameters
test.params['treeName'] = 'Tree File'
# Check parameters
test.print_variables()
# Submit the tree
print('')
print(
    ('Uploading the tree.  This may take some time depending on how large the '
Beispiel #24
0
def upload_to_itol(tree_path,
                   dataset_paths,
                   tree_name=None,
                   tree_description=None,
                   project_name=None,
                   upload_id=None):
    try:
        itol_uploader = Itol()
        itol_uploader.add_file(tree_path)
        for annotation_file in dataset_paths:
            itol_uploader.add_file(annotation_file)
        if tree_name:
            itol_uploader.params['treeName'] = tree_name
        if tree_description:
            itol_uploader.params['treeDescription'] = tree_description
        if upload_id:
            itol_uploader.params['uploadID'] = upload_id
            if project_name:
                itol_uploader.params['projectName'] = project_name
        if itol_uploader.upload():
            logging.getLogger('pastml').debug(
                'Successfully uploaded your tree ({}) to iTOL: {}.'.format(
                    itol_uploader.comm.tree_id, itol_uploader.get_webpage()))
            return itol_uploader.comm.tree_id, itol_uploader.get_webpage()
        else:
            status = itol_uploader.comm.upload_output
    except Exception as e:
        status = e
    logging.getLogger('pastml').error(
        'Failed to upload your tree to iTOL because of "{}". Please check your internet connection and itol settings{}.'
        .format(status, (', e.g. your iTOL batch upload id ({}){}'.format(
            upload_id,
            (' and whether the project {} exists'.format(project_name)
             if project_name else ''))) if upload_id else ''))
    return None, None
Beispiel #25
0
def plot(output_dir: str,
         tree: NewickFormat,
         feature_metadata: pd.DataFrame,
         category: str = 'class',
         ms2_label: bool = False,
         color_palette: str = 'Dark2',
         parent_mz: bool = True,
         grouped_table: biom.Table = None,
         normalize_features: bool = True) -> None:
    '''This function plots the phenetic tree in iTOL with clade colors,
    feature labels and relative abundance per sample group.

    Parameters
    ----------
    tree : NewickFormat
        Phenetic tree
    feature_metadata : pd.DataFrame
        Feature metadata
    grouped_table : biom.Table, optional
        Feature table of samples grouped by categories
    category : str, optional
        The feature data column used to color and label the tips.
        Default 'class'
    color_palette : str, optional
        The color palette to use for coloring tips
    ms2_label : bool, optional
        Whether to label the tips with the MS2 value
    parent_mz : bool, optional
        If the feature is unclassified, label the tips using parent mass of
        compound
    normalize_features : bool, optional
        If True (default), the feature abundances are normalized to
        a constant sum i.e converted to relative abundances

    Raises
    ------
    ValueError
        If `category` is not a column in `feature_metadata`
    UserWarning
        If the number of unique values in `category` is greater than the number
        of unique colors in `color_palette`

    Returns
    -------
    None
    '''

    if category not in feature_metadata.columns:
        raise ValueError(
            'Could not find %s in the feature data, the available'
            ' columns are: %s.' %
            (category, ', '.join(feature_metadata.columns.astype())))

    color_fp = join(output_dir, 'colors')
    with open(color_fp, 'w') as fh:
        fh.write(format_colors(feature_metadata, category, color_palette))

    label_fp = join(output_dir, 'labels')
    with open(label_fp, 'w') as fh:
        fh.write(
            format_labels(feature_metadata, category, ms2_label, parent_mz))

    # itol won't accept a tree file unless it has a .tree or .txt extension
    target = join(output_dir, 'qemistree.tree')
    copyfile(str(tree), target)

    # upload the tree, labels and tip coloring files
    itol_uploader = Itol()
    itol_uploader.add_file(target)
    itol_uploader.add_file(label_fp)
    itol_uploader.add_file(color_fp)
    if grouped_table is not None:
        barplot_fp = join(output_dir, 'barplots')
        with open(barplot_fp, 'w') as fh:
            fh.write(format_barplots(grouped_table, normalize_features))
        itol_uploader.add_file(barplot_fp)

    status = itol_uploader.upload()

    if not status:
        raise ValueError(itol_uploader.comm.upload_output)

    url = itol_uploader.get_webpage()
    print(url)

    index_path = join(TEMPLATES, 'index.html')
    q2templates.render(index_path,
                       output_dir,
                       context={
                           'url': url,
                           'has_barplots': grouped_table is not None
                       })
Beispiel #26
0
import os
import sys

pathname = os.path.dirname(sys.argv[0])
fullpath = os.path.abspath(pathname)
parent_path = fullpath + "/../"
sys.path.append(parent_path)

from itolapi import Itol, ItolExport

print('Running example itol and itolexport script')
print('')
print('Creating the upload params')

# Create the Itol class
test = Itol.Itol()

# Set the tree file
tree = fullpath + '/example_tree'
test.add_variable('treeFile', tree)
# Add parameters
test.add_variable('treeName', 'adsf')
test.add_variable('treeFormat', 'newick')
test.add_variable('dataset1File', fullpath + '/example_data')
test.add_variable('dataset1Label', 'colors')
test.add_variable('dataset1Separator', 'comma')
test.add_variable('dataset1Type', 'multibar')
# Check parameters
test.print_variables()
# Submit the tree
print('')
Beispiel #27
0
import os
import sys

current_dir = os.path.dirname(os.path.realpath(__file__))
root_path = os.path.join(current_dir, '..')
sys.path.append(root_path)

from itolapi import Itol, ItolExport  # NOQA

print('Running example itol and itolexport script')
print('')
print('Creating the upload params')

# Create the Itol class
test = Itol()

# Set the tree file
tree = os.path.join(current_dir, 'tree_of_life.tree.txt')
test.add_file(tree)
test.add_file(os.path.join(current_dir, 'colors_tol.txt'))
test.add_file(os.path.join(current_dir, 'labels.txt'))
test.add_file(os.path.join(current_dir, 'ranges.txt'))
# Add parameters
test.params['treeName'] = 'Tree of Life Example'
# Check parameters
test.print_variables()
# Submit the tree
print('')
print(
    ('Uploading the tree.  This may take some time depending on how large the '
import os
import sys
from itolapi import Itol, ItolExport

current_dir = os.path.dirname(os.path.realpath(__file__))
root_path = os.path.join(current_dir, '..')
sys.path.append(root_path)
boom = Itol()

# Set the tree file
tree = os.path.join(current_dir, 'MERS_CoV.tree.txt')
boom.add_file(tree)

# Add parameters
boom.params['treeName'] = 'MERS_CoV'
boom.params['display_mode'] = '2'
boom.params['ignore_branch_length'] = '1'
boom.params['current_font_size'] = '28'

# Check parameters
boom.print_variables()

# Submit the tree
print('')
print(
    ('Uploading the tree.  This may take some time depending on how large the '
     'tree is and how much load there is on the itol server'))
good_upload = boom.upload()
if not good_upload:
    print('There was an error:' + boom.comm.upload_output)
    sys.exit(1)