[flymine-dev] instruction
Kim Rutherford
flymine-dev@flymine.org
Fri, 16 Dec 2005 14:28:21 +0000
--H4SiwxFQJZ
Content-Type: text/plain; charset=us-ascii
Content-Description: message body text
Content-Transfer-Encoding: 7bit
On Fri, 16 Dec 2005 12:48:30 +0100 (MET), AlexanderP wrote:
> hmm... is there maybe somewhere a step for step instruction for
> putting own data in flymine(-local-database)? (own data from the
> local machine... given as file...)
Hi. Unfortunately, we don't have a good tutorial at the moment.
I understand that you would like to do things like create new
ProteinFeature objects attached to existing Proteins.
One way to do what you want is to write an ANT task that reads a file
and creates ProteinFeature objects in your copy of the FlyMine
database. I've attached a possible implementation that you could use
as a starting point. To try it, you'll need to copy the file to:
flymine/model/genomic/src/java/org/flymine/task/SimpleLoadTask.java
And add this target to build.xml in the flymine directory:
<target name="simple-load-test" depends="compile-genomic-java">
<taskdef name="simple-load-test" classname="org.flymine.task.SimpleLoadTask">
<classpath refid="models.class.path"/>
</taskdef>
<simple-load-test oswAlias="osw.production">
<fileset dir=".">
<include name="my_file"/>
</fileset>
</simple-load-test>
</target>
The Task reads files that look like:
A32CD_DROME protein_feature_1_identifier protein_feature_1_long_name
A33A_DROME protein_feature_2_identifier protein_feature_2_long_name
(etc...)
The first column of each line is the protein identifier, the second is
the identifier of the new ProteinFeature and the third is the longName
field for the new ProteinFeature.
The command:
ant simple-load-test
should be all you need to test things.
It's possible you'll have some problems with properties - let me know
go it goes.
Kim.
--H4SiwxFQJZ
Content-Type: text/plain
Content-Description: SimpleLoadTask.java
Content-Disposition: inline;
filename="SimpleLoadTask.java"
Content-Transfer-Encoding: 7bit
package org.flymine.task;
/*
* Copyright (C) 2002-2005 FlyMine
*
* This code may be freely distributed and modified under the
* terms of the GNU Lesser General Public Licence. This should
* be distributed with the code. See the LICENSE file for more
* information or http://www.gnu.org/copyleft/lesser.html.
*
*/
import org.intermine.objectstore.query.ConstraintOp;
import org.intermine.objectstore.query.Query;
import org.intermine.objectstore.query.QueryClass;
import org.intermine.objectstore.query.QueryField;
import org.intermine.objectstore.query.QueryValue;
import org.intermine.objectstore.query.Results;
import org.intermine.objectstore.query.ResultsRow;
import org.intermine.objectstore.query.SimpleConstraint;
import org.intermine.objectstore.ObjectStoreException;
import org.intermine.objectstore.ObjectStoreWriter;
import org.intermine.objectstore.ObjectStoreWriterFactory;
import org.intermine.util.DynamicUtil;
import org.intermine.util.StringUtil;
import org.flymine.model.genomic.Protein;
import org.flymine.model.genomic.ProteinFeature;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.FileSet;
/**
*
* @author Kim Rutherford
*/
public class SimpleLoadTask extends Task
{
private String oswAlias;
private List fileSets = new ArrayList();
private ObjectStoreWriter osw;
/**
* The ObjectStoreWriter alias to use when querying and creating objects.
* @param oswAlias the ObjectStoreWriter alias
*/
public void setOswAlias(String oswAlias) {
this.oswAlias = oswAlias;
}
/**
* Return the oswAlias set by setOswAlias()
* @return the object store alias
*/
public String getOswAlias() {
return oswAlias;
}
/**
* Return the set of files that should be read from.
* @return the List of FileSets
*/
public List getFileSets() {
return fileSets;
}
/**
* Return the ObjectStoreWriter given by oswAlias.
* @return the ObjectStoreWriter
* @throws BuildException if there is an error while processing
*/
protected ObjectStoreWriter getObjectStoreWriter() throws BuildException {
if (oswAlias == null) {
throw new BuildException("oswAlias attribute is not set");
}
if (osw == null) {
try {
osw = ObjectStoreWriterFactory.getObjectStoreWriter(oswAlias);
} catch (ObjectStoreException e) {
throw new BuildException("cannot get ObjectStoreWriter for: " + oswAlias, e);
}
}
return osw;
}
/**
* Add a FileSet to read from
* @param fileSet the FileSet
*/
public void addFileSet(FileSet fileSet) {
fileSets.add(fileSet);
}
/**
* Load data into the ObjectStoire given by oswAlias.
* @throws BuildException if an ObjectStore method fails
*/
public void execute() throws BuildException {
if (getOswAlias() == null) {
throw new BuildException("oswAlias not set");
}
try {
getObjectStoreWriter().beginTransaction();
} catch (ObjectStoreException e) {
throw new BuildException("cannot begin a transaction", e);
}
Iterator fileSetIter = getFileSets().iterator();
while (fileSetIter.hasNext()) {
FileSet fileSet = (FileSet) fileSetIter.next();
DirectoryScanner ds = fileSet.getDirectoryScanner(getProject());
String[] files = ds.getIncludedFiles();
for (int i = 0; i < files.length; i++) {
File file = new File(ds.getBasedir(), files[i]);
processFile(file);
}
}
try {
getObjectStoreWriter().commitTransaction();
} catch (ObjectStoreException e) {
throw new BuildException("cannot begin a transaction", e);
}
}
private void processFile(File file) {
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
if (line.trim().length() == 0) {
continue;
}
String bits[] = StringUtil.split(line, " ");
String proteinName = bits[0];
Set proteins = getProteinsForName(proteinName);
String proteinFeatureIdentifier = bits[1];
String proteinFeatureName = bits[2];
ProteinFeature proteinFeature =
(ProteinFeature) DynamicUtil.createObject(Collections.singleton(ProteinFeature.class));
proteinFeature.setIdentifier(proteinFeatureIdentifier);
proteinFeature.setName(proteinFeatureName);
proteinFeature.setProteins(proteins);
osw.store(proteinFeature);
}
} catch (FileNotFoundException e) {
throw new BuildException("problem reading file - file not found: " + file, e);
} catch (IOException e) {
throw new BuildException("problem reading file - I/O exception for:" + file, e);
} catch (ObjectStoreException e) {
throw new BuildException("problem storing object", e);
}
}
private Set getProteinsForName(String proteinIdentifier) {
Query q = new Query();
QueryClass qc = new QueryClass(Protein.class);
q.addFrom(qc);
q.addToSelect(qc);
QueryValue qv = new QueryValue(proteinIdentifier);
QueryField qf = new QueryField(qc, "identifier");
SimpleConstraint sc = new SimpleConstraint(qf, ConstraintOp.EQUALS, qv);
q.setConstraint(sc);
Set returnList = new HashSet();
try {
Results res = osw.execute(q);
Iterator resIter = res.iterator();
while (resIter.hasNext()) {
ResultsRow rr = (ResultsRow) resIter.next();
returnList.add(rr.get(0));
}
} catch (ObjectStoreException e) {
throw new BuildException("cannot query Protein objects from database", e);
}
return returnList;
}
}
--H4SiwxFQJZ--