use of uk.ac.ebi.spot.goci.model.Location in project goci by EBISPOT.
the class SnpLocationMappingService method storeSnpLocation.
public void storeSnpLocation(Map<String, Set<Location>> snpToLocations) {
// Go through each rs_id and its associated locations returned from the mapping pipeline
for (String snpRsId : snpToLocations.keySet()) {
Set<Location> snpLocationsFromMapping = snpToLocations.get(snpRsId);
// Check if the SNP exists
SingleNucleotidePolymorphism snpInDatabase = singleNucleotidePolymorphismQueryService.findByRsIdIgnoreCase(snpRsId);
if (snpInDatabase != null) {
// Store all new location objects
Collection<Location> newSnpLocations = new ArrayList<>();
for (Location snpLocationFromMapping : snpLocationsFromMapping) {
String chromosomeNameFromMapping = snpLocationFromMapping.getChromosomeName();
if (chromosomeNameFromMapping != null) {
chromosomeNameFromMapping = chromosomeNameFromMapping.trim();
}
Integer chromosomePositionFromMapping = snpLocationFromMapping.getChromosomePosition();
// if (chromosomePositionFromMapping != null) {
// chromosomePositionFromMapping = chromosomePositionFromMapping.trim();
// }
Region regionFromMapping = snpLocationFromMapping.getRegion();
String regionNameFromMapping = null;
if (regionFromMapping != null) {
if (regionFromMapping.getName() != null) {
regionNameFromMapping = regionFromMapping.getName().trim();
}
}
// Check if location already exists
Location existingLocation = locationRepository.findByChromosomeNameAndChromosomePositionAndRegionName(chromosomeNameFromMapping, chromosomePositionFromMapping, regionNameFromMapping);
if (existingLocation != null) {
newSnpLocations.add(existingLocation);
} else // Create new location
{
Location newLocation = locationCreationService.createLocation(chromosomeNameFromMapping, chromosomePositionFromMapping, regionNameFromMapping);
newSnpLocations.add(newLocation);
}
}
// If we have new locations then link to snp and save
if (newSnpLocations.size() > 0) {
// Set new location details
snpInDatabase.setLocations(newSnpLocations);
// Update the last update date
snpInDatabase.setLastUpdateDate(new Date());
singleNucleotidePolymorphismRepository.save(snpInDatabase);
} else {
getLog().warn("No new locations to add to " + snpRsId);
}
} else // SNP doesn't exist, this should be extremely rare as SNP value is a copy
// of the variant entered by the curator which
// by the time mapping is started should already have been saved
{
// TODO WHAT WILL HAPPEN FOR MERGED SNPS
getLog().error("Adding location for SNP not found in database, RS_ID:" + snpRsId);
throw new RuntimeException("Adding location for SNP not found in database, RS_ID: " + snpRsId);
}
}
}
use of uk.ac.ebi.spot.goci.model.Location in project goci by EBISPOT.
the class SnpLocationMappingService method removeExistingSnpLocations.
/**
* Method to remove the existing locations linked to a SNP
*
* @param snp SNP from which to remove the associated locations
*/
public void removeExistingSnpLocations(SingleNucleotidePolymorphism snp) {
// Get a list of locations currently linked to SNP
Collection<Location> oldSnpLocations = snp.getLocations();
if (oldSnpLocations != null && !oldSnpLocations.isEmpty()) {
Set<Long> oldSnpLocationIds = new HashSet<>();
for (Location oldSnpLocation : oldSnpLocations) {
oldSnpLocationIds.add(oldSnpLocation.getId());
}
// Remove old locations
snp.setLocations(new ArrayList<>());
singleNucleotidePolymorphismRepository.save(snp);
// Clean-up old locations that were linked to SNP
if (oldSnpLocationIds.size() > 0) {
for (Long oldSnpLocationId : oldSnpLocationIds) {
cleanUpLocations(oldSnpLocationId);
}
}
}
}
Aggregations