Created
June 11, 2009 14:15
-
-
Save mdjones/127914 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
package org.zertz.genesearch.web.ajax; | |
import org.apache.commons.logging.Log; | |
import org.apache.commons.logging.LogFactory; | |
import org.springframework.web.servlet.mvc.SimpleFormController; | |
import org.springframework.web.servlet.ModelAndView; | |
import org.springframework.web.servlet.view.AbstractView; | |
import org.springframework.core.io.Resource; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import java.util.*; | |
import java.io.*; | |
import net.sf.json.JSONObject; | |
import net.sf.json.JSONArray; | |
/* | |
* Copyright | |
* | |
* Licensed under the | |
*/ | |
/** | |
* <p/> | |
* TODO: Add Description | |
* </p> | |
* Date: Dec 7, 2007 | |
* | |
* @author Michael Jones. | |
*/ | |
public class GeneSearch extends SimpleFormController { | |
protected final Log logger = LogFactory.getLog(getClass()); | |
private Resource probeAnnotationResource; | |
private Map<String, ProbeSetAnnotation> probe2annot = new HashMap<String, ProbeSetAnnotation>(); | |
public void init() throws IOException { | |
BufferedReader in = new BufferedReader(new FileReader(probeAnnotationResource.getFile())); | |
in.readLine(); //Remove header | |
String line; | |
while((line = in.readLine()) != null){ | |
String[] toks = line.split("\t"); | |
String probe = toks[0]; | |
String syn = toks[1]; | |
String id = toks[2]; | |
String desc = toks[3]; | |
String name = toks[4]; | |
ProbeSetAnnotation annot = new ProbeSetAnnotation(probe, name, Integer.parseInt(id), syn, desc); | |
probe2annot.put(probe, annot); | |
} | |
} | |
@Override | |
protected ModelAndView onSubmit(Object command) { | |
return new ModelAndView(new JSONGeneSearchView((GeneSearchData)command)); | |
} | |
@Override | |
protected boolean isFormSubmission(HttpServletRequest request){ | |
return true; | |
} | |
class JSONGeneSearchView extends AbstractView { | |
private GeneSearchData cmd; | |
JSONGeneSearchView(GeneSearchData cmd) { | |
this.cmd = cmd; | |
} | |
protected void renderMergedOutputModel(Map map, HttpServletRequest httpServletRequest, | |
HttpServletResponse response) throws Exception { | |
logger.debug("Building JSON"); | |
response.setHeader("Content-Disposition", "attachment; filename=test.json"); | |
response.setContentType("application/json; charset=utf-8"); | |
final PrintWriter writer = response.getWriter(); | |
List<String> probes = new ArrayList<String>(Arrays.asList(cmd.getProbes().split("\\s+"))); | |
JSONArray array = new JSONArray(); | |
Set<ProbeSetAnnotation> annotations = getProbeSetAnnotations(probes); | |
logger.debug("Got annotations: " + annotations.size()); | |
for(ProbeSetAnnotation annot : annotations){ | |
logger.debug("Building JSON for " + annot.probeset); | |
JSONObject item = new JSONObject(); | |
item.put("type", "probesets"); | |
item.put("label", annot.getProbeset()); | |
item.put("name", annot.getGeneName()); | |
item.put("entrez_gene_id", annot.getGeneID()); | |
item.put("synonyms", annot.getGeneSynonyms()); | |
item.put("description", annot.getDescription()); | |
array.put(item); | |
} | |
JSONObject jsonObj = new JSONObject(); | |
jsonObj.put("items", array); | |
jsonObj.write(writer); | |
writer.close(); | |
logger.debug("Done writing JSON"); | |
} | |
} | |
private Set<ProbeSetAnnotation> getProbeSetAnnotations(List<String> probes) { | |
logger.debug("Getting probe annot : " + probes); | |
Set<ProbeSetAnnotation> annots = new HashSet<ProbeSetAnnotation>(probes.size()); | |
for(String probe : probes){ | |
if(probe2annot.containsKey(probe)) | |
annots.add(probe2annot.get(probe)); | |
} | |
return annots; | |
} | |
public Resource getProbeAnnotationResource() { | |
return probeAnnotationResource; | |
} | |
public void setProbeAnnotationResource(Resource probeAnnotationResource) { | |
this.probeAnnotationResource = probeAnnotationResource; | |
} | |
class ProbeSetAnnotation { | |
private String probeset; | |
private String geneName; | |
private int geneID; | |
private String geneSynonyms; | |
private String description; | |
ProbeSetAnnotation(String probeset, String geneName, int geneID, String geneSynonyms, String description) { | |
this.probeset = probeset; | |
this.geneName = geneName; | |
this.geneID = geneID; | |
this.geneSynonyms = geneSynonyms; | |
this.description = description; | |
} | |
public String getProbeset() { | |
return probeset; | |
} | |
public void setProbeset(String probeset) { | |
this.probeset = probeset; | |
} | |
public String getGeneName() { | |
return geneName; | |
} | |
public void setGeneName(String geneName) { | |
this.geneName = geneName; | |
} | |
public int getGeneID() { | |
return geneID; | |
} | |
public void setGeneID(int geneID) { | |
this.geneID = geneID; | |
} | |
public String getGeneSynonyms() { | |
return geneSynonyms; | |
} | |
public void setGeneSynonyms(String geneSynonyms) { | |
this.geneSynonyms = geneSynonyms; | |
} | |
public String getDescription() { | |
return description; | |
} | |
public void setDescription(String description) { | |
this.description = description; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment