cancel
Showing results for 
Search instead for 
Did you mean: 

Creating two types of nodes with a relationship between them from csv having empty cells

Kevin6482
Node Clone

I would like to create two types of nodes (CHEMICAL & DISEASE) and their relationships from Relation column. There could be 4 combinations (Source_CHEMICAL--Relation-->Target_CHEMICAL, Source_CHEMICAL--Relation-->Target_DISEASE,Source_DISEASE--Relation-->Target_CHEMICAL,Source_DISEASE--Relation-->Target_DISEASE)

 

id SourceType_CHEMICAL SourceType_DISEASE Relation TargetType_CHEMICAL TargetType_DISEASE
1   cardiac myosin induce   myocarditis
2 nitric   inhibit chrysin  
3 sesame peptide powder   exhibited angiotensin  
4 allergen   induce   hypersensitivity
5   leucoencephalopathy caused   infection

I tried using below query, but it didn't work and gave an error for below line,

"MERGE(c1)-[:row.Relation]->(c2)"
Neo.ClientError.Statement.SyntaxError: Invalid input '.': expected

can someone help with this? Thanks

 

CREATE CONSTRAINT ON (n:CHEMICAL) ASSERT n.SourceType_CHEMICAL IS UNIQUE;
CREATE CONSTRAINT ON (n:CHEMICAL) ASSERT n.TargetType_CHEMICAL IS UNIQUE;
CREATE CONSTRAINT ON (n:DISEASE) ASSERT n.SourceType_DISEASE IS UNIQUE;
CREATE CONSTRAINT ON (n:DISEASE) ASSERT n.TargetType_DISEASE IS UNIQUE;

 

LOAD CSV WITH HEADERS FROM "file:///Chemical_Disease.csv" AS row
MERGE(c1:CHEMICAL{name:row.SourceType_CHEMICAL})
MERGE(c2:DISEASE{name:row.TargetType_DISEASE})
MERGE(c1)-[:row.Relation]->(c2)

 

MERGE(c3:CHEMICAL{name:row.SourceType_CHEMICAL})
MERGE(c4:CHEMICAL{name:row.TargetType_CHEMICAL})
MERGE(c3)-[:row.Relation]->(c4)

 

MERGE(c5):DISEASE{name:row.SourceType_DISEASE})
MERGE(c6:CHEMICAL{name:row.TargetType_CHEMICAL})
MERGE(c5)-[:row.Relation]->(c6)

 

MERGE(c7):DISEASE{name:row.SourceType_DISEASE})
MERGE(c8:DISEASE{name:row.TargetType_DISEASE})
MERGE(c7)-[:row.Relation]->(c8)

#cypher #query

 
1 ACCEPTED SOLUTION

Unfortunately you cannot have dynamic relationship-types in plain cypher.

You'd either have to do a multi-pass over the with a WHERE filter on the rel-type column and different cypher-statements (or perhaps subqueries).

LOAD CSV  ... AS row

CALL { with source, target, row
WITH * WHERE row.relationship = 'induce'
MERGE (source)-[:INDUCE]->(target)
}
...

Or you can use 

call apoc.create.relationship(source, type, properties, target) yield rel

 

View solution in original post

2 REPLIES 2

Unfortunately you cannot have dynamic relationship-types in plain cypher.

You'd either have to do a multi-pass over the with a WHERE filter on the rel-type column and different cypher-statements (or perhaps subqueries).

LOAD CSV  ... AS row

CALL { with source, target, row
WITH * WHERE row.relationship = 'induce'
MERGE (source)-[:INDUCE]->(target)
}
...

Or you can use 

call apoc.create.relationship(source, type, properties, target) yield rel

 

Thank you very much and I'm very sorry for being so late in thanking you for providing a solution