Skip to content

Commit

Permalink
app: fix sonar issues, #TASK-4768, #TASK-4761
Browse files Browse the repository at this point in the history
  • Loading branch information
jtarraga committed Jul 18, 2023
1 parent a28c81f commit fa814a9
Showing 1 changed file with 12 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ public ExportCommandExecutor(AdminCliOptionsParser.ExportCommandOptions exportCo
* @throws CellBaseException CellBase exception
*/
public void execute() throws CellBaseException {
checkDataRelease();

logger.info("Exporting from data release {}", dataRelease);
this.managerFactory = new CellBaseManagerFactory(configuration);

checkDataRelease();

if (exportCommandOptions.data != null) {
// Get genes
List<String> geneNames = Arrays.asList(exportCommandOptions.gene.split(","));
Expand Down Expand Up @@ -138,8 +138,8 @@ public void execute() throws CellBaseException {
regions.addAll(Region.parseRegions(exportCommandOptions.region));
}

logger.info("{} regions: {}", regions.size(), StringUtils.join(regions.stream().map(r -> r.toString())
.collect(Collectors.toList()), ","));
String strRegions = StringUtils.join(regions.stream().map(Object::toString).collect(Collectors.toList()), ",");
logger.info("{} regions: {}", regions.size(), strRegions);

List<Variant> variants = new ArrayList<>();
if (areVariantsNeeded()) {
Expand All @@ -157,7 +157,7 @@ public void execute() throws CellBaseException {
GenomeManager genomeManager = managerFactory.getGenomeManager(species, assembly);

// Genome sequence
CellBaseDataResult results = genomeManager.getGenomeSequenceRawData(regions, dataRelease);
CellBaseDataResult<GenomeSequenceChunk> results = genomeManager.getGenomeSequenceRawData(regions, dataRelease);
counter = writeExportedData(results.getResults(), "genome_sequence", output);

// Genome info
Expand Down Expand Up @@ -342,7 +342,7 @@ private String exportPharmacogenomicsData(List<Gene> genes)
CellBaseFileSerializer serializer = new CellBaseJsonFileSerializer(output.resolve(PHARMACOGENOMICS_DATA), PHARMACOGENOMICS_DATA);

PharmaChemicalQuery query = new PharmaChemicalQuery();
List<String> geneNames = new ArrayList<>(new HashSet<>(genes.stream().map(g -> g.getName()).collect(Collectors.toList())));
List<String> geneNames = new ArrayList<>(new HashSet<>(genes.stream().map(Gene::getName).collect(Collectors.toList())));
query.setGeneNames(geneNames);
query.setDataRelease(dataRelease);
PharmacogenomicsManager pharmacogenomicsManager = managerFactory.getPharmacogenomicsManager(species, assembly);
Expand All @@ -358,7 +358,7 @@ private String exportPharmacogenomicsData(List<Gene> genes)

// Retrieve PubMed IDs from pharma chemical (discarding empty pubmed IDs)
for (PharmaGeneAnnotation gene : pharmaChemical.getGenes()) {
List<String> ids = gene.getPubmed().stream().filter(item -> StringUtils.isNotEmpty(item)).collect(Collectors.toList());
List<String> ids = gene.getPubmed().stream().filter(StringUtils::isNotEmpty).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(ids)) {
pubmedIds.addAll(ids);
}
Expand Down Expand Up @@ -400,8 +400,7 @@ private String exportPharmacogenomicsData(List<Gene> genes)
for (int i = 0; i < pubmedList.size(); i += subListSize) {
int end = Math.min(i + subListSize, pubmedList.size());
List<String> idList = pubmedList.subList(i, end);
if (CollectionUtils.isNotEmpty(idList) && idList.size() > 0) {
System.out.println(StringUtils.join(idList, ","));
if (CollectionUtils.isNotEmpty(idList)) {
publicationQuery.setIds(idList);
CellBaseDataResult<PubmedArticle> results = publicationManager.search(publicationQuery);
for (PubmedArticle pubmedArticle : results.getResults()) {
Expand Down Expand Up @@ -502,7 +501,6 @@ private List<Variant> getVariants(List<Region> regions) throws CellBaseException
VariantManager variantManager = managerFactory.getVariantManager(species, assembly);
VariantQuery query = new VariantQuery();
query.setDataRelease(dataRelease);
int batchSize = 10;
for (Region region : regions) {
query.setRegions(Collections.singletonList(region));
try {
Expand All @@ -522,14 +520,13 @@ private boolean areVariantsNeeded() {
if (data.equals(EtlCommons.VARIATION_DATA)
|| data.equals(EtlCommons.MISSENSE_VARIATION_SCORE_DATA)
|| data.equals(EtlCommons.SPLICE_SCORE_DATA)) {
// || data.equals(EtlCommons.VARIATION_FUNCTIONAL_SCORE_DATA)) {
return true;
}
}
return false;
}

private int writeExportedData(List<?> objects, String baseFilename, CellBaseFileSerializer serializer) throws IOException {
private int writeExportedData(List<?> objects, String baseFilename, CellBaseFileSerializer serializer) {
int counter = 0;
for (Object object : objects) {
serializer.serialize(object, baseFilename);
Expand All @@ -550,31 +547,15 @@ private int writeExportedData(List<?> objects, String baseFilename, Path outDir)
return counter;
}

private int writeExportedDataList(List<CellBaseDataResult<?>> results, String baseFilename, Path outDir) throws IOException {
checkPath(outDir);
int counter = 0;
CellBaseFileSerializer serializer = new CellBaseJsonFileSerializer(outDir);
for (CellBaseDataResult<?> result : results) {
for (Object object : result.getResults()) {
serializer.serialize(object, baseFilename);
counter++;
}
}
serializer.close();
return counter;
}

private void checkPath(Path outDir) throws IOException {
if (!outDir.toFile().exists()) {
if (!outDir.toFile().mkdirs()) {
throw new IOException("Impossible to create output directory: " + outDir);
}
if (!outDir.toFile().exists() && !outDir.toFile().mkdirs()) {
throw new IOException("Impossible to create output directory: " + outDir);
}
}

private void checkDataRelease() throws CellBaseException {
// Check data release
DataReleaseManager dataReleaseManager = new DataReleaseManager(database, configuration);
DataReleaseManager dataReleaseManager = managerFactory.getDataReleaseManager(species, assembly);
CellBaseDataResult<DataRelease> dataReleaseResults = dataReleaseManager.getReleases();
if (CollectionUtils.isEmpty(dataReleaseResults.getResults())) {
throw new CellBaseException("No data releases are available");
Expand Down

0 comments on commit fa814a9

Please sign in to comment.