Retrieving SMILES string from Marvin to ASP.NET server

User 0908c5ccdd

12-11-2007 12:14:42

Hi





I am a complete novice to JavaScript so apologies if this seems like a simple question. I would appreciate some help getting my Marvin Sketch application to work. I have a page that works fine, using the Marvin Viewer. You can see by the code below that it is simple, but it seems to work. The SMILES string is provided by a variable sSMILES in ASP.NET.





Code:
<div>                         


    <script language= "Javascript1.1" src="Marvin/marvin.js"></script>


    <script language="Javascript1.1">


    <!--


    mview_begin("Marvin/", 100, 100);


    mview_param("mol", "<%= convertForJavaScript(sSMILES) %>")


    mview_param("background", "#FFF5F5");


    mview_end();


    !-->


    </script>                 


</div>






I cannot seem to get the process to work in reverse, ie. capturing the structure from the Sketch window in the form of a SMILES string and storing it as an ASP.NET variable. The page on which the Sketch window is displayed has a postback button to capture data from other controls. I would really like to capture the SMILES code from the structure drawn in the Sketch window too.





So far I have the code below. It does not give any errors and it displays the Sketch window and allows me to draw, but when I query the variable sSmilesIn back in the ASP.NET server it does not contain the SMILES string that I am after.





Code:
<div>                       


    <script language= "Javascript1.1" src="Marvin/marvin.js"></script>


    <script language="Javascript1.1">


    <!--


   


    function exportMol(format) {


       if(document.MSketch != null) {


          var sSmilesIn = document.MSketch.getMol(format);


          sSmilesIn = unix2local(sSmilesIn); // Convert "\n" to local line separator


          document.MolForm.MolTxt.value = sSmilesIn;


       } else {


          alert("Cannot import molecule:\n"+


                "no JavaScript to Java communication in your browser.\n");


       }


    }


   


    msketch_begin("Marvin/", 680, 300);


    msketch_param("background", "#FFF5F5");


    msketch_param("preload", "MolExport,Parity,SmilesExport,Clean2D,MyformatExport");


    msketch_end();


    !-->


    </script>                 


    </div>






If any has managed to get this to work with ASP.NET then I'd be very grateful for a tip. Many thanks...

ChemAxon 7c2d26e5cf

12-11-2007 13:54:30

Are you sure that the given structure is convertable into SMILES?


Please display the getMol output to check there is any output string by the applet.


Or the output string may be contain some html specific characters that cause problem to JavaScript.


If you can not display getMol output, you can test SMILES export also with the help of Edit/Source window (by selecting SMILES format).


Both getMol and Edit Source have to give the same result.

User 0908c5ccdd

12-11-2007 14:15:52

Tamas wrote:
Are you sure that the given structure is convertable into SMILES?


Please display the getMol output to check there is any output string by the applet.


Or the output string may be contain some html specific characters that cause problem to JavaScript.


If you can not display getMol output, you can test SMILES export also with the help of Edit/Source window (by selecting SMILES format).


Both getMol and Edit Source have to give the same result.
Hi Tamas





Thanks for replying. My problem is more the mechanism for retrieving the SMILES string. I am sure that the structure is convertable, it is just a benzene ring from the list of ring structures in the Sketcher applet. If I understand correctly then I could possibly use an asp: HiddenField control to return the SMILES string back to my VB.NET code when the user clicks the 'Save' button on the form. This will also return other data held in text boxes on the web page.





I am hoping to extract the value from this hidden field back in the code and then process it onwards from there. I am stuck on how to get the SMILES code back to my VB.NET code back on the ASP.NET server. Perhaps something along the lines of: HiddenFieldSmiles.value = but I'm not sure how to assign the SMILES code from the structure into this value. The code I have below isn't working. Hope you can help?





Code:
<div>                       


    <script language= "Javascript1.1" src="Marvin/marvin.js"></script>


    <script language="Javascript1.1">


    <!--


   


    function exportMol(format) {


       if(document.MSketch != null) {


          HiddenFieldSmiles.value = document.MSketch.getMol(format);


          sSmilesIn = unix2local(sSmilesIn); // Convert "\n" to local line separator


          HiddenFieldSmiles.value = document.MolForm.MolTxt.value;


       } else {


          alert("Cannot import molecule:\n"+


                "no JavaScript to Java communication in your browser.\n");


       }


    }


   


    msketch_begin("Marvin/", 680, 300);


    msketch_param("background", "#FFF5F5");


    msketch_param("preload", "MolExport,Parity,SmilesExport,Clean2D,MyformatExport");


    msketch_end();


    !-->


    </script>                 


    </div>






