Correct way to use JChemRgroupDecomposiiton?

User 1ccd233681

09-02-2014 22:46:08

Hello,


I am trying to use JChemRGroupDecomposition in some vb.net code and I keep getting an error thrown. I'm trying to instantiate a new Rgroupdecomp, set the core and target molecules, and then run the decomposition. However everything I run this I get an "Object reference not set to an instance of an object" error thrown when I try to call result.First(). My code and the stack trace are shown below for reference.


My guess is that I'm not setting this up correctly, however I can't find any documentation to help me. Does anyone know where to documentation for the JChem* classes (e.g. JChemMolecule, JChemRGroupDecomposition) can be found?


Thanks!


 
  StackTrace:
       at ChemAxon.NET.IKVM.RGroupDecomposition.JChemRGroupDecomposition.†††
    †††Š†š(Object )
       at ChemAxon.NET.IKVM.RGroupDecomposition.JChemRGroupDecomposition.GetAdditionalColumns()
       at ChemAxon.NET.IKVM.RGroupDecomposition.JChemRGroupDecomposition.<GetResults>d__0.†††
    †††ŒŒ(Object )
       at ChemAxon.NET.IKVM.RGroupDecomposition.JChemRGroupDecomposition.<GetResults>d__0.MoveNext()
       at System.Linq.Enumerable.Count[TSource](IEnumerable`1 source)

 


  Public Sub loadRGroups(ByVal core As JChemMolecule, ByVal molecule as JChemMolecule)


        If (core IsNot Nothing AndAlso molecule IsNot Nothing) Then
            Dim rgDecomp As New chemaxon.NET.IKVM.RGroupDecomposition.JChemRGroupDecomposition()
            

            rgDecomp.QueryMolecule = core
            rgDecomp.Targets = New BasicJChemMoleculeEnumerator(New List(Of IJChemMolecule) From {molecule})

            Dim result = rgDecomp.GetResults()
            If (result IsNot Nothing AndAlso result.Count() > 0) Then ' <--error thrown here
                rGroups = result.First()
            End If
        End If
    End Sub

ChemAxon 2e7d8629fa

10-02-2014 12:06:51

 Hi,


 


As I can see the problem is the calling of extension method Count(). At this point you reach the end of the 'result' enumeration (result = rgDecomp.GetResults()) so the method First() throws an exception.


What you can do:


1.) Check the 'result' that is NULL or not and use a simple For Each block to read the elements one by one. Do not get the count of the 'result'.


2.) Create a new list (IEnumerable<IJChemMolecule[]>) and load the 'result' into it. You can get the count of the result set and you can get any of the items you want anytime.


 


Regards,


Gergely

User 1ccd233681

10-02-2014 16:29:20










gkovacs wrote:

 Hi,


As I can see the problem is the calling of extension method Count(). At this point you reach the end of the 'result' enumeration (result = rgDecomp.GetResults()) so the method First() throws an exception.


What you can do:


1.) Check the 'result' that is NULL or not and use a simple For Each block to read the elements one by one. Do not get the count of the 'result'.


2.) Create a new list (IEnumerable<IJChemMolecule[]>) and load the 'result' into it. You can get the count of the result set and you can get any of the items you want anytime.



Thanks for the prompt reply Gergely. I'm afraid I don't quite understand all of what you said so let me follow-up each of your points. But first let me ask, have I missed anything in the setup of the Rgroupdecomp before I call get results? 


My comments on your points:


a. "At this point you reach the end of the 'result' enumeration (result = rgDecomp.GetResults()) so the method First() throws an exception."


According to the stack trace, the method First() does not throw the exception. Instead the method Count() throws the exception. Also, my understanding is that IEnumerables do not maintain "state" in that they have a current position. Consequently, Calling First() will always return the first item regardless of the last item accessed.


 


b. "Check the 'result' that is NULL or not and use a simple For Each block to read the elements one by one. Do not get the count of the 'result'."


I do check the the result is not null. An it isn't null (or "nothing" in vb.net parlance). Any non-null object that implements IEnumerable should respond successfully to the Count() function -- yet this one doesn't -- that is strange to me. I did what you recommended and instead of calling count() performed a simple foreach loop, but this throws the same error:


            Dim result = rgDecomp.GetResults()
            If (result IsNot Nothing) Then
                For Each r In result ' <- throws the same error as before
                    'never reaches here
                Next
            End If



c. "Create a new list (IEnumerable<IJChemMolecule[]>) and load the 'result' into it. You can get the count of the result set and you can get any of the items you want anytime."


 


I am trying various way to test this now. will report back if I have any luck with this.


 


Thanks!


 

ChemAxon 2e7d8629fa

11-02-2014 09:48:34

 Hi,


 


Which version of JChem .NET API do you use? I have executed the following code successfully with version 6.2:


Public Sub loadRGroups()
        Dim core As JChemMolecule = New JChemMolecule(New MoleculeData("ClC1=CC([$([#1,*])])=C([$([#1,*])])C([$([#1,*])])=C1 |$;;;;_R1;;_R2;;_R1;$,c:8,t:1,4|", MoleculeFormat.CXSMILES))
        Dim molecule As JChemMolecule = New JChemMolecule(New MoleculeData("CCC(N)C1=CC(Cl)=CC(C(N)CC)=C1Br", MoleculeFormat.SMILES))

        If (core IsNot Nothing AndAlso molecule IsNot Nothing) Then
            Dim rgDecomp As New ChemAxon.NET.IKVM.RGroupDecomposition.JChemRGroupDecomposition()

            rgDecomp.QueryMolecule = core
            rgDecomp.Targets = New BasicJChemMoleculeEnumerator(New List(Of IJChemMolecule) From {molecule})

            Dim result = rgDecomp.GetResults()

            'If (result IsNot Nothing AndAlso result.Count() > 0) Then ' <--error thrown here
            '    Dim rGroups = result.First()
            'End If

            If (result IsNot Nothing) Then
                For Each r In result
                    Dim formula = r.ElementAt(0).Formula
                Next
            End If
        End If
    End Sub


 


If you use older version than 6.2 you need to initialize the r-group options:


Dim rgDecomp As New ChemAxon.NET.IKVM.RGroupDecomposition.JChemRGroupDecomposition()
rgDecomp.Options = New ChemAxon.NET.Base.RGroupDecomposition.RGroupDecompositionOptions()


 


You can download version 6.2. from here:


http://www.chemaxon.com/download/jchem-suite/#jchemdotnet


 


Regards,


Gergely

User 1ccd233681

11-02-2014 17:26:58










gkovacs wrote:

 Hi,


 


Which version of JChem .NET API do you use? I have executed the following code successfully with version 6.2:


Public Sub loadRGroups()
        Dim core As JChemMolecule = New JChemMolecule(New MoleculeData("ClC1=CC([$([#1,*])])=C([$([#1,*])])C([$([#1,*])])=C1 |$;;;;_R1;;_R2;;_R1;$,c:8,t:1,4|", MoleculeFormat.CXSMILES))
        Dim molecule As JChemMolecule = New JChemMolecule(New MoleculeData("CCC(N)C1=CC(Cl)=CC(C(N)CC)=C1Br", MoleculeFormat.SMILES))

        If (core IsNot Nothing AndAlso molecule IsNot Nothing) Then
            Dim rgDecomp As New ChemAxon.NET.IKVM.RGroupDecomposition.JChemRGroupDecomposition()

            rgDecomp.QueryMolecule = core
            rgDecomp.Targets = New BasicJChemMoleculeEnumerator(New List(Of IJChemMolecule) From {molecule})

            Dim result = rgDecomp.GetResults()

            'If (result IsNot Nothing AndAlso result.Count() > 0) Then ' <--error thrown here
            '    Dim rGroups = result.First()
            'End If

            If (result IsNot Nothing) Then
                For Each r In result
                    Dim formula = r.ElementAt(0).Formula
                Next
            End If
        End If
    End Sub



Hello. I think we are making some progress. I updated to 6.2 and now I can get the code you provided to work. However there are still issues.


#1. If I insert a Count() call before getting the results this causes an error. I know you'll simply suggest that I "don't call Count()" , but at a fundamental level, you *should* be able to call Count() if IEnumerable is being properly implemented. The fact that it doesn't work suggests some problem either with the underlying code structure or with my understanding of how IEnumerables should work.


Public Sub loadRGroups()
        Dim core As JChemMolecule = New JChemMolecule(New MoleculeData("ClC1=CC([$([#1,*])])=C([$([#1,*])])C([$([#1,*])])=C1 |$;;;;_R1;;_R2;;_R1;$,c:8,t:1,4|", MoleculeFormat.CXSMILES))
        Dim molecule As JChemMolecule = New JChemMolecule(New MoleculeData("CCC(N)C1=CC(Cl)=CC(C(N)CC)=C1Br", MoleculeFormat.SMILES))
       

	 If (core IsNot Nothing AndAlso molecule IsNot Nothing) Then
            Dim rgDecomp As New ChemAxon.NET.IKVM.RGroupDecomposition.JChemRGroupDecomposition()
            rgDecomp.QueryMolecule = core
            rgDecomp.Targets = New BasicJChemMoleculeEnumerator(New List(Of IJChemMolecule) From {molecule})
          

	  Dim result = rgDecomp.GetResults()


            If (result IsNot Nothing) Then
'Debug.Print(results.Count()
                For Each r In result
                    Dim formula = r.ElementAt(0).Formula
                Next
            End If
        End If
    End Sub




# 2. If I extend your code by looping through each molecule in the array 'r', then the code generates an error at the position shown.

Public Sub loadRGroups()
        Dim core As JChemMolecule = New JChemMolecule(New MoleculeData("ClC1=CC([$([#1,*])])=C([$([#1,*])])C([$([#1,*])])=C1 |$;;;;_R1;;_R2;;_R1;$,c:8,t:1,4|", MoleculeFormat.CXSMILES))
        Dim molecule As JChemMolecule = New JChemMolecule(New MoleculeData("CCC(N)C1=CC(Cl)=CC(C(N)CC)=C1Br", MoleculeFormat.SMILES))
       

	 If (core IsNot Nothing AndAlso molecule IsNot Nothing) Then
            Dim rgDecomp As New ChemAxon.NET.IKVM.RGroupDecomposition.JChemRGroupDecomposition()
            rgDecomp.QueryMolecule = core
            rgDecomp.Targets = New BasicJChemMoleculeEnumerator(New List(Of IJChemMolecule) From {molecule})
          

	  Dim result = rgDecomp.GetResults()


            If (result IsNot Nothing) Then
                For Each r In result

			For each n in r
                    Debug.Print(n.Formula)

			Next
                Next ' throws "Object reference not set to an instance of an object."
            End If
        End If
    End Sub




 

ChemAxon 2e7d8629fa

12-02-2014 14:34:06

 Hi,


About the issues what you mentioned:


#1 You are right, an object which implements IEnumerable interface has to support First() after calling Count(). We will fix it soon. Thank you for the feedback.


#2 JChem .NET API is a layer over the low-level JChem API. This JChem API can give back NULL (Nothing) as R-group result. You should check this case. (In your project variables 'r' and 'n' can be Nothing.)


You can find information about R-group decomposition and the related API on the following pages:


http://www.chemaxon.com/jchem/doc/user/RGroupDecomposition.html


http://www.chemaxon.com/jchem/doc/dev/java/api/chemaxon/sss/search/RGroupDecomposition.html


Best regards,


Gergely

User 1ccd233681

13-02-2014 02:42:41










gkovacs wrote:

 Hi,


About the issues what you mentioned:


#1 You are right, an object which implements IEnumerable interface has to support First() after calling Count(). We will fix it soon. Thank you for the feedback.


#2 JChem .NET API is a layer over the low-level JChem API. This JChem API can give back NULL (Nothing) as R-group result. You should check this case. (In your project variables 'r' and 'n' can be Nothing.)


You can find information about R-group decomposition and the related API on the following pages:


http://www.chemaxon.com/jchem/doc/user/RGroupDecomposition.html


http://www.chemaxon.com/jchem/doc/dev/java/api/chemaxon/sss/search/RGroupDecomposition.html


Best regards,


Gergely



Gergely,


I followed all your advice and have it working now. Thank you very much for your help!


--Josh

User 1ccd233681

19-02-2014 19:20:32

A follow up question about using rgroupdecompose from .NET.


This page:  http://www.chemaxon.com/jchem/doc/dev/java/api/chemaxon/sss/search/RGroupDecomposition.html


described how to set the alignment options for the rgroup decomposition using thejava API.


 RGroupDecomposition rgd = new RGroupDecomposition();
rgd.setAttachmentType(RGroupDecomposition.ATTACHMENT_POINT);
rgd.setAlign(true);


How can I do the same thing in .NET? I can't seem to find alignment options in the RGroupDecomposition object.


Best,

--Josh

User 1ccd233681

28-02-2014 18:28:19

Bumping the follow-up question posted above.

ChemAxon bd13b5bd77

03-03-2014 08:35:06

We are exposing the alignment option in 6.3.

User 1ccd233681

03-03-2014 21:27:17

Okay. Thanks for the info.


 


Best,


--Josh