merge 3.4.1 into trunk
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / molecular / SingleReadAlignment.java
1 /**
2 *
3 */
4 package eu.etaxonomy.cdm.model.molecular;
5
6 import javax.persistence.Entity;
7 import javax.persistence.FetchType;
8 import javax.persistence.Lob;
9 import javax.persistence.ManyToOne;
10 import javax.xml.bind.annotation.XmlAccessType;
11 import javax.xml.bind.annotation.XmlAccessorType;
12 import javax.xml.bind.annotation.XmlElement;
13 import javax.xml.bind.annotation.XmlIDREF;
14 import javax.xml.bind.annotation.XmlRootElement;
15 import javax.xml.bind.annotation.XmlSchemaType;
16 import javax.xml.bind.annotation.XmlType;
17
18 import org.hibernate.annotations.Cascade;
19 import org.hibernate.annotations.CascadeType;
20 import org.hibernate.annotations.Type;
21 import org.hibernate.envers.Audited;
22
23 import eu.etaxonomy.cdm.model.common.VersionableEntity;
24
25 /**
26 * @author a.mueller
27 */
28 @XmlAccessorType(XmlAccessType.FIELD)
29 @XmlType(name = "SingleReadAlignment", propOrder = {
30 "consensusAlignment",
31 "singleRead",
32 "shifts",
33 "editedSequence",
34 "reverseComplement"
35 })
36 @XmlRootElement(name = "SingleReadAlignment")
37 @Entity
38 @Audited
39 public class SingleReadAlignment extends VersionableEntity {
40 private static final long serialVersionUID = 6141518347067279304L;
41
42 /** @see #getDnaMarker() */
43 @XmlElement(name = "ConsensusAlignment")
44 @XmlIDREF
45 @XmlSchemaType(name = "IDREF")
46 @ManyToOne(fetch = FetchType.LAZY)
47 //for now we do not cascade but expect the user to save the sequence manually
48 private Sequence consensusAlignment;
49
50 /** @see #getDnaMarker() */
51 @XmlElement(name = "SingleRead")
52 @XmlIDREF
53 @XmlSchemaType(name = "IDREF")
54 @ManyToOne(fetch = FetchType.LAZY)
55 @Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE})
56 private SingleRead singleRead;
57
58 //TODO XML mapping / user type
59 @Type(type="shiftUserType")
60 private Shift[] shifts;
61
62 @XmlElement(name = "EditedSequence")
63 @Lob
64 private String editedSequence;
65
66 @XmlElement(name = "ReverseComplement")
67 private boolean reverseComplement;
68
69
70 public static class Shift{
71 public int position;
72 public int shift;
73
74 public Shift(){};
75 public Shift(int position, int steps) {
76 this.position = position;
77 this.shift = steps;
78 }
79
80 @Override
81 public String toString(){
82 return String.valueOf(position) + "," + String.valueOf(shift);
83 }
84 }
85
86 //****************** FACTORY *******************/
87
88 public static SingleReadAlignment NewInstance(Sequence consensusSequence, SingleRead singleRead){
89 return new SingleReadAlignment(consensusSequence, singleRead, null, null);
90 }
91
92 public static SingleReadAlignment NewInstance(Sequence consensusSequence, SingleRead singleRead,
93 Shift[] shifts, String editedSequence){
94 return new SingleReadAlignment(consensusSequence, singleRead, shifts, editedSequence);
95 }
96
97 // ***************** CONSTRUCTOR *************************/
98
99 protected SingleReadAlignment(){};
100
101 private SingleReadAlignment(Sequence consensusAlignment, SingleRead singleRead,
102 Shift[] shifts, String editedSequence){
103 setConsensusAlignment(consensusAlignment);
104 setSingleRead(singleRead);
105 this.shifts = shifts;
106 this.editedSequence = editedSequence;
107 }
108
109
110 // ****************** GETTER / SETTER ***********************/
111
112 //consensus sequence
113 public Sequence getConsensusSequence() {
114 return consensusAlignment;
115 }
116 public void setConsensusAlignment(Sequence consensusAlignment) {
117 if (this.consensusAlignment != null && this.consensusAlignment.getSingleReadAlignments().contains(this)){
118 this.consensusAlignment.removeSingleReadAlignment(this);
119 }
120 this.consensusAlignment = consensusAlignment;
121 if (consensusAlignment != null && ! consensusAlignment.getSingleReadAlignments().contains(this)){
122 consensusAlignment.addSingleReadAlignment(this);
123 }
124 }
125
126 public SingleRead getSingleRead() {
127 return singleRead;
128 }
129 public void setSingleRead(SingleRead singleRead) {
130 // if (this.singleRead != null xxx){
131 // this.singleRead.removeSingleReadAlignment(this);
132 // }
133 this.singleRead = singleRead;
134 // if (singleRead != null && singleRead.getSingleReadAlignments().contains(this)){
135 // singleRead.addSingleReadAlignment(this);
136 // }
137 }
138
139 //shifts
140 public Shift[] getShifts() {
141 return shifts;
142 }
143 public void setShifts(Shift[] shifts) {
144 this.shifts = shifts;
145 }
146
147 //edited sequence
148 public String getEditedSequence() {
149 return editedSequence;
150 }
151
152 public void setEditedSequence(String editedSequence) {
153 this.editedSequence = editedSequence;
154 }
155
156 // ******************* CLONE *********************/
157
158
159
160 @Override
161 public Object clone() throws CloneNotSupportedException {
162 //all objects can be reused
163 return super.clone();
164 }
165 }