Skip to content

Commit

Permalink
Reverted accidental commit
Browse files Browse the repository at this point in the history
  • Loading branch information
GuilhemSempere committed Oct 7, 2024
1 parent df37aac commit a964d6b
Showing 1 changed file with 9 additions and 225 deletions.
234 changes: 9 additions & 225 deletions src/main/java/fr/cirad/web/controller/gigwa/GigwaRestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand All @@ -33,7 +32,6 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.file.Paths;
import java.net.UnknownHostException;
import java.text.Normalizer;
import java.util.ArrayList;
Expand All @@ -58,7 +56,6 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import java.util.zip.GZIPInputStream;

import javax.ejb.ObjectNotFoundException;
import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -107,7 +104,6 @@
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ModelAndView;
Expand All @@ -126,7 +122,6 @@
import com.sun.jersey.api.client.ClientResponse;

import fr.cirad.io.brapi.BrapiService;
import fr.cirad.mgdb.annotation.SnpEffAnnotationService;
import fr.cirad.manager.IModuleManager;
import fr.cirad.manager.ImportProcess;
import fr.cirad.mgdb.exporting.IExportHandler;
Expand Down Expand Up @@ -188,7 +183,6 @@
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.swagger.annotations.Authorization;
import static java.lang.Integer.parseInt;
import springfox.documentation.annotations.ApiIgnore;

/**
Expand Down Expand Up @@ -277,9 +271,6 @@ public class GigwaRestController extends ControllerInterface {
static public final String DELETE_QUERY_URL = "/deleteQuery";
static public final String VARIANTS_BY_IDS = "/variants/byIds";
static public final String VARIANTS_LOOKUP = "/variants/lookup";
static public final String SNPEFF_ANNOTATION_PATH = "/snpEff/annotate";
static public final String SNPEFF_GENOME_LIST = "/snpEff/genomes";
static public final String SNPEFF_INSTALL_GENOME = "/snpEff/install";
static public final String GENES_LOOKUP = "/genes/lookup";
static public final String GALAXY_HISTORY_PUSH = "/pushToGalaxyHistory";
static public final String DISTINCT_INDIVIDUAL_METADATA = "/distinctIndividualMetadata";
Expand Down Expand Up @@ -940,14 +931,14 @@ public void getSelectionIgvData(HttpServletRequest request, HttpServletResponse
for (GenotypingSample gs : samples)
sampleIdToIndividualMap.put(gs.getId(), gs.getIndividual());

Collection<BasicDBList> variantRunDataQueries = varQueryWrapper.getVariantRunDataQueries();
Map<String, Collection<String>> individualsByPop = new HashMap<>();
Map<String, HashMap<String, Float>> annotationFieldThresholdsByPop = new HashMap<>();
List<List<String>> callsetIds = gr.getAllCallSetIds();
for (int i = 0; i < callsetIds.size(); i++) {
individualsByPop.put(gr.getGroupName(i), callsetIds.get(i).isEmpty() ? MgdbDao.getProjectIndividuals(info[0], projId) /* no selection means all selected */ : callsetIds.get(i).stream().map(csi -> csi.substring(1 + csi.lastIndexOf(Helper.ID_SEPARATOR))).collect(Collectors.toSet()));
annotationFieldThresholdsByPop.put(gr.getGroupName(i), gr.getAnnotationFieldThresholds(i));
}
Collection<BasicDBList> variantRunDataQueries = varQueryWrapper.getVariantRunDataQueries();
Map<String, Collection<String>> individualsByPop = new HashMap<>();
Map<String, HashMap<String, Float>> annotationFieldThresholdsByPop = new HashMap<>();
List<List<String>> callsetIds = gr.getAllCallSetIds();
for (int i = 0; i < callsetIds.size(); i++) {
individualsByPop.put(gr.getGroupName(i), callsetIds.get(i).isEmpty() ? MgdbDao.getProjectIndividuals(info[0], projId) /* no selection means all selected */ : callsetIds.get(i).stream().map(csi -> csi.substring(1 + csi.lastIndexOf(Helper.ID_SEPARATOR))).collect(Collectors.toSet()));
annotationFieldThresholdsByPop.put(gr.getGroupName(i), gr.getAnnotationFieldThresholds(i));
}

