Last active
January 25, 2019 18:29
-
-
Save Irtazaraza/efbad7b8778eb9f01b67a191b2abe3e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
public static void main(String[] args) { | |
SimpleURI localGrakn = new SimpleURI("127.0.0.1", 48555); | |
Keyspace keyspace = Keyspace.of("gene-disease-associations"); | |
Grakn grakn = new Grakn(localGrakn); | |
Grakn.Session session = grakn.session(keyspace); | |
migrateDiseases(session); | |
migrateGenes(session); | |
migrateGeneDiseaseAssociations(session); | |
session.close(); | |
} | |
public static void migrateGenes(Grakn.Session session) { | |
try { | |
BufferedReader reader = Files.newBufferedReader(Paths.get("PATH_TO_CSV_FILE")); | |
CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT); | |
for (CSVRecord csvRecord: csvParser) { | |
// skip header | |
if (csvRecord.getRecordNumber() == 1) { | |
continue; | |
} | |
String geneId = csvRecord.get(0); | |
String geneSymbol = csvRecord.get(1); | |
InsertQuery insertQuery = Graql.insert(var("g").isa("gene") | |
.has("gene-id", geneId) | |
.has("gene-symbol", geneSymbol)); | |
Grakn.Transaction writeTransaction = session.transaction(GraknTxType.WRITE); | |
List<ConceptMap> insertedId = insertQuery.withTx(writeTransaction).execute(); | |
System.out.println("Inserted a gene with ID: " + insertedId.get(0).get("g").id()); | |
writeTransaction.commit(); | |
} | |
System.out.println("-----genes have been migrated-----"); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void migrateDiseases(Grakn.Session session) { | |
try { | |
BufferedReader reader = Files.newBufferedReader(Paths.get("PATH_TO_CSV_FILE")); | |
CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT); | |
for (CSVRecord csvRecord: csvParser) { | |
// skip header | |
if (csvRecord.getRecordNumber() == 1) { | |
continue; | |
} | |
String diseaseName = csvRecord.get(0); | |
String source = csvRecord.get(1); | |
String diseaseId = csvRecord.get(2); | |
double dieaseMim = Double.parseDouble(csvRecord.get(3)); | |
double lastModified = Double.parseDouble(csvRecord.get(4)); | |
InsertQuery insertQuery = Graql.insert(var("d").isa("disease") | |
.has("diease-name", diseaseName) | |
.has("source", source) | |
.has("disease-id", diseaseId) | |
.has("disease-mim", dieaseMim) | |
.has("last-modified", lastModified)); | |
Grakn.Transaction writeTransaction = session.transaction(GraknTxType.WRITE); | |
List<ConceptMap> insertedId = insertQuery.withTx(writeTransaction).execute(); | |
System.out.println("Inserted a disease with ID: " + insertedId.get(0).get("v").id()); | |
writeTransaction.commit(); | |
} | |
System.out.println("-----diseases have been migrated-----"); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void migrateGeneDiseaseAssociations(Grakn.Session session) { | |
try { | |
BufferedReader reader = Files.newBufferedReader(Paths.get("PATH_TO_CSV_FILE")); | |
CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT); | |
for (CSVRecord csvRecord: csvParser) { | |
// skip header | |
if (csvRecord.getRecordNumber() == 1) { | |
continue; | |
} | |
double geneId = Double.parseDouble(csvRecord.get(0)); | |
String diseaseId = csvRecord.get(2); | |
InsertQuery insertQuery = Graql.match( | |
var("g").isa("gene").has("gene-id", geneId), | |
var("d").isa("disease").has("disease-id", diseaseId)) | |
.insert(var("gda").isa("gene-disease-association").rel("associated-gene", "g").rel("associated-disease", "d")); | |
Grakn.Transaction writeTransaction = session.transaction(GraknTxType.WRITE); | |
List<ConceptMap> insertedId = insertQuery.withTx(writeTransaction).execute(); | |
System.out.println("Inserted a gene disease associaiton with ID: " + insertedId.get(0).get("gda").id()); | |
writeTransaction.commit(); | |
} | |
System.out.println("-----gene disease associations have been migrated-----"); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment