| 1 |
;; This buffer is for notes you don't want to save, and for Lisp evaluation. |
| 2 |
;; If you want to create a file, visit that file with C-x C-f, |
| 3 |
;; then enter the text in that file's own buffer. |
| 4 |
|
| 5 |
Samfile -> AlignmentFile |
| 6 |
AlignedRead -> AlignedSegment |
| 7 |
Tabixfile -> TabixFile |
| 8 |
Fastafile -> FastaFile |
| 9 |
Fastqfile -> FastqFile |
| 10 |
|
| 11 |
Changes to the AlignedSegment.API: |
| 12 |
|
| 13 |
Basic attributes |
| 14 |
================ |
| 15 |
|
| 16 |
qname -> query_name |
| 17 |
tid -> reference_id |
| 18 |
pos -> reference_start |
| 19 |
mapq -> mapping_quality |
| 20 |
rnext -> next_reference_id |
| 21 |
pnext -> next_reference_start |
| 22 |
cigar = alignment (now returns CigarAlignment object) |
| 23 |
cigarstring = cigarstring |
| 24 |
tlen -> query_length |
| 25 |
seq -> query_sequence |
| 26 |
qual -> query_qualities, now returns array |
| 27 |
tags = tags (now returns Tags object) |
| 28 |
|
| 29 |
|
| 30 |
Derived simple attributes |
| 31 |
========================= |
| 32 |
|
| 33 |
alen -> reference_length, reference is always "alignment", so removed |
| 34 |
aend -> reference_end |
| 35 |
rlen -> query_length |
| 36 |
query -> query_alignment_sequence |
| 37 |
qqual -> query_alignment_qualities, now returns array |
| 38 |
qstart -> query_alignment_start |
| 39 |
qend -> query_alignment_end |
| 40 |
qlen -> query_alignment_length, kept, because can be computed without fetching the sequence |
| 41 |
mrnm -> next_reference_id |
| 42 |
mpos -> next_reference_start |
| 43 |
rname -> reference_id |
| 44 |
isize -> query_length |
| 45 |
|
| 46 |
|
| 47 |
Complex attributes - functions |
| 48 |
=============================== |
| 49 |
|
| 50 |
blocks -> getBlocks() |
| 51 |
aligned_pairs -> getAlignedPairs() |
| 52 |
inferred_length -> inferQueryLength() |
| 53 |
positions -> getReferencePositions() |
| 54 |
overlap() -> getOverlap() |
| 55 |
|
| 56 |
|
| 57 |
Backwards incompatible changes: |
| 58 |
================================ |
| 59 |
|
| 60 |
1. Empty cigarstring now returns None (instead of '') |
| 61 |
|
| 62 |
2. Empty cigar now returns None (instead of []) |
| 63 |
|
| 64 |
3. When using the extension classes in cython modules, AlignedRead |
| 65 |
needs to be substituted with AlignedSegment. Automatic casting |
| 66 |
of the base class to the derived class seems not to work? |
| 67 |
|
| 68 |
4. fancy_str() has been removed |
| 69 |
|
| 70 |
|
| 71 |
===================================================== |
| 72 |
|
| 73 |
Kevin's suggestions: |
| 74 |
|
| 75 |
|
| 76 |
* smarter file opener |
| 77 |
* CRAM support |
| 78 |
* CSI index support (create and use) |
| 79 |
* better attribute and property names |
| 80 |
* remove deprecated names |
| 81 |
* add object oriented CIGAR and tag handling |
| 82 |
* fetch query that recruits mate pairs that overlap query region |
| 83 |
* other commonly re-invented functionality |
| 84 |
|
| 85 |
qname -> template_name |
| 86 |
pos -> reference_start |
| 87 |
aend -> reference_stop |
| 88 |
alen -> reference_length |
| 89 |
tid -> ref_id |
| 90 |
qname -> template_name (not a high priority, but would be clearer) |
| 91 |
qstart -> query_start (really segment_align_start) |
| 92 |
qend -> query_stop (really segment_align_stop) |
| 93 |
qlen -> query_length (really segment_align_length) |
| 94 |
qqual -> query_qual (really segment_align_qual) |
| 95 |
rlen -> drop in favor of len(align.seq) |
| 96 |
inferred_length -> inferred_query_length |
| 97 |
is_* -> replace with flag object that is a subclass of int |
| 98 |
cigarstring -> str(align.cigar) |
| 99 |
tags -> opts object with a mapping-like interface |
| 100 |
Non-backward compatible: |
| 101 |
rname, mrnm, mpos, isize -> remove |
| 102 |
cigar -> CigarSequence object (or something similar) |
| 103 |
All coordinate and length queries return None when a value is not available (currently some return None, some 0) |
| 104 |
Store qualities as an array or bytes type |
| 105 |
|
| 106 |
|
| 107 |
Marcel's suggestions: |
| 108 |
|
| 109 |
I recently sent in a pull request (which was merged) that improves the pysam doc a bit. While preparing that, I also wrote down some ways in which the API itself could be improved. I think the API is ok, but for someone like me who uses pysam not every day, some of the details are hard to remember and every time I do very basic things I end up looking them up again in the docs. I can recommend this article, written for C++, but many points still apply: |
| 110 |
http://qt-project.org/wiki/API_Design_Principles . |
| 111 |
|
| 112 |
Originally, I wanted to convert at least some of these suggestions to pull requests, but I'm not sure I have the time for that in the near future. So before I forget about this completely, I thought it's best to at least send this to the list. I'm concentrating on issues I found in Samfile and AlignedRead here. |
| 113 |
|
| 114 |
- The terminology is inconsistent - often two words are used |
| 115 |
to describe the same thing: opt vs. tag, query vs. read, |
| 116 |
reference vs. target. My suggestion is to consistently use |
| 117 |
the same terms as in the SAM spec: tag, query, |
| 118 |
reference. This applies both to documentation and |
| 119 |
function/property names. |
| 120 |
|
| 121 |
- In line with the document linked to above (see the |
| 122 |
section "The Art of Naming"): Do not abbreviate function |
| 123 |
and property names. For example, tlen -> template_length, |
| 124 |
pos -> position, mapq -> mapping_quality etc. |
| 125 |
|
| 126 |
- Be consistent with multiword function and variable names. I |
| 127 |
suggest to use the PEP8 convention. This isn't so visible |
| 128 |
to the user in Samfile and AlignedRead, it seems, but there |
| 129 |
are things like convertBinaryTagToList which could be |
| 130 |
renamed to convert_binary_tag_to_list. |
| 131 |
|
| 132 |
- Don't make functions that are not setters or getters a |
| 133 |
property. This applies to |
| 134 |
AlignedRead.positions/.inferred_length/.aligned_pairs/.blocks |
| 135 |
and currently also Samfile.references, for example. Making |
| 136 |
these a property implies to the user that access requires |
| 137 |
constant time. This is important in code like this: |
| 138 |
|
| 139 |
for i in range(n): |
| 140 |
do_something_with(read.positions[i]) |
| 141 |
|
| 142 |
This looks inconspicuous, but is possibly inefficient since |
| 143 |
.positions actually builds up the result it returns each time |
| 144 |
it's accessed. |
| 145 |
|
| 146 |
- Update the examples in the docs to use context manager |
| 147 |
syntax. |
| 148 |
|
| 149 |
Particular to Samfile: |
| 150 |
|
| 151 |
- Deprecate Samfile.nreferences: propose to use |
| 152 |
len(samfile.references) instead. Cache Samfile.references |
| 153 |
so it's not re-created every time. |
| 154 |
|
| 155 |
- Move Samfile.mapped/.unmapped/.nocoordinate into a .stats |
| 156 |
attribute (Samfile.stats.mapped/.unmapped/.noocordinate). |
| 157 |
|
| 158 |
Particular to AlignedRead: |
| 159 |
|
| 160 |
- When read is an AlignedRead, print(read) should print out a |
| 161 |
properly formatted SAM line. |
| 162 |
|
| 163 |
- Force assignment to .qual/.seq to go through a function |
| 164 |
that sets both at the same time. This avoids the problem |
| 165 |
that they must be set in a particular order, which is easy |
| 166 |
to forget and only 'enforced' through documentation. |
| 167 |
|
| 168 |
- Deprecate rlen, use len(read.seq) instead. |
| 169 |
|
| 170 |
- Handling of tags is a little awkward. Would be cool if this |
| 171 |
worked: |
| 172 |
|
| 173 |
read.tags['XK'] = 'hello' # add a new tag if it doesn't exist |
| 174 |
read.tags['AS'] += 17 # update the value of a tag |
| 175 |
del read.tags['AS'] |
| 176 |
if 'AS' in read.tags: ... |
| 177 |
|
| 178 |
- Add a property AlignedRead.qualities that behaves like a |
| 179 |
Py3-bytes object in both Py2 and Py3. That is, accessing |
| 180 |
read.qualities[0] gives you an int value that represents |
| 181 |
the quality. The fact that qualities are encoded as |
| 182 |
ASCII(q+33) is an implementation detail that can be hidden. |
| 183 |
|
| 184 |
Done |
| 185 |
|
| 186 |
- And finally: Add a Cigar class. I guess this is already |
| 187 |
what 'improved CIGAR string handling' refers to in the |
| 188 |
roadmap. AlignedRead.cigar would then return a Cigar object |
| 189 |
that can be printed, compared and concatenated to others, |
| 190 |
whose length can be measured etc. The .unclipped and |
| 191 |
.inferred length properties can also be moved here. Or |
| 192 |
perhaps: Make this an Alignment class that even knows about |
| 193 |
the two strings it is aligning. One could then also iterate |
| 194 |
over all columns of the alignment. But I guess this goes |
| 195 |
too far since that's what the AlignedRead itself should be. |
| 196 |
|
| 197 |
Regards, |
| 198 |
Marcel |