Seite 1 von 1

Re: Von HTML Knopf in Python rein

Verfasst: Mittwoch 27. April 2011, 23:15
von BlackJack
@mzh: Falls mit "das" `os.dirname()` gemeint sein sollte: Das heisst eigentlich `os.path.dirname()`.

Re: Von HTML Knopf in Python rein

Verfasst: Donnerstag 28. April 2011, 09:01
von mzh
@BlackJack, cool.
@deets: ach so, in der interaktiven Session ist das __file__ Attribute nicht definiert. Klar, dass meine Beispiele nicht laufen.
Ich habs jetzt so hingekriegt:

Code: Alles auswählen

import os.path
print(os.path.dirname(os.path.abspath(__file__)))
Ich möchte meine Ursprüngliche Frage nochmals anders stellen:
Ganz konkret geht es darum, dass folgendes Skript über einen Knopf auf einer Webseite gestartet werden kann. Das Skript ruft ein paar Programme auf, die auf dem Server installiert sind und schreibt ein paar Files, mehr nicht.
Alles was ich möchte ist jetzt, dass der Besucher der Website dieses Skript starten kann.

Code: Alles auswählen

#!/usr/local/bin/python3

"""Description of the 'connect' module.

The 'connect' module prepares the BioFET-SIM calculation.
It carries out the following steps:
    - Download of the PDB file
    - Fixing the PDB file with PDB2PQR
    - Realigning the protein structure with VMD
    - From the realignment, the dimensions are obtained
    - Calculation of overall charge with PROPKA 3.
""" 

# NOTE:
# - Calls all functions in order to obtain the
#   charge, charge-carrier number and dimensions
#   of the protein.

# Default modules
import sys
import os
import time
from os.path    import splitext
from subprocess import call 

# Custom modules
import propka 
import getCharge 
import getDimension

# Builtin modules 
#import realign 
#import getPointCharges
#import getCharge 
from signature import signature
from tests     import hasExt
from parse     import parse

# ************************************************************
# ************************************************************
# SETUP
def setup(pdbfile): 
    """Setting up the run.

    The function handles the submitted argument, ie. it checks
    if there is an extension or not. Incase it is missing, it is
    appended, incase it is present, the extension remains.

    setup(<PDB_ID[.pdb]>) --> target

    The return value is an uppercase string including the '.pdb'
    extension.
    """
    signature('SETUP')

    # Handle the case with/without extension in the argument
    if hasExt(pdbfile):
        target = pdbfile.upper() 
    else:
        target = pdbfile.upper() + '.pdb' 
    return target 

def getPDB(target):
    """Obtain the PDB file.
    The 'target' argument is the correct file name, including
    the '.pdb' extension, eg. '1AVD.pdb'.

    The file is written to disc after download."""

    print("Provided PDB file: " + target) 
    print("Requesting PDB file: " + target)
    #call(['wget', 'http://www.pdb.org/pdb/files/'\
    #        + target])
    os.system("wget http://www.pdb.org/pdb/files/"\
            + target)
# ------------------------------------------------------------



# ************************************************************
# ************************************************************
# PDB2PQR Part 
def callPDB2PQR(target): 
    signature('PDB2PQR')
    call(['pdb2pqr.py', '-v', '--ff=CHARMM', target,\
            splitext(target)[0] + '-out.pqr'])
    #os.system('pdb2pqr.py -v --ff=CHARMM ' + target\
    #        + ' ' + splitext(target)[0] + '-out.pqr')
    print('PDB2PQR done.')
    print() 
# ------------------------------------------------------------



# ************************************************************
# ************************************************************
# VMD Part
def callVMD(target):
    signature('VMD') 
    #call(['align.py', target])
    os.system('realign.py ' + target[0:4] + '-out.pqr')
    print('VMD done.')
    print() 
# ------------------------------------------------------------ 



# ************************************************************
# ************************************************************
# PROPKA Part
def callPropka3(target):
    signature('PROPKA') 
    #call(['cp', splitext(target)[0] + '-out.pqr',\
    #            splitext(target)[0] + '-out.pdb'])
    #call(['propka.py', splitext(target)[0] + '-out.pdb'])
    #os.system('cp ' + splitext(target)[0] + '-out.pqr'\
    #        ' ' + splitext(target)[0] + '-out.pdb')
    if os.system('propka.py ' + splitext(target)[0] +\
            '-out-new.pdb') == 0:
        print('Propka 3 done.') 
    print() 
# ------------------------------------------------------------



# ************************************************************
# ************************************************************
# BOX Part
def callBox(target):
    signature('BOX')
    dim = getDimension.BoxPDB(target)
    print(dim.boxIt())
# ------------------------------------------------------------



# ************************************************************
# ************************************************************
# COLLECT Part 
def collectData(pH, target):
    signature("COLLECTING DATA") 
    charge = getCharge.getCharge(pH, target)
    print('Charge at pH {0}: {1}'.format(pH, charge[1])) 
    print()
# ------------------------------------------------------------ 



# ************************************************************
# ************************************************************
# Entry point of Python.  
try:
    # Get the argument
    pdbfile = sys.argv[1] 
    target = setup(pdbfile)

    # Download
    getPDB(target) 
    # OUT: 'PDB.pdb'

    # Correction
    # IN:  'PDB.pdb'
    callPDB2PQR(target) 
    # OUT: 'PDB-out.pqr'

    # Realignment
    # IN:  'PDB-out.pqr'
    callVMD(target)
    # OUT: 'PDB-out-new.pdb'

    # Charging
    # IN:  'PDB-out-new.pdb'
    callPropka3(target) 
    # OUT: 'PDB-out-new.pka'


    if 1:
        # pH=7
        parse(target)
        collectData(7, target)
    else:
        for i in range(1,15):
            collectData(i, target)
    print(callBox(target))



except IndexError:
    if len(sys.argv) == 1:
        print("No argument provided.")
        print("$ connect.py <PDB>") 

# End of the program.
# ************************************************************
# ************************************************************ 

Aus irgendeinem Grund wird der wget Befehl ausgeschrieben.. das sieht im Code normal aus.
Wie kann ich das machen? Wenn ein Framework (Flask) die beste Lösung ist, dann probiere ich es aus. Ich habe einfach etwas die Befürchtung, dass es dann nur noch etwas komplizierter wird.. deshalb habe ich bis anhin versucht es zu vermeiden. Ich bin mir auch bewusst, dass das Skript so wahrscheinlich alle Programmierpraktiken ausdolcht, Hinweise in dieser Richtung nehme ich gerne entgegen, allerdings haben diese für mich im Moment weniger Priorität.
Danke für weiteren Input.