Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 836419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:58:40+00:00 2026-05-15T04:58:40+00:00

I’m storing contacts between different elements. I want to eliminate elements of certain type

  • 0

I’m storing contacts between different elements. I want to eliminate elements of certain type and store new contacts of elements which were interconnected by the eliminated element.

Problem background

Imagine this problem. You have a water molecule which is in contact with other molecules (if the contact is a hydrogen bond, there can be 4 other molecules around my water). Like in the following picture (A, B, C, D are some other atoms and dots mean the contact).

 A   B
 |   |
 H   H
  . .
   O
  / \
 H   H
 .   .
 C   D

I have the information about all the dots and I need to eliminate the water in the center and create records describing contacts of A-C, A-D, A-B, B-C, B-D, and C-D.

Database structure

Currently, I have the following structure in the database:

Table atoms:

  • "id" integer PRIMARY KEY,
  • "amino" char(3) NOT NULL, (HOH for water or other value)
  • other columns identifying the atom

Table contacts:

  • "acceptor_id" integer NOT NULL, (the atom near to my hydrogen, here C or D)
  • "donor_id" integer NOT NULL, (here A or B)
  • "directness" char(1) NOT NULL, (this should be D for direct and W for water-mediated)
  • other columns about the contact, such as the distance

EDIT:
How would look the data in the case depicted earlier.

atoms:

id|atom|amino
1 | O  | HOH
2 | N  | ARG  <- atom A from image
3 | S  | CYS  <- B 
4 | O  | SER  <- C
5 | N  | ARG  <- D

contacts:

donor_id|acceptor_id|directness
1        4           D
1        5           D
2        1           D
3        1           D

From which I need to make

contacts:

donor_id|acceptor_id|directness
3        4           W            <- B-C
3        5           W            <- B-D
2        4           W            <- A-C
2        5           W            <- A-D
2        3           X            <- A-B    (These last two rows are escaping me,
4        5           X            <- C-D     there could be also row D-C, but not
                                             both C-D and D-C. A char 'X' could 
                                             be used to mark "no donor/acceptor")

Current solution (insufficient)

Now, I’m going through all the contacts which have donor.amino = "HOH". In this sample case, this would select contacts from C and D. For each of these selected contacts, I look up contacts having the same acceptor_id as is the donor_id in the currently selected contact. From this information, I create the new contact. At the end, I delete all contacts to or from HOH.

This way, I am obviously unable to create C-D and A-B contacts (the other 4 are OK).

If I try a similar approach – trying to find two contacts having the same donor_id, I end up with duplicate contacts (C-D and D-C).

Is there a simple way to retrieve all six contacts without duplicates?

I’m dreaming about some one page long SQL query which retrieves just these six wanted rows. 🙂
However, any other ideas are welcome.

It is preferable to conserve information about who is donor (where possible), but not strictly necessary.

Big thanks to all of you who read this question to this point.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T04:58:41+00:00Added an answer on May 15, 2026 at 4:58 am

    Well, its hard to provide examples in comments, I decided to post an answer:

    If you have to following original data, there is no way to distinguish data from the first structure from those of the second. There should be an additional grouping condition to eleminate directions between the first and the second structure.

    sqlite> create table atoms (id INT, atom TEXT, amino TEXT);
    sqlite> insert into atoms VALUES (1, 'O', 'HOH');
    sqlite> insert into atoms VALUES (2, 'A', 'ARG');
    sqlite> insert into atoms VALUES (3, 'B', 'CYS');
    sqlite> insert into atoms VALUES (4, 'C', 'SER');
    sqlite> insert into atoms VALUES (5, 'D', 'ARG');
    sqlite> insert into atoms VALUES (6, 'O1', 'HOH');
    sqlite> insert into atoms VALUES (7, 'A1', 'ARG');
    sqlite> insert into atoms VALUES (8, 'B1', 'CYS');
    sqlite> insert into atoms VALUES (9, 'C1', 'SER');
    sqlite> insert into atoms VALUES (10, 'D1', 'ARG');
    sqlite> select * from atoms;
    1|O|HOH
    2|A|ARG
    3|B|CYS
    4|C|SER
    5|D|ARG
    6|O1|HOH
    7|A1|ARG
    8|B1|CYS
    9|C1|SER
    10|D1|ARG
    

    UPD

    Here is the original data:

    sqlite> .headers ON
    sqlite> .mode columns
    sqlite> select * from atoms;
    id          atom        amino
    ----------  ----------  ----------
    1           O           HOH
    2           A           ARG
    3           B           CYS
    4           C           SER
    5           D           ARG
    6           O1          HOH
    7           A1          ARG
    8           B1          CYS
    9           C1          SER
    10          D1          ARG
    sqlite> select * from contacts;
    donor_id    acceptor_id  directness
    ----------  -----------  ----------
    1           4            D
    1           5            D
    2           1            D
    3           1            D
    6           9            D
    6           10           D
    7           6            D
    8           6            D
    

    Here is the query:

    select
        c1.donor_id, c2.acceptor_id, 'W' as directness
    from
        contacts c1, contacts c2, atoms a
    where
        c1.acceptor_id = c2.donor_id
        and c1.acceptor_id=a.id
        and a.amino='HOH'
    UNION ALL
    select
        c1.donor_id, c2.donor_id, 'X' as directness
    from
        contacts c1, contacts c2, atoms a
    where
        c1.acceptor_id = c2.acceptor_id
        and c1.acceptor_id=a.id
        and a.amino='HOH'
        and c1.donor_id < c2.donor_id
    UNION ALL
    select
        c1.acceptor_id, c2.acceptor_id, 'X' as directness
    from
        contacts c1, contacts c2, atoms a
    where
        c1.donor_id = c2.donor_id
        and c1.donor_id=a.id
        and a.amino='HOH'
        and c1.acceptor_id < c2.acceptor_id;
    

    Here is the result:

    donor_id    acceptor_id  directness
    ----------  -----------  ----------
    2           4            W
    2           5            W
    3           4            W
    3           5            W
    7           9            W
    7           10           W
    8           9            W
    8           10           W
    2           3            X
    7           8            X
    4           5            X
    9           10           X
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I want use html5's new tag to play a wav file (currently only supported
I would like to run a str_replace or preg_replace which looks for certain words
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I used javascript for loading a picture on my website depending on which small
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.