Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. /** Class for storing and working with sam formatted DNA sequence.
  2. *
  3. * Upon construction, only the String record is stored.
  4. * All querying of fields is done on demand, to save time.
  5. *
  6. */
  7. public class SamRecord implements Record {
  8.  
  9. private final String read;
  10. private String id = null;
  11. private int flag = -1;
  12. private String referenceName = null;
  13. private int pos = -1;
  14. private int mappingQuality = -1;
  15. private String cigar = null;
  16. private String mateReferenceName = null;
  17. private int matePosition = -1;
  18. private int templateLength = -1;
  19. private String sequence = null;
  20. private String quality = null;
  21. private String variableTerms = null;
  22.  
  23. private final static String REPEAT_TERM = "ZS:Z:R";
  24. private final static String MATCH_TERM = "ZS:Z:NM";
  25. private final static String QUALITY_CHECK_TERM = "ZS:Z:QC";
  26.  
  27. /** Simple constructor for the sam record
  28. * @param read full read
  29. */
  30. public SamRecord(String read) {
  31. this.read = read;
  32. }
  33.  
  34. public String getRead() {
  35. return read;
  36. }
  37.  
  38. /**
  39. * {@inheritDoc}
  40. */
  41. @Override
  42. public String getId() {
  43. if(id == null){
  44. id = XsamReadQueries.findID(read);
  45. }
  46.  
  47. return id;
  48. }
  49.  
  50. /**
  51. * {@inheritDoc}
  52. */
  53. @Override
  54. public int getFlag() throws NumberFormatException {
  55. if(flag == -1) {
  56. flag = Integer.parseInt(XsamReadQueries.findElement(read, 1));
  57. }
  58. return flag;
  59. }
  60.  
  61. /**
  62. * {@inheritDoc}
  63. */
  64. @Override
  65. public String getReferenceName() {
  66. if(referenceName == null){
  67. referenceName = XsamReadQueries.findReferneceName(read);
  68. }
  69.  
  70. return referenceName;
  71. }
  72.  
  73. /**
  74. * {@inheritDoc}
  75. */
  76. @Override
  77. public int getPos() throws NumberFormatException{
  78. if(pos == -1){
  79. pos = Integer.parseInt(XsamReadQueries.findElement(read, 3));
  80. }
  81.  
  82. return pos;
  83. }
  84.  
  85. /**
  86. * {@inheritDoc}
  87. */
  88. @Override
  89. public int getMappingQuality() throws NumberFormatException {
  90. if(mappingQuality == -1){
  91. mappingQuality = Integer.parseInt(XsamReadQueries.findElement(read, 4));
  92. }
  93.  
  94. return mappingQuality;
  95. }
  96.  
  97. /**
  98. * {@inheritDoc}
  99. */
  100. @Override
  101. public String getCigar() {
  102. if(cigar == null){
  103. cigar = XsamReadQueries.findCigar(read);
  104. }
  105.  
  106. return cigar;
  107. }
  108.  
  109. /**
  110. * {@inheritDoc}
  111. */
  112. @Override
  113. public String getMateReferenceName() {
  114. if(mateReferenceName == null){
  115. mateReferenceName = XsamReadQueries.findElement(read, 6);
  116. }
  117.  
  118. return mateReferenceName;
  119. }
  120.  
  121. /**
  122. * {@inheritDoc}
  123. */
  124. @Override
  125. public int getMatePosition() throws NumberFormatException {
  126. if(matePosition == -1){
  127. matePosition = Integer.parseInt(XsamReadQueries.findElement(read, 7));
  128. }
  129.  
  130. return matePosition;
  131. }
  132.  
  133. /**
  134. * {@inheritDoc}
  135. */
  136. @Override
  137. public int getTemplateLength() throws NumberFormatException {
  138. if(templateLength == -1){
  139. templateLength = Integer.parseInt(XsamReadQueries.findElement(read, 8));
  140. }
  141.  
  142. return templateLength;
  143. }
  144.  
  145. /**
  146. * {@inheritDoc}
  147. */
  148. @Override
  149. public String getSequence() {
  150. if(sequence == null){
  151. sequence = XsamReadQueries.findBaseSequence(read);
  152. }
  153.  
  154. return sequence;
  155. }
  156.  
  157. /**
  158. * {@inheritDoc}
  159. */
  160. @Override
  161. public String getQuality() {
  162. if(quality == null){
  163. quality = XsamReadQueries.findElement(read, 10);
  164. }
  165.  
  166. return quality;
  167. }
  168.  
  169. /**
  170. * {@inheritDoc}
  171. */
  172. @Override
  173. public boolean isRepeat() {
  174. return read.contains(REPEAT_TERM);
  175. }
  176.  
  177. /**
  178. * {@inheritDoc}
  179. */
  180. @Override
  181. public boolean isMapped() {
  182. return !read.contains(MATCH_TERM);
  183. }
  184.  
  185.  
  186. /**
  187. * {@inheritDoc}
  188. */
  189. @Override
  190. public String getVariableTerms() {
  191. if(variableTerms == null){
  192. variableTerms = XsamReadQueries.findVariableRegionSequence(read);
  193. }
  194.  
  195. return variableTerms;
  196. }
  197.  
  198. /**
  199. * {@inheritDoc}
  200. */
  201. @Override
  202. public boolean isQualityFailed() {
  203. return read.contains(QUALITY_CHECK_TERM);
  204. }
  205.  
  206.  
  207. @Override
  208. public boolean equals(Object o) {
  209. if (this == o) return true;
  210. if (o == null || getClass() != o.getClass()) return false;
  211. SamRecord samRecord = (SamRecord) o;
  212. return Objects.equals(read, samRecord.read);
  213. }
  214.  
  215. @Override
  216. public int hashCode() {
  217. return Objects.hash(read);
  218. }
  219.  
  220. @Override
  221. public String toString() {
  222. return read;
  223. }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement