Trouble constructing function to visualize Alphafold2 predictions - python

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()

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}})

Related

Formatting a card expiry date validator in Python

Hello community!
I have quite a problem with my python code, I have been trying to resolve it for days, but I can't make it work. I am working on an online shopping service for my school's assignment. I am trying to create a credit card validator. What my problem concerns is that when I have an entry for user to put his expiry date, I am trying to format it in this way MM/YY so for instance 01/20 or 11/23 . I am trying to make it automatically add the / slash. So that when user inputs 12 it will automatically add the /
So far I am tracking when there is a change in the entry box, so it knows when to add the slash. I have unfortunately problem with formatting. So when the input is 12/21 and the users deletes the first "2" char, the input becomes this:
While debbuing I sometimes can see these results:
Or such:
Here is the code I use. Do you see any mistake or is there an efficient way to do so?
def validateDate():
global entryDateCard,dateCard ##entryDateCard is just Entry ID, dateCard is the expiry date
print("bbb")
if (len(entryDateCard.get())==5):
print("ccc")
entryDateCard.insert(0,dateCard)
dateCard = entryDateCard.get()
if (2<=len(entryDateCard.get())<=4 and (entryDateCard.get().find("/")!=2)): ## does not work even when true
print("aaa")
dateCard.replace("/","")
entryDateCard.delete(0,"end")
entryDateCard.insert(0,dateCard)
entryDateCard.insert(2,"/")
if(len(entryDateCard.get())>5):
entryDateCard.delete(0,"end")
entryDateCard.insert(0,dateCard[:5])
dateCard = dateCard[:5]
EDIT1 : Tried this but still does not work (Posting an entire snippet of code)
import tkinter
w = 1280
h = 720
canvas = tkinter.Canvas(width=w,height=h,bg="#71CAE7")
canvas.pack()
entryDateCard=0
dateCard=""
cardDateSV=tkinter.StringVar()
cardDateSV.trace("w", lambda name, index, mode, sv=cardDateSV: validateDate())
entryDateCard = tkinter.Entry(width=15,font = "Helvetica 15 bold",textvariable=cardDateSV)
entryDateCard.pack()
entryDateCard.place(x=(w//2)-275,y=h-(0.61*h),height=30)
def validateDate():
global entryDateCard,dateCard
dateCard = entryDateCard.get()
if (len(dateCard.replace("/",""))==2):
entryDateCard.insert(2,"/")
print("two")
elif (len(dateCard.replace("/",""))==3):
entryDateCard.delete(0,"end")
entryDateCard.insert(0,dateCard[:2])
entryDateCard.insert(2,"/")
entryDateCard.insert(3,dateCard[3:4])
print("three")
elif (len(dateCard.replace("/",""))==4):
entryDateCard.delete(0,"end")
entryDateCard.insert(0,dateCard[:2])
entryDateCard.insert(2,"/")
entryDateCard.insert(3,dateCard[3:5])
print("four")
elif (len(dateCard.replace("/",""))>=5):
print("max")
entryDateCard.delete(0,"end")
entryDateCard.insert(0,dateCard[:5])
dateCard = dateCard[:5]

why am i getting an object invalid error?

I think there is something wrong with my naming convention but I'm not sure how to fix it. it keeps giving me an invalid object even when tried to name it based on the vertex please help.
for i in range(1,100):
print i
def cactus():
#creating base of cactus
cmds.polyCylinder(sc=1,sa=10,sh=10, n= "cactus1_Base["+str(i)+"]")
The error I'm getting is:
# Error: TypeError: file <maya console> line 17: Object cactus1_Base[99].e[140:169] is invalid this is the error im getting and the code is
I dont have maya this week so I cant really check the result of this code
The first piece of codes woulld be for me the best solution but you have also the second one.
Note that in your code, character '[' and ']' are reserved in maya for components : vtx[], e[], f[]...etc
so you cant use them for naming
Secondly when you create your iteration 'i', it is outside your function so there is no real connection between i and your function cactus()
So you have to think on how you want to create cactus. That why I have written those two examples :
the first consider that you are creating cactus elements
the second one is just for creating a bunch of cactus
You could go beyond with kwargs and try to mimic cmds.polyCylinder
Just in case a bit python lessons for maya : https://www.youtube.com/watch?v=PDKxDbt6EGQ&t=4s
def nameChecker(name, start=0, max=100000, stringFormat=True):
if not '{' in name:
stringFormat = False
a = start
while a < max:
if stringFormat:
confName = name.format(a)
else:
confName = name + str(a)
if not cmds.objExists(confName):
return confName
a+=1
def create_cactus(baseName='cactus1_Base_{:03d}'):
name_conform = nameChecker(baseName)
cactus = cmds.polyCylinder(sc=1,sa=10,sh=10, n=name_conform)[0]
return cactus
cactus_output = []
for i in range(1,100):
cactus = create_cactus()
cactus_output.append(cactus)
print(cactus_output )
OR more simple :
def create_cactus(nb_of_cactus=100):
cactus_output = []
for nb in range(nb_of_cactus):
name = "cactus1_Base_{}".format(nb)
cactus = cmds.polyCylinder(sc=1,sa=10,sh=10, n=name)[0]
cactus_output.append(cactus)
return cactus
myCactus= create_cactus(100)
print(myCactus)
or based on selection :
def create_cactusVtx():
mysel = cmds.ls(sl=True, fl=True)
for i in range(len(mysel)):
id = mysel.split('[')[-1][:-1]
name = "cactus1_Base_{}".format(i)
cactus = cmds.polyCylinder(sc=1,sa=10,sh=10, n=name)[0]

Creating loop for __main__

I am new to Python, and I want your advice on something.
I have a script that runs one input value at a time, and I want it to be able to run a whole list of such values without me typing the values one at a time. I have a hunch that a "for loop" is needed for the main method listed below. The value is "gene_name", so effectively, i want to feed in a list of "gene_names" that the script can run through nicely.
Hope I phrased the question correctly, thanks! The chunk in question seems to be
def get_probes_from_genes(gene_names)
import json
import urllib2
import os
import pandas as pd
api_url = "http://api.brain-map.org/api/v2/data/query.json"
def get_probes_from_genes(gene_names):
if not isinstance(gene_names,list):
gene_names = [gene_names]
#in case there are white spaces in gene names
gene_names = ["'%s'"%gene_name for gene_name in gene_names]**
api_query = "?criteria=model::Probe"
api_query= ",rma::criteria,[probe_type$eq'DNA']"
api_query= ",products[abbreviation$eq'HumanMA']"
api_query= ",gene[acronym$eq%s]"%(','.join(gene_names))
api_query= ",rma::options[only$eq'probes.id','name']"
data = json.load(urllib2.urlopen(api_url api_query))
d = {probe['id']: probe['name'] for probe in data['msg']}
if not d:
raise Exception("Could not find any probes for %s gene. Check " \
"http://help.brain- map.org/download/attachments/2818165/HBA_ISH_GeneList.pdf? version=1&modificationDate=1348783035873 " \
"for list of available genes."%gene_name)
return d
def get_expression_values_from_probe_ids(probe_ids):
if not isinstance(probe_ids,list):
probe_ids = [probe_ids]
#in case there are white spaces in gene names
probe_ids = ["'%s'"%probe_id for probe_id in probe_ids]
api_query = "? criteria=service::human_microarray_expression[probes$in%s]"% (','.join(probe_ids))
data = json.load(urllib2.urlopen(api_url api_query))
expression_values = [[float(expression_value) for expression_value in data["msg"]["probes"][i]["expression_level"]] for i in range(len(probe_ids))]
well_ids = [sample["sample"]["well"] for sample in data["msg"] ["samples"]]
donor_names = [sample["donor"]["name"] for sample in data["msg"] ["samples"]]
well_coordinates = [sample["sample"]["mri"] for sample in data["msg"] ["samples"]]
return expression_values, well_ids, well_coordinates, donor_names
def get_mni_coordinates_from_wells(well_ids):
package_directory = os.path.dirname(os.path.abspath(__file__))
frame = pd.read_csv(os.path.join(package_directory, "data", "corrected_mni_coordinates.csv"), header=0, index_col=0)
return list(frame.ix[well_ids].itertuples(index=False))
if __name__ == '__main__':
probes_dict = get_probes_from_genes("SLC6A2")
expression_values, well_ids, well_coordinates, donor_names = get_expression_values_from_probe_ids(probes_dict.keys())
print get_mni_coordinates_from_wells(well_ids)
whoa, first things first. Python ain't Java, so do yourself a favor and use a nice """xxx\nyyy""" string, with triple quotes to multiline.
api_query = """?criteria=model::Probe"
,rma::criteria,[probe_type$eq'DNA']
...
"""
or something like that. you will get white spaces as typed, so you may need to adjust.
If, like suggested, you opt to loop on the call to your function through a file, you will need to either try/except your data-not-found exception or you will need to handle missing data without throwing an exception. I would opt for returning an empty result myself and letting the caller worry about what to do with it.
If you do opt for raise-ing an Exception, create your own, rather than using a generic exception. That way your code can catch your expected Exception first.
class MyNoDataFoundException(Exception):
pass
#replace your current raise code with...
if not d:
raise MyNoDataFoundException(your message here)
clarification about catching exceptions, using the accepted answer as a starting point:
if __name__ == '__main__':
with open(r"/tmp/genes.txt","r") as f:
for line in f.readlines():
#keep track of your input data
search_data = line.strip()
try:
probes_dict = get_probes_from_genes(search_data)
except MyNoDataFoundException, e:
#and do whatever you feel you need to do here...
print "bummer about search_data:%s:\nexception:%s" % (search_data, e)
expression_values, well_ids, well_coordinates, donor_names = get_expression_values_from_probe_ids(probes_dict.keys())
print get_mni_coordinates_from_wells(well_ids)
You may want to create a file with Gene names, then read content of the file and call your function in the loop. Here is an example below
if __name__ == '__main__':
with open(r"/tmp/genes.txt","r") as f:
for line in f.readlines():
probes_dict = get_probes_from_genes(line.strip())
expression_values, well_ids, well_coordinates, donor_names = get_expression_values_from_probe_ids(probes_dict.keys())
print get_mni_coordinates_from_wells(well_ids)

Arcpy SelectLayerByLocation giving an error

this script is supposed to select features within distance in two layers based on some their characteristics one feature will get a score (example: water pipes crossing naturally sensitive areas like rivers, the type of that river and its permanency matter in the scoring, so each type will be selected then used in select by lactation function to give water pipes that are within a distance its score
this is the error I get when i run these codes:
Executing: SelectLayerByLocation water_mains WITHIN_A_DISTANCE Just_selected "2.5 Meters" NEW_SELECTION
Start Time: Thu Sep 25 15:21:09 2014
ERROR 999999: Error executing function.
A column was specified that does not exist.
A column was specified that does not exist.
Failed to execute (SelectLayerByLocation).
the select layer by location is in a script that is called by another script (main script)
the main script :
def main():
try:
import arcpy
from arcpy import env
# pathing to avoid retyping
env.workspace = "C:/Users/abusheikan/Desktop/prev_files/RiskAnalysisModel"
dataPath = 'C:\\Users\\abusheikan\\Desktop\\prev_files\\RiskAnalysisModel\\ToolData2'
arcpy.env.overwriteOutput = True
import imp
##Defines INPUT variables
#some variable wont be used but are there for future use, I'm starting off as simple as possible
creekLayer = dataPath + "\\ENVIRONMENTAL\\OHN_WaterCourse.shp"
PipeLayer=dataPath + "\\SERVICES\\water_mains.shp"
nameField = 'ROW_1'
scoreField = 'ROW_SCORE1'
crossingField = 'CROSS_ROW1'
ROWfield = 'ROW_TRUE1'
diaField='INTERNAL_D'
rangeVal= 416
Field = 'WARTERCOURS'
Field2='PERMANENCY'
arcpy.MakeFeatureLayer_management(PipeLayer,"water_mains")
inFeatures = "water_mains"
#The following lists contain road classes. Format is (a, b, c,d) such that a is the creek class name,
#b is an average permencnacy of flow, c is the width, nd d is the xscore to be given .
#Lower value of c means lower criticality.
creeks = [('Vertual Flow','intermittent',10,1),
('Vertual Connector','intermittent', 10,2),
('Vertual Flow','Permanent', 10,1),
('Vertual Connector', 'Permanent', 10,2),
('Ditch','Intermittent',5,3),
('Ditch','Permanent',5,4),
('Stream','Intermittent',5,3),
('Stream','Intermittent',5,4)]
## the following isnt used yet
creeks2 = [('Vertual Flow','intermittent',10,1),
('Vertual Connector','intermittent', 10,2),
('Vertual Flow','Permanent', 10,1),
('Vertual Connector', 'Permanent', 10,2),
('Ditch','Intermittent',5,3),
('Ditch','Permanent',5,4),
('Stream','Intermittent',5,3),
('Stream','Intermittent',5,4)]
## This codeblock isnt utilized yet and will always return row_score, it is supposed to adjusts the value of ROW_SCORE
##based on whether the water main crosses a creek, by looking up the value in CROSS_ROW1 feild that is obtained later on
expression = "crossing(!CROSS_ROW1!,!ROW_SCORE1!)"
codeblock = """def crossing(crosses, row_score):
if crosses != 0:
return 5
else:
return row_score"""
except:
arcpy.AddError("Definitions failed")
arcpy.AddMessage(arcpy.GetMessages())
try:
## pathing to a funtion to be called
fpath = 'C:\\Users\\abusheikan\\Desktop\\prev_files\\RiskAnalysisModel\\Scripts\\'
## defining the function pathing we retyped anyway for debugging purpuses.
functions = imp.load_source('functions', 'C:\\Users\\abusheikan\\Desktop\\prev_files\\RiskAnalysisModel\\Scripts\\functions_creeks.py')
## check check :-p
arcpy.AddMessage("Funtions Loaded")
except:
arcpy.AddMessage("Functions not loaded")
try:
##Clear all selections, because otherwise commands will be applied only to selected features, why? I ont know pls explain where this is
## supposed to be used and where not to. THANKs!
arcpy.SelectLayerByAttribute_management(inFeatures, "CLEAR_SELECTION")
arcpy.AddMessage("Selected")
##This new field will show the road overlying the pipe. Default value of "no Creek" will be assigned.
arcpy.AddField_management(inFeatures, nameField, "TEXT")
arcpy.CalculateField_management(inFeatures, nameField, '"No Creek"')
##This field will contain a score for the highest creek class over the pipe.
## Default of 0 means no creeks
arcpy.AddField_management(inFeatures, scoreField, "SHORT")
arcpy.CalculateField_management(inFeatures, scoreField, 1)
arcpy.AddField_management(inFeatures, crossingField, "SHORT")
## arcpy.AddField_management(mainRoadLayer, ROWfield, "FLOAT",3,1)
## arcpy.CalculateField_management("t_Pavement_Line", ROWfield, expressionROW, "PYTHON_9.3", codeblockROW)
except:
#Report error
arcpy.AddError("Could not create new fields")
#Report error messages
arcpy.AddMessage(arcpy.GetMessages())
try:
## functions.roadclass_loop is a function that loops through all creek classes in
## a list, selects the water mains within a distance of each one, and assigns the
## appropriate score. Full script is in the called function.
## the following s a failed test so never mind that commented out line, it may ciome in handy so left it in there
## arcpy.MakeFeatureLayer_management(PipeLayer,
## "JUST_MADE",str(dialField) + " <= "+ str(rangeVal))
## calls creek_loop funtion() i think here is where the error is created pls check the inputs they may be where problem is! but i cant see anything wrong with them.
functions.roadclass_loop(creeks, creekLayer, Field, inFeatures, "WITHIN_A_DISTANCE",
nameField, scoreField)
arcpy.AddMessage("small pipes")
## same as b4 but with the second tuple list.
functions.roadclass_loop(creeks2, creekLayer, Field, inFeatures, "WITHIN_A_DISTANCE",
nameField, scoreField)
arcpy.AddMessage("BIG PIPES")
## functions.roadclass_loop(provincial, provincialLayer, Field3, inFeatures, "INTERSECT",
## "", crossingField)
## If the CROSS_ROW field has a nonzero value (i.e. if the water main crosses a large road)
## the road class score will be increased to 5(the maximum).
## inserts the scores into the
arcpy.CalculateField_management(inFeatures, scoreField, expression, "PYTHON_9.3", codeblock)
except:
arcpy.AddMessage("Could not run")
arcpy.AddMessage(arcpy.GetMessages())
if __name__== "__main__":
main()
the called function is:
def test():
## import arcpy
arcpy.AddMessage("This function works")
##def roadclass_loop(listOfClassTuples, sourceLayer, fieldName, targetLayer,
## outputField1, outputField2):
def roadclass_loop(listOfClassTuples, sourceLayer, fieldName, targetLayer, crossingType,
outputField1, outputField2):
import arcpy
from arcpy import env
env.workspace = "C:/data/"
##try:
for creekclass in listOfClassTuples:
(classname, Permanency, creekWidth, score) = creekclass
bufferDistance = creekWidth*0.5
try:
if crossingType == "INTERSECT":
stringBuffer = ""
else:
stringBuffer = "%s Meters" % str(bufferDistance)
except:
arcpy.AddMessage("its here")
arcpy.MakeFeatureLayer_management(sourceLayer, "Just_selected",
fieldName + "= '"+ classname + "'")
#arcpy.MakeFeatureLayer_management("Just_Selected", "JUST_SELECTED", FieldName2+" = '"+ Permanency + "'")
arcpy.SelectLayerByLocation_management(targetLayer, crossingType,
"Just_selected", stringBuffer, "NEW_SELECTION")
classname = classname.lower()
if outputField1!= "":
arcpy.CalculateField_management(targetLayer, outputField1, classname )
arcpy.CalculateField_management(targetLayer, outputField2, score )
arcpy.Delete_management("Just_selected")
arcpy.SelectLayerByAttribute_management(targetLayer, "CLEAR_SELECTION")
##except:
# arcpy.AddMessage("Function failed")
#arcpy.AddMessage(arcpy.GetMessages())
See this question on the GIS StackExchange: Points in Polygon Count: Error with arcpy.selectLayerByLocation_management . They made a mistake when calling MakeFeatureLayer_management, but the error was thrown by SelectLayerByLocation_management. You may have a similar situation.
In your case, are you confident that the feature class stored in dataPath + "\\ENVIRONMENTAL\\OHN_WaterCourse.shp" has a field called WARTERCOURS? Is there maybe a typo there? (The word WARTERCOURS caught my attention; Google says you're the first person on the Internet to use it.)
Is your listOfClassTuples, which is being fed by your creeks variable, supposed to be set of fields within your creekLayer (dataPath + "\ENVIRONMENTAL\OHN_WaterCourse.shp)?

Python Maya - If objectType returns "No object name specified"

I am trying to get maya to check if the listed object is a blendshape node or not.
This is my code:
def bake(self, *args):
self.items["selection"] = cmds.ls(sl = True)
self.items["shapes"] = cmds.listRelatives(self.items["selection"], ad = True)
shapes = ()
for i in self.items["shapes"]:
bs = cmds.listConnections(i, type = "blendShape", exactType = True)
if cmds.objectType(bs, isType = "blendShape"):
print bs
It returns # Error: RuntimeError: file X:/Documents/maya/scripts\jtBakeCharacter.py line 16: No object name specified
Line 16 is: if cmds.objectType(bs, isType = "blendShape"):
Except that I AM specifying an object name, that object name is bs .. I have printed the result of bs and it has many objects listed. Many.
The code is redundant. You don't need most of the lines. The listConnections already ensures that you have only blendshapes. The exact problem is that you are calling something like:
cmds.objectType([])
for some of those extra shapes. And this is illegal. But mostly you code can be encapsulated as follows:
selected = cmds.ls(sl = True, dag=True ,shapes = True)
blends = cmds.listConnections(selected , type = "blendShape", exactType = True)
for item in blends:
print item
But this may not catch your intent perfectly, but shows how may extra steps you take. In reality you don't need the line if cmds.objectType(bs, isType = "blendShape"): for anything
Joojaa's answer is elegant, but you can get it down even shorter by using the default selection behavior:
blendshapes = cmds.ls(cmds.listHistory(pdo=True), type='blendShape') or []
for item in blendshapes:
print item
(In the quest to make it even shorter I'm not checking for the selection, so this one fails if nothing is selected).
PS: if you need to get to the blendshape from one of the upstream shapes, instead of the deformed shape, you can use listHistory (f=True)
You could try this:
from pymel.core import *
for obj in selected():
shapeNode = obj.getChildren()[0]
for output in shapeNode.outputs():
if nodeType(output) == "blendShape":
print obj, "is a blendshape"

Categories

Resources