+aa_3letter = [
+ 'DPR','DLY','DAR','DHI','DAS','DGL','DSG','DGN','DSN','DTH',
+ 'DAL','DTY','DTR','DVA','DLE','DIL','DPN','MED','DCY',
+ 'CYS','MET','PHE','ILE','LEU','VAL','TRP','TYR','ALA','GLY','THR',
+ 'SER','GLN','ASN','GLU','ASP','HIS','ARG','LYS','PRO',
+ 'SME','AIB','ABU','DBZ']
+
+
+def pdb_code_chain(pdbid):
+
+ msg=''
+ chain=''
+ if len(pdbid)>4:
+ if pdbid[4]!=':':
+ return('use : between pdb code and chain id, like 5G3Q:B to select single chain')
+ chain=pdbid[5]
+ pdbid=pdbid[:4]
+
+ test=urllib.urlopen('http://files.rcsb.org/download/'+pdbid+'.pdb')
+ if test.code != 200:
+ msg = 'wrong pdb code'
+ else:
+ msg=pdb_missing_res_chain(test,chain)
+ test.close()
+ return(msg)
+
+def pdb_missing_res_chain(file,chain):
+ msg=''
+ newchain = True
+ ires=[]
+ for line in file:
+ if line[0:6] == 'ATOM ' and line[13:15] == 'CA' and (line[21] == chain or chain==''):
+ i = int(line[22:26])
+ ch = line[21]
+ if line[17:20] not in aa_3letter:
+ msg='residue '+line[17:20]+' '+str(i)+' not recognized'
+ return(msg)
+ if ires and i==ires[-1]:
+ continue
+ if newchain or i==ires[-1]+1:
+ ires.append(i)
+ newchain = False
+ else:
+ msg = 'chain breaks between residues '+\
+ str(ires[-1])+' and '+str(i)+' of chain '+ch+\
+ ', server cannot add missing residues to PDB file - please repair the structure using e.g. Modeller'
+ break
+ if line[0:3] == 'TER':
+ newchain = True
+ if line[0:3] == 'END':
+ break
+
+ if len(ires) == 0:
+ if chain == '':
+ msg='no CA atoms in this pdb'
+ else:
+ msg='wrong chain id'
+ return(msg)
+
+
+def code_2d(line):
+ msg=''
+ set ='HEC-'
+ line2 = ''.join([c for c in line if c in set])
+ if line2 != line:
+ msg='use only H,E,C or - letters'
+ return(msg)
+