Advertisement
xosski

Genome sequencing/mapping

Jan 2nd, 2025
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. Gene entity
  2. @Entity
  3. @Table(name = "gene")
  4. @Getter
  5. @Setter
  6. @NoArgsConstructor
  7. @AllArgsConstructor
  8. @ToString
  9. public class Gene {
  10.  
  11. @Id
  12. @Column(name = "id")
  13. private Short id;
  14.  
  15. @NotBlank
  16. @Size(max = 32)
  17. @Column(name = "name", nullable = false, unique = true, length = 32)
  18. private String name;
  19.  
  20. @NotBlank
  21. @Size(max = 128)
  22. @Column(name = "description", nullable = false, unique = true, length = 128)
  23. private String description;
  24.  
  25. @NotNull
  26. @Enumerated(EnumType.STRING)
  27. @Column(name = "priority", nullable = false)
  28. private GenePriority priority;
  29. }
  30. //////////////
  31. Genome class
  32.  
  33. @Embeddable
  34. @Getter
  35. @Setter
  36. @NoArgsConstructor
  37. @AllArgsConstructor
  38. @ToString
  39. public class Genome {
  40.  
  41. public static final int SIZE = 8;
  42.  
  43. @ManyToOne(fetch = FetchType.EAGER, optional = false)
  44. @JoinColumn(name = "gene0_id", referencedColumnName = "id", nullable = false)
  45. private Gene gene0;
  46.  
  47. @ManyToOne(fetch = FetchType.EAGER, optional = false)
  48. @JoinColumn(name = "gene1_id", referencedColumnName = "id", nullable = false)
  49. private Gene gene1;
  50.  
  51. @ManyToOne(fetch = FetchType.EAGER, optional = false)
  52. @JoinColumn(name = "gene2_id", referencedColumnName = "id", nullable = false)
  53. private Gene gene2;
  54.  
  55. @ManyToOne(fetch = FetchType.EAGER, optional = false)
  56. @JoinColumn(name = "gene3_id", referencedColumnName = "id", nullable = false)
  57. private Gene gene3;
  58.  
  59. @ManyToOne(fetch = FetchType.EAGER, optional = false)
  60. @JoinColumn(name = "gene4_id", referencedColumnName = "id", nullable = false)
  61. private Gene gene4;
  62.  
  63. @ManyToOne(fetch = FetchType.EAGER, optional = false)
  64. @JoinColumn(name = "gene5_id", referencedColumnName = "id", nullable = false)
  65. private Gene gene5;
  66.  
  67. @ManyToOne(fetch = FetchType.EAGER, optional = false)
  68. @JoinColumn(name = "gene6_id", referencedColumnName = "id", nullable = false)
  69. private Gene gene6;
  70.  
  71. @ManyToOne(fetch = FetchType.EAGER, optional = false)
  72. @JoinColumn(name = "gene7_id", referencedColumnName = "id", nullable = false)
  73. private Gene gene7;
  74.  
  75. @Transient
  76. public List<Gene> getGenes() {
  77. return List.of(gene0, gene1, gene2, gene3, gene4, gene5, gene6, gene7);
  78. }
  79.  
  80. public Genome(List<Gene> genes) {
  81. setGenes(genes);
  82. }
  83.  
  84. @Transient
  85. public void setGenes(List<Gene> genes) {
  86. if (genes.size() != SIZE) {
  87. throw new ValidationException("The genome must contain " + SIZE + " genes.");
  88. }
  89. this.gene0 = genes.get(0);
  90. this.gene1 = genes.get(1);
  91. this.gene2 = genes.get(2);
  92. this.gene3 = genes.get(3);
  93. this.gene4 = genes.get(4);
  94. this.gene5 = genes.get(5);
  95. this.gene6 = genes.get(6);
  96. this.gene7 = genes.get(7);
  97. }
  98. }
  99.  
  100. ///////////
  101. Mapping DTO(records)
  102. public record GenomeGeneIds(
  103. @Size(min = Genome.SIZE, max = Genome.SIZE)
  104. @Schema(example = "[1, 2, 3, 4, 5, 6, 7, 8]")
  105. @JsonProperty(value = "geneIds", required = true)
  106. List<Short> geneIds
  107. ) {}
  108.  
  109. public record CellGeneIds(
  110. @JsonProperty(value = "coordinates", required = true)
  111. Coordinates coordinates,
  112.  
  113. @Valid
  114. @JsonProperty(value = "genome", required = true)
  115. GenomeGeneIds genome,
  116.  
  117. @Schema(example = "null")
  118. @JsonProperty(value = "deathTimeout", required = false)
  119. Integer deathTimeout
  120. ) {}
  121.  
  122. public record FieldGeneIds(
  123. @Valid
  124. @JsonProperty(value = "grid", required = true)
  125. CellGeneIds[][] grid
  126. ) {}
  127.  
  128. ///////
  129. Genome mapping
  130. @Service
  131. public class GenomeMappingService {
  132.  
  133. private final GeneRepository geneRepository;
  134.  
  135. @Autowired
  136. public GenomeMappingService(GeneRepository geneRepository) {
  137. this.geneRepository = geneRepository;
  138. }
  139.  
  140. public Genome toGenome(GenomeGeneIds genomeGeneIds) {
  141. List<Gene> genes = genomeGeneIds.geneIds().stream()
  142. .map(geneId -> geneRepository.findById(geneId)
  143. .orElseThrow(() -> new IllegalArgumentException("Gene not found for ID: " + geneId)))
  144. .toList();
  145. return new Genome(genes);
  146. }
  147.  
  148. public GenomeGeneIds toGenomeGeneIds(Genome genome) {
  149. List<Short> geneIds = genome.getGenes().stream()
  150. .map(Gene::getId)
  151. .toList();
  152. return new GenomeGeneIds(geneIds);
  153. }
  154. }
  155. ///////////
  156. Field mapping
  157. @Service
  158. public class FieldMappingService {
  159.  
  160. private final GenomeMappingService genomeMappingService;
  161.  
  162. @Autowired
  163. public FieldMappingService(GenomeMappingService genomeMappingService) {
  164. this.genomeMappingService = genomeMappingService;
  165. }
  166.  
  167. public Field toField(FieldGeneIds fieldGeneIds) {
  168. Cell[][] grid = Arrays.stream(fieldGeneIds.grid())
  169. .map(row -> Arrays.stream(row)
  170. .map(this::toCell)
  171. .toArray(Cell[]::new))
  172. .toArray(Cell[][]::new);
  173. return new Field(grid);
  174. }
  175.  
  176. public FieldGeneIds toFieldGeneIds(Field field) {
  177. CellGeneIds[][] grid = Arrays.stream(field.grid())
  178. .map(row -> Arrays.stream(row)
  179. .map(this::toCellGeneIds)
  180. .toArray(CellGeneIds[]::new))
  181. .toArray(CellGeneIds[][]::new);
  182. return new FieldGeneIds(grid);
  183. }
  184.  
  185. private Cell toCell(CellGeneIds cellGeneIds) {
  186. Genome genome = genomeMappingService.toGenome(cellGeneIds.genome());
  187. return new Cell(
  188. cellGeneIds.coordinates(),
  189. genome,
  190. cellGeneIds.deathTimeout()
  191. );
  192. }
  193.  
  194. private CellGeneIds toCellGeneIds(Cell cell) {
  195. GenomeGeneIds genomeGeneIds = genomeMappingService.toGenomeGeneIds(cell.genome());
  196. return new CellGeneIds(
  197. cell.coordinates(),
  198. genomeGeneIds,
  199. cell.deathTimeout()
  200. );
  201. }
  202. }
  203.  
  204.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement