Changeset 286

Show
Ignore:
Timestamp:
01/30/08 02:09:41 (10 months ago)
Author:
t
Message:

miscellaneous cleanup; added "Raw PWM Motif" for imported motif types; roughed
out forms.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cartwheel-server/data-model/website/motifs.sql

    r283 r286  
    1313        visible BOOLEAN DEFAULT 't', 
    1414        motif_type TEXT CONSTRAINT valid_type 
    15                                CHECK (motif_type IN ('IUPAC', 'PWM')), 
     15                CHECK (motif_type IN ('IUPAC', 'PWM', 'RawPWM')), 
    1616        folder_id INTEGER 
    1717) INHERITS (cartwheel_objects); 
     
    3838        class_id INTEGER REFERENCES classes DEFAULT :class_id, 
    3939        source_motifs TEXT DEFAULT '', 
    40         default_threshold FLOAT 
     40        threshold FLOAT 
    4141) INHERITS (named_motifs); 
     42 
     43\set class_id 2043 
     44 
     45INSERT INTO classes (id, name, description) VALUES 
     46        (:class_id, 'cartwheel.website.RawPWMMotif', 
     47        'A raw PWM motif.'); 
     48 
     49CREATE TABLE raw_pwm_motifs ( 
     50        class_id INTEGER REFERENCES classes DEFAULT :class_id, 
     51        matrix TEXT NOT NULL, 
     52        threshold FLOAT, 
     53        format TEXT NOT NULL 
     54) INHERITS (named_motifs); 
  • trunk/cartwheel-server/lib/cartwheel/website/PWMMotif.py

    r277 r286  
    66class PWMMotif(NamedMotif): 
    77    table = 'pwm_motifs' 
    8     mymembers = ('source_motifs', 'default_threshold') 
     8    mymembers = ('source_motifs', 'threshold') 
    99 
    1010    def make_pwm(self): 
     
    2525 
    2626        return pwm.find(sequence.seq[start:stop], 
    27                         self.default_threshold, offset=start) 
     27                        self.threshold, offset=start) 
    2828 
    2929    def extra_information(self): 
     
    3434         
    3535        return "PWM consensus: %s; default threshold %g" % (consensus, 
    36                                                        self.default_threshold) 
     36                                                       self.threshold) 
    3737    def simple_key(self): 
    3838        import motility 
  • trunk/cartwheel-server/tests/functional-tests/test-with-user/test-web/test-folder-motifs.py

    r280 r286  
    253253 
    254254    follow('Return to folder menu') 
     255 
     256def test_add_jaspar_motif(): 
     257    show() 
     258    follow("manage motifs") 
     259    follow("import_single_matrix") 
     260 
     261    fv('1', 'name', 'test_jaspar') 
     262    fv('1', 'format', 'JASPAR') 
     263    fv('1', 'matrix', 'AGATAG\nTGATAG\nTGATAA') 
     264    fv('1', 'notes', 'figgledybum') 
     265 
     266    submit() 
     267    code('200') 
     268 
     269    show() 
     270 
     271    find("Matrix motif 'test_jaspar' created.") 
     272 
     273    follow("Return to motif list") 
     274 
     275    ## simple test of edit 
     276    follow('edit-TGATAG') 
     277    submit('return') 
     278 
     279    ## end 
     280    follow('Return to folder menu') 
  • trunk/cartwheel-server/website/canal/motif/__init__.py

    r283 r286  
    1313 
    1414from pages import add_iupac, _q_index, build_pwm_from_list, \ 
    15      display_pwm_information 
     15     display_pwm_information,  import_single_matrix 
    1616 
    1717 
     
    1919from cartwheel.website.IUPACMotif import IUPACMotif 
    2020from cartwheel.website.PWMMotif import PWMMotif 
     21from cartwheel.website.RawPWMMotif import RawPWMMotif 
    2122 
    2223_q_exports = ['add_iupac', 'delete', 'edit', 'go_analyze', 
    23               'build_pwm_from_list', 'batch_export', 'batch_import'] 
     24              'build_pwm_from_list', 'batch_export', 'batch_import', 
     25              'import_single_matrix'] 
    2426 
    2527def edit(request): 
     
    3335    if isinstance(motif, IUPACMotif): 
    3436        return edit_iupac_motif(request, motif) 
     37    elif isinstance(motif, PWMMotif): 
     38        return edit_pwm_motif(request, motif) 
     39    elif isinstance(motif, RawPWMMotif): 
     40        return edit_raw_pwm_motif(request, motif) 
    3541    else: 
    36         return edit_pwm_motif(request, motif
     42        raise Exception("unknown motif type"
    3743 
    3844def delete(request): 
     
    138144    name = motif.name 
    139145    sequences = motif.source_motifs.split("\n") 
    140     threshold = motif.default_threshold 
     146    threshold = motif.threshold 
    141147    notes = motif.notes 
    142148 
     
    149155            errors.append("you must enter a name!") 
    150156 
    151     if 'default_threshold' in request.form: 
    152         threshold = request.form['default_threshold'] 
     157    if 'threshold' in request.form: 
     158        threshold = request.form['threshold'] 
    153159        try: 
    154160            threshold = float(threshold) 
     
    175181    motif.name = name 
    176182    motif.source_motifs = "\n".join(sequences) 
    177     motif.default_threshold = threshold 
     183    motif.threshold = threshold 
    178184    motif.notes = notes 
    179185     
     
    187193                  name=motif.name, 
    188194                  sequences=motif.source_motifs, 
    189                   default_threshold=round(motif.default_threshold, 2), 
     195                  threshold=round(motif.threshold, 2), 
    190196                  notes=motif.notes) 
    191197 
     
    203209<textarea name='sequences' rows='5' cols='30'></textarea> 
    204210<p> 
    205 Threshold: <input type='text' name='default_threshold' size='7'> 
     211Threshold: <input type='text' name='threshold' size='7'> 
    206212<p> 
    207213Notes:<br> 
     
    221227    return wrap(htmlfill.render(formhtml, values), 
    222228                display_pwm_information(pwm, sequences, threshold), 
     229                title="Edit PWM Motif '%s'" % (name,), 
     230                show_anything = False) 
     231 
     232def edit_raw_pwm_motif(request, motif): 
     233    name = motif.name 
     234    matrix = motif.matrix 
     235    format = motif.format 
     236    threshold = motif.threshold 
     237    notes = motif.notes 
     238 
     239    errors = [] 
     240     
     241    if 'name' in request.form: 
     242        name = request.form['name'].strip() 
     243         
     244        if not name: 
     245            errors.append("you must enter a name!") 
     246 
     247    if 'threshold' in request.form: 
     248        threshold = request.form['threshold'] 
     249        try: 
     250            threshold = float(threshold) 
     251        except ValueError: 
     252            errors.append("threshold must be a number!") 
     253 
     254    if 'notes' in request.form: 
     255        notes = request.form['notes'] 
     256 
     257    if errors: 
     258        return "ERROR:<p><ul><li> %s</ul>" % ("<li>".join(errors,)) 
     259 
     260    motif.name = name 
     261    motif.matrix = matrix 
     262    motif.format = format 
     263    motif.threshold = threshold 
     264    motif.notes = notes 
     265     
     266    motif.manager.commit() 
     267 
     268    if 'return' in request.form: 
     269        return request.redirect(request.get_url(1)) 
     270     
     271    # fill 
     272    values = dict(motif_id=motif.id, 
     273                  name=motif.name, 
     274                  format=format, 
     275                  matrix=matrix, 
     276                  threshold=round(motif.threshold, 2), 
     277                  notes=motif.notes) 
     278 
     279    formhtml = """\ 
     280<h2>Edit imported matrix motif '%s'</h2> 
     281<form method='POST' action=''> 
     282 
     283<input type='hidden' name='motif_id'> 
     284 
     285Name: <input type='text'  name='name'> <p> 
     286 
     287Matrix data type: 
     288<select name='format'> 
     289 <option value='JASPAR'> JASPAR 
     290</select> 
     291<p> 
     292 
     293Matrix data:<br> 
     294<textarea name='matrix' rows='8' cols='40'></textarea> 
     295<p> 
     296Threshold: <input type='text' name='threshold' size='7'> 
     297<p> 
     298Notes:<br> 
     299 <blockquote> 
     300  <textarea name='notes' rows='5' cols='50'></textarea> 
     301 </blockquote> 
     302<p> 
     303<input type='submit' value='save & return to motif list' name='return'> 
     304<input type='submit' value='save & update information'> 
     305<input type='reset' value='reset form'> 
     306</form> 
     307<hr> 
     308""" % (motif.name,) 
     309 
     310    formhtml = htmlfill.render(formhtml, values) 
     311     
     312    return wrap(htmlfill.render(formhtml, values), 
     313#                display_pwm_information(pwm, sequences, threshold), 
    223314                title="Edit PWM Motif '%s'" % (name,), 
    224315                show_anything = False) 
     
    246337        elif motif.motif_type == 'PWM': 
    247338            output.write('%% PWM from sites\n') 
    248             output.write('%% threshold=%g\n' % (motif.default_threshold,)) 
     339            output.write('%% threshold=%g\n' % (motif.threshold,)) 
    249340            output.write('%s\n' % (motif.source_motifs,)) 
    250341        else: 
  • trunk/cartwheel-server/website/canal/motif/analyze.ptl

    r283 r286  
    11""" 
    22Web handler for dealing with sequence analysis by motifs. 
    3  
    4 CTB to test: 
    5   - empty annotations 
    63""" 
    74 
     
    388385        <hr> 
    389386        """ % (name, len(self.seq.seq), 
    390                motif.simple_key(), len(pwm), motif.default_threshold,) 
     387               motif.simple_key(), len(pwm), motif.threshold,) 
    391388 
    392389        if not matches: 
     
    480477 
    481478        s = """# PWM motif %s: consensus %s/threshold %f\n# sequence: %s\n"""%\ 
    482             (motif.name, consensus, motif.default_threshold, self.seq.name,) 
     479            (motif.name, consensus, motif.threshold, self.seq.name,) 
    483480 
    484481        # add in score. 
  • trunk/cartwheel-server/website/canal/motif/pages.ptl

    r283 r286  
    11import cartwheel.website 
    22from canal.templates import header, footer, wrap, error 
     3 
    34from cartwheel.website.IUPACMotif import IUPACMotif 
    45from cartwheel.website.PWMMotif import PWMMotif 
     6from cartwheel.website.RawPWMMotif import RawPWMMotif 
     7 
    58from canal.group.utils import make_sequence_list_widget 
    69from canal.motif import utils 
     
    1013 
    1114nice_motif_types = dict(IUPAC='simple motif (IUPAC notation)', 
    12                         PWM='matrix motif (PWM)') 
     15                        PWM='matrix motif (PWM)', 
     16                        RawPWM='imported matrix motif (PWM)') 
    1317 
    1418template _q_index(request): 
     
    6670                       <li> <a href="build_pwm_from_list">build a matrix (PWM) 
    6771                                                from a list of sites</a> 
    68                        <li> <a href="">import matrix from JASPAR</a> 
     72                       <li> <a href="import_single_matrix">import a matrix from another program</a> 
    6973       </ul> 
    7074 
     
    286290    m = manager.create(PWMMotif, name=name, source_motifs="\n".join(sequences), 
    287291                       motif_type='PWM', 
    288                        default_threshold=lowest_score, 
     292                       threshold=lowest_score, 
    289293                       notes=notes, 
    290294                       folder_id=folder_id) 
     
    383387    ### 
    384388 
     389template display_pwm_from_sites(request, name, sequences, notes): 
     390    header("Motif '%s' created" % (name,)) 
     391     
     392    "<h2>Matrix motif '%s' created.</h2>" % (name,) 
     393     
     394    pwm = motility.make_pwm(sequences) 
     395    site_scores = [ (pwm.calc_score(s),s) for s in sequences ] 
     396    site_scores.sort() 
     397    site_scores.reverse() 
     398 
     399    lowest_score = site_scores[-1][0] 
     400     
     401    ### 
     402 
     403    display_pwm_information(pwm, sequences, lowest_score) 
     404 
     405    ### create 
     406 
     407    manager = cartwheel.website.get_object_manager() 
     408 
     409    folder_id = request.session.get_current_folder().id 
     410 
     411    m = manager.create(PWMMotif, name=name, source_motifs="\n".join(sequences), 
     412                       motif_type='PWM', 
     413                       threshold=lowest_score, 
     414                       notes=notes, 
     415                       folder_id=folder_id) 
     416 
     417    manager.commit() 
     418 
     419    """ 
     420    <p> 
     421    <a href='./edit?motif_id=%d'>Edit motif information and threshold</a> 
     422    <p> 
     423    <a href='./'>Return to motif list</a> 
     424    """ % (m.id,) 
     425 
     426    footer() 
     427 
     428### 
     429 
     430def import_single_matrix[html](request): 
     431    if not request.form: 
     432        return wrap("""\ 
     433<form method="POST"> 
     434<h3>Import a matrix motif (PWM) from another program</h3> 
     435 
     436<b>Instructions:</b> 
     437<p> 
     438... 
     439<p> 
     440<hr> 
     441Name: <input type='text' name='name'> 
     442<p> 
     443Matrix data type: 
     444<select name='format'> 
     445 <option value='JASPAR'> JASPAR 
     446</select> 
     447<p> 
     448Matrix data: 
     449<br> 
     450<textarea name='matrix' rows='8' cols='40'></textarea> 
     451<p> 
     452Notes:<br> 
     453<blockquote> 
     454 <textarea name='notes' rows='5' cols='50'></textarea> 
     455</blockquote> 
     456<p> 
     457<input type='submit' value='import'> 
     458</form> 
     459""", show_anything=False) 
     460 
     461    errors = [] 
     462 
     463    name = request.form['name'].strip() 
     464    matrix = request.form['matrix'] 
     465    format = request.form['format'] 
     466    notes = request.form.get('notes', '') 
     467 
     468    if not name: 
     469        errors.append("you must enter a name!") 
     470 
     471    if format not in ('JASPAR',): 
     472        errors.append("unknown data format: %s" % (format,)) 
     473 
     474    if errors: 
     475        return error("ERROR:<p><ul><li> %s</ul>" % ("<li>".join(errors,))) 
     476 
     477    header("Motif '%s' created" % (name,)) 
     478     
     479    "<h2>Matrix motif '%s' created.</h2>" % (name,) 
     480     
     481    ### create 
     482 
     483    manager = cartwheel.website.get_object_manager() 
     484    folder_id = request.session.get_current_folder().id 
     485 
     486    m = manager.create(RawPWMMotif, name=name, matrix=matrix, 
     487                       motif_type=str('RawPWM'), 
     488                       format=format, 
     489                       threshold=0, 
     490                       notes=notes, 
     491                       folder_id=folder_id) 
     492 
     493    manager.commit() 
     494 
     495    """ 
     496    <p> 
     497    <a href='./edit?motif_id=%d'>Edit motif information and threshold</a> 
     498    <p> 
     499    <a href='./'>Return to motif list</a> 
     500    """ % (m.id,) 
     501 
     502    footer()