and then back in VB.NET on the server:





Code:
msgbox(HiddenFieldSmiles.value)

ChemAxon 7c2d26e5cf

12-11-2007 15:15:28

I am not a .NET developer. I have no experience neither in VB.NET nor ASP.NET server. But I am presuming that the problem can be in the posing of the form. Use "POST" method (instead of "GET") by submitting a form.


By GET, the form data (parameters) are inserted in the requested URL (in the query part of the URL). Since, you can not use special characters in URL string, you should encode data in this case. Probably the missing encoding causes the problem (use the escape() method for encoding and unescape() for decoding).


Furthermore, the URL length is limited, so you can not transfer any length of data via GET protocol.


By POST, you the parameters are inserted in the HTTP request body, there is no limitation.

User 0908c5ccdd

13-11-2007 10:16:23

OK, I think I'm nearly there on this one. Just need a list bit of help.





I have managed to work out how to retrieve a string from a JavaScript function running on the client control and send it back to the server for processing in the VB.NET server code. This is how I propose to retrieve the SMILES string for my molecule that the user will draw, and then click the ASP.NET submit button.





I hope that somebody at ChemAxon will be able to help me with the last part of my problem. This is how to grab the SMILES string from a molcule that exists in the MarvinSketch applet. I have code as below, note that the RetrieveSmiles function is run when the ASP.NET button is clicked by the user. The code behind the button expects a value in the hidden control to be sent back to the server. All of this works, I just need to know how to access the SMILES string from a structure drawn into the Sketch control - I can then use this variable instead of my literal string "test".





Code:
  <div>


    <asp:HiddenField ID="HiddenFieldSmiles" runat="server"/>


         


    <script language= "Javascript1.1" src="Marvin/marvin.js"></script>


    <script language="Javascript1.1">


    <!--   


   function RetrieveSmiles()


   {


   document.getElementByID('<%=HiddenFieldSmiles.ClientID%>').value = "test";


   }


   !-->


    </script>  &nbsp;


    </div>





<asp:Button ID="ButtonSaveCreate" runat="server" OnClientClick="return RetrieveSmiles()" Text="Save"/>


ChemAxon 7c2d26e5cf

13-11-2007 15:41:42

Sorry, but I don't understand you. Seeing your earlier code, I have believed you know how to retrieve the SMILES from the applet.


Anyway, here is a simple code to do it:


Code:
function getSmiles() {


if(document.MSketch != null) {


    return document.MSketch.getMol("smiles");


}


return "";


}

ChemAxon 7c2d26e5cf

13-11-2007 15:44:07

If I misunderstood your question, please tell me.

User 0908c5ccdd

13-11-2007 16:33:55

Hi Tamas





I have established that my version of Internet Explorer does not appear to support LiveConnect. Therefore I cannot use your code from the previous post and I need to use something like the following code:


Code:



function submitMethod() {


        document.molform.molecule.value = getResult();


    }


   


   function RetrieveSmiles() {


   setMethod("MSketch.getMol","java.lang.String");


   addMethodParam('smiles');


   setPostJsMethod("parent.submitMethod()");


   runMethod();


   }






However, being new to JavaScript, I still need to figure out how I can get the text SMILES string (eg. c1cccccc1N) into the variable labelled "s" in the code shown below. It is this code that is called when the user clicks the ASP.NET button control. How can I get this second piece of JavaScript code to call the first section of code and return the variable "s", being the SMILES string, so that it can be sent back to the server?





Code:
function RetrieveSmiles()


   {


   document.getElementById("<%=HiddenFieldSmiles.ClientID%>").value = s;


   }

ChemAxon 7c2d26e5cf

13-11-2007 17:20:07

Which IE version do you use? Most of IE supports LiveConnect.


http://www.chemaxon.com/marvin/doc/dev/applet-platforms.html

User 0908c5ccdd

13-11-2007 17:26:00

I am using IE7 and the isLiveConnect function returns false from JavaScript.





Isn't there a function that just returns the SMILES string from the MarvinSketch applet back into a JavaScript variable? Once I have it there then I can use it and send it back to ASP.NET.





Thanks...

ChemAxon 7c2d26e5cf

13-11-2007 17:32:45

