Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: format code with Autopep8 and Google Java Format #468

Merged
merged 2 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/main/java/DNAnalyzer/adapter/StatusController.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package DNAnalyzer.adapter;

import java.util.Map;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;

@RestController
@RequestMapping("/api")
@CrossOrigin(origins = "*")
public class StatusController {

@GetMapping("/status")
public ResponseEntity<?> getStatus() {
return ResponseEntity.ok(Map.of(

@GetMapping("/status")
public ResponseEntity<?> getStatus() {
return ResponseEntity.ok(
Map.of(
"status", "online",
"version", "1.0.0"
));
}
}
"version", "1.0.0"));
}
}
198 changes: 98 additions & 100 deletions src/main/java/DNAnalyzer/web/AnalyzerController.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package DNAnalyzer.web;

import DNAnalyzer.core.DNAAnalysis;
import DNAnalyzer.utils.core.DNATools;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;

import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
Expand All @@ -14,113 +15,110 @@
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import DNAnalyzer.core.DNAAnalysis;
import DNAnalyzer.utils.core.DNATools;

@RestController
@RequestMapping("/api")
@CrossOrigin(origins = "*")
public class AnalyzerController {

@PostMapping(value = "/analyze", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<String> analyzeDNA(
@RequestParam("dnaFile") MultipartFile dnaFile,
@RequestParam(value = "amino", defaultValue = "M") String amino,
@RequestParam(value = "minCount", defaultValue = "1") int minCount,
@RequestParam(value = "maxCount", defaultValue = "100") int maxCount,
@RequestParam(value = "reverse", defaultValue = "false") boolean reverse,
@RequestParam(value = "rcomplement", defaultValue = "false") boolean rcomplement,
@RequestParam(value = "codons", defaultValue = "false") boolean codons,
@RequestParam(value = "coverage", defaultValue = "false") boolean coverage,
@RequestParam(value = "longest", defaultValue = "false") boolean longest,
@RequestParam(value = "format", defaultValue = "text") String format) {

try {
// Create temporary file
Path tempFile = Files.createTempFile("dna-", ".fa");
dnaFile.transferTo(tempFile.toFile());

// Capture console output
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
PrintStream old = System.out;
System.setOut(ps);

try {
// Read DNA sequence
String dnaSequence = new String(Files.readAllBytes(tempFile));

// Run analysis
DNAAnalysis analysis = new DNAAnalysis(new DNATools(dnaSequence), null, amino);

if (reverse) {
analysis = analysis.reverseDna();
}
if (rcomplement) {
analysis = analysis.reverseComplement();
}

if (codons) {
analysis.outPutCodons(minCount, maxCount, System.out);
}
if (coverage) {
analysis.printHighCoverageRegions(System.out);
}
if (longest) {
analysis.printLongestProtein(System.out);
}

// Get output
String output = baos.toString();

// Format output
if ("json".equals(format)) {
output = formatAsJson(output);
} else if ("csv".equals(format)) {
output = formatAsCsv(output);
}

return ResponseEntity.ok(output);

} finally {
// Restore console output and cleanup
System.setOut(old);
Files.deleteIfExists(tempFile);
}

} catch (Exception e) {
return ResponseEntity.badRequest().body("Error analyzing DNA: " + e.getMessage());
@PostMapping(value = "/analyze", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<String> analyzeDNA(
@RequestParam("dnaFile") MultipartFile dnaFile,
@RequestParam(value = "amino", defaultValue = "M") String amino,
@RequestParam(value = "minCount", defaultValue = "1") int minCount,
@RequestParam(value = "maxCount", defaultValue = "100") int maxCount,
@RequestParam(value = "reverse", defaultValue = "false") boolean reverse,
@RequestParam(value = "rcomplement", defaultValue = "false") boolean rcomplement,
@RequestParam(value = "codons", defaultValue = "false") boolean codons,
@RequestParam(value = "coverage", defaultValue = "false") boolean coverage,
@RequestParam(value = "longest", defaultValue = "false") boolean longest,
@RequestParam(value = "format", defaultValue = "text") String format) {

try {
// Create temporary file
Path tempFile = Files.createTempFile("dna-", ".fa");
dnaFile.transferTo(tempFile.toFile());

// Capture console output
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
PrintStream old = System.out;
System.setOut(ps);

try {
// Read DNA sequence
String dnaSequence = new String(Files.readAllBytes(tempFile));

// Run analysis
DNAAnalysis analysis = new DNAAnalysis(new DNATools(dnaSequence), null, amino);

if (reverse) {
analysis = analysis.reverseDna();
}
if (rcomplement) {
analysis = analysis.reverseComplement();
}
}

private String formatAsJson(String output) {
// Convert output to JSON format
StringBuilder json = new StringBuilder();
json.append("{\"results\": [");

String[] lines = output.split("\n");
for (int i = 0; i < lines.length; i++) {
json.append("\"").append(lines[i].replace("\"", "\\\"")).append("\"");
if (i < lines.length - 1) {
json.append(",");
}
if (codons) {
analysis.outPutCodons(minCount, maxCount, System.out);
}
if (coverage) {
analysis.printHighCoverageRegions(System.out);
}
if (longest) {
analysis.printLongestProtein(System.out);
}

json.append("]}");
return json.toString();
}

private String formatAsCsv(String output) {
// Convert output to CSV format
StringBuilder csv = new StringBuilder();

String[] lines = output.split("\n");
for (String line : lines) {
// Replace any commas in the data with semicolons
line = line.replace(",", ";");
csv.append(line).append(",\n");
// Get output
String output = baos.toString();

// Format output
if ("json".equals(format)) {
output = formatAsJson(output);
} else if ("csv".equals(format)) {
output = formatAsCsv(output);
}

return csv.toString();

return ResponseEntity.ok(output);

} finally {
// Restore console output and cleanup
System.setOut(old);
Files.deleteIfExists(tempFile);
}

} catch (Exception e) {
return ResponseEntity.badRequest().body("Error analyzing DNA: " + e.getMessage());

Check warning

Code scanning / CodeQL

Information exposure through an error message Medium

Error information
can be exposed to an external user.
}
}

private String formatAsJson(String output) {
// Convert output to JSON format
StringBuilder json = new StringBuilder();
json.append("{\"results\": [");

String[] lines = output.split("\n");
for (int i = 0; i < lines.length; i++) {
json.append("\"").append(lines[i].replace("\"", "\\\"")).append("\"");
if (i < lines.length - 1) {
json.append(",");
}
}
}

json.append("]}");
return json.toString();
}

private String formatAsCsv(String output) {
// Convert output to CSV format
StringBuilder csv = new StringBuilder();

String[] lines = output.split("\n");
for (String line : lines) {
// Replace any commas in the data with semicolons
line = line.replace(",", ";");
csv.append(line).append(",\n");
}

return csv.toString();
}
}
33 changes: 17 additions & 16 deletions src/test/java/DNAnalyzer/core/ApiKeyServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
package DNAnalyzer.core;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

class ApiKeyServiceTest {

private ApiKeyService apiKeyService = new ApiKeyService();
private ApiKeyService apiKeyService = new ApiKeyService();

@Test
void shouldSetAndGetApiKey() {
String newKey = "test-api-key";
apiKeyService.setApiKey(newKey);
assertEquals(newKey, apiKeyService.getApiKey());
}
@Test
void shouldSetAndGetApiKey() {
String newKey = "test-api-key";
apiKeyService.setApiKey(newKey);
assertEquals(newKey, apiKeyService.getApiKey());
}

@Test
void shouldReturnFalseWhenNoKeySet() {
assertFalse(apiKeyService.hasApiKey());
}
@Test
void shouldReturnFalseWhenNoKeySet() {
assertFalse(apiKeyService.hasApiKey());
}

@Test
void shouldReturnTrueWhenKeySet() {
apiKeyService.setApiKey("test-key");
assertTrue(apiKeyService.hasApiKey());
}
@Test
void shouldReturnTrueWhenKeySet() {
apiKeyService.setApiKey("test-key");
assertTrue(apiKeyService.hasApiKey());
}
}
Loading