Trouble constructing function to visualize Alphafold2 predictions

I’m trying to use the following script (which is largely based off of this wonderful Colab here: https://colab.research.google.com/github/sokrypton/ColabFold/blob/main/AlphaFold2.ipynb#scrollTo=UGUBLzB3C6WN) to visualize the predicted folding of an amino acid sequence. I’m having trouble configuring the script in a way that will run, and for largely-institutional reasons, I’m not able to freely run-and-debug the script on my data, and can really only use it for the final runs. The error message I’m receiving right now (in reference to the show_pdb() third-to-last line) is:

AttributeError: 'NoneType' object has no attribute 'show'

Can someone help me resolve this issue, and possibly others that aren’t yet apparent to me? I’ve included the hashed-out parts for context, if it’s necessary or desired, but the main part I’d like help with is the construction of the show_pdb() function.

import os
import re
import glob

#@title Display 3D structure {run: "auto"}
model_num = 1 #@param ["1", "2", "3", "4", "5"] {type:"raw"}
color = "lDDT" #@param ["chain", "lDDT", "rainbow"]
show_sidechains = False #@param {type:"boolean"}
show_mainchains = False #@param {type:"boolean"}

def get_filepaths(root_path: str, file_regex: str):
    return glob.glob(os.path.join(root_path, file_regex))

rootdir = '/projects/p31492/long_alphafold/alphafold__long_sequence_file'
regex = '.pdb'
pdb_files = get_filepaths(rootdir, regex)

def show_pdb(model_num=1, show_sidechains=False, show_mainchains=False, color="lDDT"):
    for file in pdb_files:      
        view = py3Dmol.view(js='https://3dmol.org/build/3Dmol.js',)
        view.addModel(open(file,'r').read(),'pdb')

        if color == "lDDT":
            view.setStyle({'cartoon': {'colorscheme': {'prop':'b','gradient': 'roygb','min':50,'max':90}}})
        elif color == "rainbow":
            view.setStyle({'cartoon': {'color':'spectrum'}})
        elif color == "chain":
            for n,chain,color in zip(range(homooligomer),list("ABCDEFGH"),
                             ["lime","cyan","magenta","yellow","salmon","white","blue","orange"]):
                view.setStyle({'chain':chain},{'cartoon': {'color':color}})
        if show_sidechains:
            BB = ['C','O','N']
            view.addStyle({'and':[{'resn':["GLY","PRO"],'invert':True},{'atom':BB,'invert':True}]},
                                {'stick':{'colorscheme':f"WhiteCarbon",'radius':0.3}})
            view.addStyle({'and':[{'resn':"GLY"},{'atom':'CA'}]},
                                {'sphere':{'colorscheme':f"WhiteCarbon",'radius':0.3}})
            view.addStyle({'and':[{'resn':"PRO"},{'atom':['C','O'],'invert':True}]},
                                {'stick':{'colorscheme':f"WhiteCarbon",'radius':0.3}})  
        if show_mainchains:
            BB = ['C','O','N','CA']
            view.addStyle({'atom':BB},{'stick':{'colorscheme':f"WhiteCarbon",'radius':0.3}})

        view.zoomTo()
    return view

show_pdb(model_num,show_sidechains, show_mainchains, color="1DDT").show()
if color == "lDDT": plot_plddt_legend().show()  
plot_confidence(model_num).show()

Answer

The view object needs to be created before the for loop so that it gets updated every cycle instead of overwriting the previous version. So the following code

 for file in pdb_files:      
        view = py3Dmol.view(js='https://3dmol.org/build/3Dmol.js',)
        view.addModel(open(file,'r').read(),'pdb')
        ...

will look something like below.

 
 view = py3Dmol.view(js='https://3dmol.org/build/3Dmol.js')
 for file in pdb_files:
        view.addModel(open(file,'r').read(),'pdb')
        ...

And you need to assign a value to the variable homooligomer prior to its use later in the code.

 elif color == "chain":
            for n,chain,color in zip(range(homooligomer),list("ABCDEFGH"),
                             ["lime","cyan","magenta","yellow","salmon","white","blue","orange"]):
                view.setStyle({'chain':chain},{'cartoon': {'color':color}})