Skip to content
This repository has been archived by the owner on Jan 26, 2021. It is now read-only.

Commit

Permalink
First integration with org.neuroml.export for displaying info and plots
Browse files Browse the repository at this point in the history
  • Loading branch information
tarelli committed Nov 18, 2013
1 parent 0a4b0c7 commit 8338672
Show file tree
Hide file tree
Showing 9 changed files with 3,373 additions and 115 deletions.
5 changes: 5 additions & 0 deletions .project
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
Expand Down
132 changes: 67 additions & 65 deletions src/main/java/org/neuroml/visualiser/core/NeuroMLModelInterpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import org.geppetto.core.visualisation.model.Reference;
import org.geppetto.core.visualisation.model.Scene;
import org.geppetto.core.visualisation.model.Sphere;
import org.neuroml.export.info.InfoTreeCreator;
import org.neuroml.export.info.model.InfoNode;
import org.neuroml.export.info.model.PlotNode;
import org.neuroml.model.Cell;
import org.neuroml.model.ChannelDensity;
import org.neuroml.model.Include;
Expand All @@ -41,24 +44,24 @@
import org.neuroml.model.util.UnitsFormatterUtils;

/**
* This class is used to create a Geppetto compatible Scene from a NeuroML document
*
* @author matteocantarelli
*
*/
public class NeuroMLModelInterpreter
{

private NeuroMLConverter _neuromlConverter = null;
private static final String GROUP_PROPERTY = "group";
// The number of attempts when there's a network error
private static final int MAX_ATTEMPTS = 3;

// neuroml hardcoded concepts
private static final String DENDRITE_GROUP = "dendrite_group";
private static final String AXON_GROUP = "axon_group";
private static final String SOMA_GROUP = "soma_group";
private static final String GROUP_PROPERTY = "group";

/**
*
*/
public NeuroMLModelInterpreter()
public NeuroMLModelInterpreter() throws RuntimeException
{
super();
try
Expand All @@ -67,15 +70,15 @@ public NeuroMLModelInterpreter()
}
catch(Exception e)
{
e.printStackTrace();
throw new RuntimeException(e);
}
}

/**
* @param neuromls
* @return
*/
public Scene getSceneFromNeuroML(List<URL> neuromlURLs)
public Scene getSceneFromNeuroML(List<URL> neuromlURLs) throws RuntimeException
{
Scene scene = new Scene();
for(URL url : neuromlURLs)
Expand All @@ -86,15 +89,67 @@ public Scene getSceneFromNeuroML(List<URL> neuromlURLs)
neuroml = _neuromlConverter.urlToNeuroML(url);
scene.getEntities().addAll(getEntitiesFromMorphologies(neuroml)); // if there's any morphology
scene.getEntities().addAll(getEntitiesFromNetwork(neuroml, url)); // if a population is described -> network
if(scene.getEntities().isEmpty())
{
//The ultimate weapon: if there's no cell or network morphology use the info module to pull off just metadata
scene.getEntities().addAll(getEntitiesFromInfoModule(neuroml, url));
}
}
catch(Exception e)
{
e.printStackTrace();
throw new RuntimeException(e);
}
}
return scene;
}

/**
* @param neuroml
* @param url
* @return
*/
private List<Entity> getEntitiesFromInfoModule(NeuroMLDocument neuroml, URL url)
{
List<Entity> entities = new ArrayList<Entity>();
InfoNode infoRoot = InfoTreeCreator.createInfoTree(neuroml);
Entity e = new Entity();
Metadata m = processNode(infoRoot);
e.setId(url.getFile());
e.setMetadata(m);
entities.add(e);
return entities;
}

/**
* Process an InfoNode and returns a MetaData element which will be displayed by the frontend
*
* @param infoRoot
* @param entities
*/
private Metadata processNode(InfoNode infoRoot)
{
Metadata m = new Metadata();
for(String key : infoRoot.getProperties().keySet())
{
// Matteo: a tree model should implement a visitor pattern to avoid this
// ugly chain of ifs..
if(infoRoot.get(key) instanceof String)
{
m.setAdditionalProperties(key, infoRoot.get(key));
}
else if(infoRoot.get(key) instanceof InfoNode)
{
m.setAdditionalProperties(key, processNode((InfoNode) infoRoot.get(key)));
}
else if(infoRoot.get(key) instanceof PlotNode)
{
//$PLOT$ will inform the frontend that the object contained is a plot
m.setAdditionalProperties("$PLOT$"+key, infoRoot.get(key));
}
}
return m;
}