I hardly believe it. IE7 supports LiveConnect.


Please check the following example:


http://www.chemaxon.com/marvin/examples/applets/example-sketch3.4.html


Draw something in the sketcher then push "Export" button.


If the draw structure source displays in the text box (above the applet), liveconnect works.

User 0908c5ccdd

14-11-2007 10:13:59

Hi Tamas





OK, the example on the page you quote works.





However, when I run the code below I see that apparently LiveConnect is not functioning. As you can see, the the function named CheckConnect is run when I click the button on the form. When I do this it returns "LiveConnect is not working!"





Code:
<div>


    <script language= "Javascript1.1" src="Marvin/marvin.js"></script>


    <script language="Javascript1.1">


    <!--   


    msketch_begin("Marvin/", 680, 300);


    msketch_param("background", "#FFF5F5");


    mview_param("mol", "c1ccccc1");


    msketch_end();


    setPath("Marvin/..");


   


   var isJs2Java = isLiveConnect();


   


   function CheckConnect() {


   if(isJs2Java) {


       alert("LiveConnect is working!");


   } else if(!isJs2Java) {


       alert("LiveConnect is not working!");


       }   


   }


   !-->


    </script>


    </div>


<asp:Button ID="ButtonSaveCreate" runat="server" OnClientClick="return CheckConnect()" Text="Save"/>








Please though, could you answer my more important question that is: rather than concentrate on passing the SMILES string to a form to be sumitted isn't there an easy way to simply assign the SMILES string to a variable within the JavaScript? Once I have the SMILES string in a variable then I can submit it to the server in a different way (that currently works) and my problem is solved.





Many thanks for your help on this.

ChemAxon 0e37943a96

14-11-2007 15:05:42

I think the function isLiveConnect() in js2java.js returns the correct result only with MacOs.





You could check your LiveConnect capability on the following sites:





http://www.javasonics.com/support/check_liveconnect.php


http://developer.apple.com/internet/safari/samples/ColorBlockApplet.html





Without LiveConnect you could write the following (into the submit function):


Code:
function submitMethod() {


    document.getElementById("<%=HiddenFieldSmiles.ClientID%>").value = getResult();


    }






Have you named the applet with msketch_name = "MSketch"; before msketch_begin?





Could you post your complete aspx page?

User 0908c5ccdd

14-11-2007 15:31:12

tpelcz wrote:






Have you named the applet with msketch_name = "MSketch"; before msketch_begin?





Could you post your complete aspx page?
Excellent!! Many thanks... the problem was that I had not named the sketch before msketch_begin. I can now receive the SMILES string back in ASP.NET. I will gladly post the complete solution on this thread for reference once I have it polished a bit more.

User 010ddfdc89

10-01-2008 06:16:20

Hi - wonder if you ever got around to finishing up that code. I'm having the exact same problem.





Cheers

User 0908c5ccdd

10-01-2008 16:48:25

Sure - here is my code. There is almost certainly a better and more secure way of handling this, but it works.





Code:
<div>


  <asp:HiddenField ID="HiddenFieldSmilesCreate" runat="server"/>


         


   <script language= "Javascript1.1" src="Marvin/marvin.js"></script>


   <script language="Javascript1.1">


   <!--   


      msketch_name = "MSketchCreate";


      msketch_begin("Marvin/", 680, 300);


      msketch_param("background", "#FFF5F5");


      msketch_end();


       


   function RetrieveSmilesCreate()


   {


   document.getElementById("<%=HiddenFieldSmilesCreate.ClientID%>").value = document.MSketchCreate.getMol("smiles");


   }


   !-->


   


   </script>


</div>









In the ASP.NET code I have the code below in the button tag so that it fires the JavaScript function and posts back to the server.





Code:
<asp:Button ID="ButtonSaveCreate" runat="server" OnClientClick="return RetrieveSmilesCreate()" Text="Save"/>






In your server-side code you should then be able to pick the value out of the hidden field control. Remember, as ChemAxon pointed out earlier in the thread, to name the sketch in the first line of JavaScript. It's important to refer to your hidden field control correctly in the JavaScript code because the id of the control changes at run time. If you use getElementById("<%=HiddenFieldSmilesCreate.ClientID%>") then this should pick up the unique id of the control being displayed on the browser.





Hope it works.

User 010ddfdc89

16-01-2008 01:56:14

Great ! Thanks. I 'll give it a shot later and let you know how it goes.