HapMapExportHandler heh = (HapMapExportHandler) AbstractMarkerOrientedExportHandler.getMarkerOrientedExportHandlers().get("HAPMAP");
heh.writeGenotypeFile(true, true, true, true, os, info[0], mongoTemplate.findOne(new Query(Criteria.where("_id").is(Assembly.getThreadBoundAssembly())), Assembly.class), individualsByPop, sampleIdToIndividualMap, annotationFieldThresholdsByPop, progress, fWorkingOnTempColl ? tempVarColl.getNamespace().getCollectionName() : null, !variantRunDataQueries.isEmpty() ? variantRunDataQueries.iterator().next() : new BasicDBList(), markerCount, null, samples);
Expand Down Expand Up @@ -1226,11 +1217,10 @@ public void pushFileToGalaxyHistory(HttpServletRequest request, HttpServletRespo
if (resp.getStatus() >= HttpServletResponse.SC_TEMPORARY_REDIRECT + 3) // "Too many Redirects" or 4xx / 5xx error
throw new Exception("Remote error - " + resp.toString());


final Map<String, Object> responseObjects = resp.getEntity(Map.class);
List<Map<String, Object>> outputs = (List<Map<String, Object>>) responseObjects.get("outputs");
for (Map<String, Object> output : outputs)
System.err.println(output.get("id"));
LOG.debug("Dataset sent to " + gi.getGalaxyUrl() + " -> " + output.get("id"));

// HistoryDatasetAssociation hda = read(resp, com.github.jmchilton.blend4j.galaxy.HistoriesClientImpl.HDA_TYPE_REFERENCE);
// hda.getOutputs().iterator().next().getid()
Expand Down Expand Up @@ -1928,7 +1918,6 @@ else if (filesByExtension.containsKey(fileExtension))
else
nTotalImportSize += fileSize * (fileExtension.toLowerCase().equals("gz") ? 20 : 1);
}

}
catch (Exception e)
{
Expand Down Expand Up @@ -2651,211 +2640,6 @@ public LinkedHashMap<String, Set<String>> distinctIndividualMetadata(HttpServlet
return MgdbDao.getInstance().distinctIndividualMetadata(module, sUserName, projID, (Collection<String>) reqBody.get("individuals"));
}

@ApiIgnore
@ApiOperation(authorizations = {@Authorization(value = "AuthorizationToken")}, value = SNPEFF_ANNOTATION_PATH, notes = "Annotates variants with snpEff")
@ApiResponses(value = {@ApiResponse(code = 200, message = "Success")})
@RequestMapping(value = BASE_URL + SNPEFF_ANNOTATION_PATH, method = RequestMethod.POST)
public String snpEffAnnotation(HttpServletRequest request, HttpServletResponse response,
@RequestParam("module") final String sModule,
@RequestParam("project") final String sProject,
@RequestParam("run") final String sRun,
@RequestParam("genome") final String genomeName) throws Exception {
String token = tokenManager.readToken(request);
if (token.length() == 0)
return null;

String configFile = appConfig.get("snpEffConfigFile");
String dataPath = appConfig.get("snpEffDataRepository");
if (configFile == null || dataPath == null)
throw new Exception("Online annotation is not available");

ProgressIndicator progress = new ProgressIndicator(token, new String[] { "Checking input" });
ProgressIndicator.registerProgressIndicator(progress);
// progress.setPercentageEnabled(false);

if (!SnpEffAnnotationService.getAvailableGenomes(dataPath).contains(genomeName)) {
progress.setError("Unknown genome, you must install it first");
return null;
}

MongoTemplate mongoTemplate = MongoTemplateManager.get(sModule);
final GenotypingProject project = mongoTemplate.findOne(new Query(Criteria.where(GenotypingProject.FIELDNAME_NAME).is(sProject)), GenotypingProject.class);
if (tokenManager.canUserWriteToProject(token, sModule, project.getId()))
try {
SnpEffAnnotationService.annotateRun(configFile, dataPath, sModule, project.getId(), sRun, genomeName, progress);
progress.markAsComplete();
}
catch (Exception e) {
progress.setError("Error annotating dataset: " + e.getMessage());
LOG.error("Error annotating dataset", e);
}
else {
LOG.error("NOT AUTHENTICATED");
}

return null;
}

@ApiIgnore
@RequestMapping(value = BASE_URL + SNPEFF_GENOME_LIST, method = RequestMethod.GET)
public Map<String, Object> snpEffGenomeList(HttpServletRequest request, HttpServletResponse response) {
HashMap<String, Object> result = new HashMap<>();

String configFile = appConfig.get("snpEffConfigFile");
String dataPath = appConfig.get("snpEffDataRepository");

result.put("availableGenomes", SnpEffAnnotationService.getAvailableGenomes(dataPath));
result.put("downloadableGenomes", SnpEffAnnotationService.getDownloadableGenomes(configFile, dataPath));

return result;
}

@ApiIgnore
@RequestMapping(value = BASE_URL + SNPEFF_INSTALL_GENOME, method = RequestMethod.POST)
public Map<String, Object> snpEffInstallGenome(MultipartHttpServletRequest request, HttpServletResponse response,
@RequestParam(value="genomeName", required=false) final String genomeName,
@RequestParam(value="genomeURL", required=false) final URL genomeURL,
@RequestParam(value="newGenomeID", required=false) String newGenomeID,
@RequestParam(value="newGenomeName", required=false) String newGenomeName) throws Exception {

String token = tokenManager.readToken(request);
if (token.length() == 0)
return null;

String configFile = appConfig.get("snpEffConfigFile");
String dataPath = appConfig.get("snpEffDataRepository");
if (configFile == null || dataPath == null)
throw new Exception("Online annotation is not available");

ProgressIndicator progress = new ProgressIndicator(token, new String[] { "Checking input" });
ProgressIndicator.registerProgressIndicator(progress);

HashMap<String, Object> result = new HashMap<>();
result.put("log", null);
result.put("success", false);

Map<String, MultipartFile> fileMap = request.getFileMap();

if (genomeName != null) {
if (!SnpEffAnnotationService.getAvailableGenomes(dataPath).contains(genomeName)) {
SnpEffAnnotationService.downloadGenome(configFile, dataPath, genomeName, progress);
result.put("success", true);
}
} else if (genomeURL != null) {
SnpEffAnnotationService.downloadGenome(configFile, dataPath, genomeURL, progress);
result.put("success", true);
} else if (fileMap != null && newGenomeID != null) {
if (newGenomeName == null)
newGenomeName = newGenomeID;
File fastaFile = File.createTempFile("snpEffFasta-", "");
File referenceFile = File.createTempFile("snpEffRef-", "");
File cdsFile = File.createTempFile("snpEffCDS-", "");
File proteinFile = File.createTempFile("snpEffProtein-", "");
String referenceFormat = null;
boolean fastaFound = false, cdsFound = false, proteinFound = false;

for (MultipartFile file : fileMap.values()) {
// Some browsers (or malicious users) might supply additional path components in surplus of the raw file name
String fileName = Paths.get(file.getOriginalFilename()).getFileName().toString();

boolean gzipped = false;
int extensionPosition = fileName.lastIndexOf('.');
String extension = fileName.substring(extensionPosition + 1).toLowerCase();
String baseName = fileName.substring(0, extensionPosition);

if (extension.equals("gz")) {
gzipped = true;
extensionPosition = baseName.lastIndexOf('.');
extension = baseName.substring(extensionPosition + 1).toLowerCase();
baseName = baseName.substring(0, extensionPosition);
}

if (extension.equals("fa") || extension.equals("fas") || extension.equals("fasta")) {
if (baseName.toLowerCase().equals("cds")) {
transferSnpEffImport(file, cdsFile, gzipped, progress);
cdsFound = true;
} else if (baseName.toLowerCase().equals("protein")) {
transferSnpEffImport(file, proteinFile, gzipped, progress);
proteinFound = true;
} else {
transferSnpEffImport(file, fastaFile, gzipped, progress);
fastaFound = true;
}
} else if (extension.equals("gtf")) {
transferSnpEffImport(file, referenceFile, gzipped, progress);
referenceFormat = "gtf22";
} else if (extension.equals("gff") || extension.equals("gff3")) {
transferSnpEffImport(file, referenceFile, gzipped, progress);
referenceFormat = "gff3";
} else if (extension.equals("gff2")) {
transferSnpEffImport(file, referenceFile, gzipped, progress);
referenceFormat = "gff2";
} else if (extension.equals("genbank") || extension.equals("gbk")) {
transferSnpEffImport(file, referenceFile, gzipped, progress);
referenceFormat = "genbank";
} else if (extension.equals("refseq")) {
transferSnpEffImport(file, referenceFile, gzipped, progress);
referenceFormat = "refSeq";
} else if (extension.equals("embl")) {
transferSnpEffImport(file, referenceFile, gzipped, progress);
referenceFormat = "embl";
} else if (extension.equals("kg") || extension.equals("knowngenes")) {
transferSnpEffImport(file, referenceFile, gzipped, progress);
referenceFormat = "knowngenes";
} else {
progress.setError("Unsupported file type : " + fileName);
return result;
}
}

if (referenceFormat == null) {
progress.setError("No reference file found");
return result;
} else if (!fastaFound) {
progress.setError("No sequence file found");
return result;
}

LOG.debug("FASTA found, reference format is " + referenceFormat);

String log = SnpEffAnnotationService.importGenome(newGenomeID, newGenomeName, fastaFile, referenceFile, (cdsFound ? cdsFile : null), (proteinFound ? proteinFile : null), referenceFormat, configFile, dataPath, progress);
result.put("log", log);
} else {
progress.setError("No genome specified");
return result;
}
// TODO : Upload : https://pcingola.github.io/SnpEff/se_buildingdb/

if (progress.getError() != null)
return result;

result.put("success", true);
progress.markAsComplete();
return result;
}

// FIXME : Is this compatible with BGZip-compressed files ?
private void transferSnpEffImport(MultipartFile inputFile, File outputFile, boolean gzipped, ProgressIndicator progress) throws FileNotFoundException, IOException {
String fileName = Paths.get(inputFile.getOriginalFilename()).getFileName().toString();
if (gzipped) {
progress.addStep("Decompressing " + fileName);
progress.moveToNextStep();

GZIPInputStream input = new GZIPInputStream(inputFile.getInputStream());
FileOutputStream output = new FileOutputStream(outputFile);

byte[] buffer = new byte[65536];
int readLength;
while ((readLength = input.read(buffer)) > 0)
output.write(buffer, 0, readLength);

input.close();
output.close();
} else {
inputFile.transferTo(outputFile);
}
}

@ApiIgnore
@RequestMapping(value = BASE_URL + FILTER_INDIVIDUAL_METADATA + "/{module}", method = RequestMethod.POST, produces = "application/json")
public Collection<Individual> filterIndividualMetadata(HttpServletRequest request, HttpServletResponse response, @PathVariable String module, @RequestBody LinkedHashMap<String, Set<String>> filters, @RequestParam(required = false) final Integer projID) throws IOException {
Expand Down

0 comments on commit a964d6b

Please sign in to comment.