/**
* @param neuroml
* @return
Expand Down Expand Up @@ -130,8 +185,6 @@ public List<Entity> getEntitiesFromMorphologies(NeuroMLDocument neuroml)
return entities;
}

private static final int MAX_ATTEMPTS = 3;

/**
* @param neuroml
* @param scene
Expand Down Expand Up @@ -162,7 +215,6 @@ private Collection<Entity> getEntitiesFromNetwork(NeuroMLDocument neuroml, URL u
{
attemptConnection = false;
attempts++;

neuromlComponent = _neuromlConverter.urlToNeuroML(componentURL);
}
catch(UnmarshalException e)
Expand Down Expand Up @@ -208,7 +260,6 @@ private Collection<Entity> getEntitiesFromNetwork(NeuroMLDocument neuroml, URL u

for(int i = 0; i < size; i++)
{
// FIXME the position of the population within the network needs to be specified in neuroml
List<Entity> localEntities = getEntitiesFromMorphologies(neuromlComponent);
for(Entity e : localEntities)
{
Expand All @@ -218,8 +269,8 @@ private Collection<Entity> getEntitiesFromNetwork(NeuroMLDocument neuroml, URL u
}
}

// FIXME what's the purpose of the id here?
String id = p.getId();
// Matteo: what's the purpose of the id here?
// String id = p.getId();

}
for(SynapticConnection c : n.getSynapticConnection())
Expand Down Expand Up @@ -334,7 +385,7 @@ private void augmentWithMetaData(Entity entity, Cell c)
}
catch(NullPointerException ex)
{

// Matteo: check why this is being silenced
}
}

Expand All @@ -357,9 +408,6 @@ private List<Entity> getEntitiesFromMorphologyBySegmentGroup(Morphology morpholo
Entity allSegments = getEntityFromListOfSegments(morphology.getSegment());
List<Entity> entities = new ArrayList<Entity>();
Map<String, List<AGeometry>> segmentGeometries = new HashMap<String, List<AGeometry>>();
SegmentGroup somaGroup = null;
SegmentGroup axonGroup = null;
SegmentGroup dendriteGroup = null;

if(morphology.getSegmentGroup().isEmpty())
{
Expand All @@ -371,20 +419,6 @@ private List<Entity> getEntitiesFromMorphologyBySegmentGroup(Morphology morpholo
Map<String, List<String>> subgroupsMap = new HashMap<String, List<String>>();
for(SegmentGroup sg : morphology.getSegmentGroup())
{
// three hardcoded groups :(
if(sg.getId().equals(SOMA_GROUP))
{
somaGroup = sg;
}
else if(sg.getId().equals(AXON_GROUP))
{
axonGroup = sg;
}
else if(sg.getId().equals(DENDRITE_GROUP))
{
dendriteGroup = sg;
}

for(Include include : sg.getInclude())
{
// the map is <containedGroup,containerGroup>
Expand Down Expand Up @@ -470,38 +504,6 @@ private String getGroupId(String cellId, String segmentGroupId)
return cellId + " " + segmentGroupId;
}

/**
* @param somaGroup
* @param segmentGeometries
*/
private Entity createEntityForMacroGroup(SegmentGroup macroGroup, Map<String, List<AGeometry>> segmentGeometries, List<AGeometry> allSegments)
{
Entity entity = new Entity();
entity.setAdditionalProperties(GROUP_PROPERTY, macroGroup.getId());
for(Include i : macroGroup.getInclude())
{
if(segmentGeometries.containsKey(i.getSegmentGroup()))
{
entity.getGeometries().addAll(segmentGeometries.get(i.getSegmentGroup()));
segmentGeometries.remove(i.getSegmentGroup());
}
}
for(Member m : macroGroup.getMember())
{
for(AGeometry g : allSegments)
{
if(g.getId().equals(m.getSegment().toString()))
{
entity.getGeometries().add(g);
allSegments.remove(g);
break;
}
}
}
segmentGeometries.remove(macroGroup.getId());
return entity;
}

/**
* @param sg
* @param allSegments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,58 +36,53 @@ public class Get3DSceneServlet extends HttpServlet
*/
public Get3DSceneServlet()
{
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{

try
{

URL url = new URL(request.getParameter("url"));

//find all URLs if it's a folder
// find all URLs if it's a folder
List<URL> neuroMLfiles = new ArrayList<URL>();
if (url.getFile().endsWith("nml"))
if(url.getFile().endsWith("nml"))
{
neuroMLfiles.add(url);
}
else
{

if (url.getProtocol().equals("http"))
if(url.getProtocol().equals("http"))
{
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = null;

while ((line = br.readLine()) != null)
while((line = br.readLine()) != null)
{
addURLIfPresent(neuroMLfiles, line, request.getParameter("url"));
}

}
else if (url.getProtocol().equals("file"))
else if(url.getProtocol().equals("file"))
{
File f = new File(url.getPath());

if (f != null && f.isDirectory())
if(f != null && f.isDirectory())
{

List<File> files = Arrays.asList(f.listFiles(new FilenameFilter()
{
public boolean accept(File dir, String filename)
{
return filename.endsWith(".nml");
}
}));
for (File file : files)
for(File file : files)
{
neuroMLfiles.add(new URL("file://" + file.getAbsolutePath()));
}
Expand All @@ -98,22 +93,25 @@ public boolean accept(File dir, String filename)
NeuroMLModelInterpreter morphologyConverter = new NeuroMLModelInterpreter();
Scene scene = morphologyConverter.getSceneFromNeuroML(neuroMLfiles);
ObjectMapper mapper = new ObjectMapper();

response.setCharacterEncoding("UTF-8");
response.setContentType("application/json");
mapper.writeValue(response.getOutputStream(), scene);

}
catch (Exception e)
catch(Exception e)
{
e.printStackTrace();
}
}

/**
* @param neuroMLfiles
* @param line
* @param folderURL
*/
private void addURLIfPresent(List<URL> neuroMLfiles, String line, String folderURL)
{
String pattern = ".nml?rev";
if (line.contains(pattern))
if(line.contains(pattern))
{
int startURLIndex = line.indexOf("\"");
int endURLIndex = line.indexOf("\"", startURLIndex + 1);
Expand All @@ -124,10 +122,9 @@ private void addURLIfPresent(List<URL> neuroMLfiles, String line, String folderU
{
neuroMLfiles.add(new URL(folderURL + fileName));
}
catch (MalformedURLException e)
catch(MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
}
}

Expand Down
Loading

0 comments on commit 8338672

Please sign in to comment.