Mercurial > repos > galaxyp > mzsqlite_psm_align
comparison profmt.py @ 0:f2dc9805107a draft
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/mzsqlite_psm_align commit 464e05be1084ed9a65b542c8eabb18147d425666
| author | galaxyp |
|---|---|
| date | Mon, 16 Apr 2018 18:00:53 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:f2dc9805107a |
|---|---|
| 1 #!/usr/bin/env python | |
| 2 """ | |
| 3 # | |
| 4 #------------------------------------------------------------------------------ | |
| 5 # University of Minnesota | |
| 6 # Copyright 2018, Regents of the University of Minnesota | |
| 7 #------------------------------------------------------------------------------ | |
| 8 # Author: | |
| 9 # | |
| 10 # James E Johnson | |
| 11 # | |
| 12 #------------------------------------------------------------------------------ | |
| 13 """ | |
| 14 | |
| 15 import sys,re | |
| 16 from operator import itemgetter, attrgetter | |
| 17 from twobitreader import TwoBitFile | |
| 18 | |
| 19 | |
| 20 PROBAM_TAGS = ['NH', 'XO', 'XL', 'XP', 'YP', 'XF', 'XI', 'XB', 'XR', 'YB', 'YA', 'XS', 'XQ', 'XC', 'XA', 'XM', 'XN', 'XT', 'XE', 'XG', 'XU'] | |
| 21 | |
| 22 | |
| 23 PROBAM_TYTPES = { | |
| 24 'NH' : 'i', #number of genomic locations to which the peptide sequence maps | |
| 25 'XO' : 'Z', #uniqueness of the peptide mapping | |
| 26 'XL' : 'i', #number of peptides to which the spectrum maps | |
| 27 'XP' : 'Z', #peptide sequence | |
| 28 'YP' : 'Z', #Protein accession ID from the original search result | |
| 29 'XF' : 'Z', #Reading frame of the peptide (0, 1, 2) | |
| 30 'XI' : 'f', #Peptide intensity | |
| 31 'XB' : 'Z', #massdiff; experimental mass; calculated mass massdiff can be calculated by experimental mass - calculated mass. If any number is unavailable, the value should be left blank (such as 0.01;;). | |
| 32 'XR' : 'Z', #reference peptide sequence | |
| 33 'YB' : 'Z', #Preceding amino acids (2 AA, B stands for before). | |
| 34 'YA' : 'Z', #Following amino acids (2 AA, A stands for after). | |
| 35 'XS' : 'f', #PSM score | |
| 36 'XQ' : 'f', #PSM FDR (i.e. q-value or 1-PEP). | |
| 37 'XC' : 'i', #peptide charge | |
| 38 'XA' : 'i', #Whether the peptide is annotated 0:yes; 1:parially unknown; 2:totally unknown; | |
| 39 'XM' : 'Z', #Modifications | |
| 40 'XN' : 'i', #Number of missed cleavages in the peptide (XP) | |
| 41 'XT' : 'i', #Enzyme specificity | |
| 42 'XE' : 'i', #Enzyme used in the experiment | |
| 43 'XG' : 'A', #Peptide type | |
| 44 'XU' : 'Z', #URI | |
| 45 } | |
| 46 | |
| 47 | |
| 48 PROBAM_DEFAULTS = { | |
| 49 'NH' : -1, #number of genomic locations to which the peptide sequence maps | |
| 50 'XO' : '*', #uniqueness of the peptide mapping | |
| 51 'XL' : -1, #number of peptides to which the spectrum maps | |
| 52 'XP' : '*', #peptide sequence | |
| 53 'YP' : '*', #Protein accession ID from the original search result | |
| 54 'XF' : '*', #Reading frame of the peptide (0, 1, 2) | |
| 55 'XI' : -1, #Peptide intensity | |
| 56 'XB' : '*', #massdiff; experimental mass; calculated mass massdiff can be calculated by experimental mass - calculated mass. If any number is unavailable, the value should be left blank (such as 0.01;;). | |
| 57 'XR' : '*', #reference peptide sequence | |
| 58 'YB' : '*', #Preceding amino acids (2 AA, B stands for before). | |
| 59 'YA' : '*', #Following amino acids (2 AA, A stands for after). | |
| 60 'XS' : -1, #PSM score | |
| 61 'XQ' : -1, #PSM FDR (i.e. q-value or 1-PEP). | |
| 62 'XC' : -1, #peptide charge | |
| 63 'XA' : -1, #Whether the peptide is annotated 0:yes; 1:parially unknown; 2:totally unknown; | |
| 64 'XM' : '*', #Modifications | |
| 65 'XN' : -1, #Number of missed cleavages in the peptide (XP) | |
| 66 'XT' : -1, #Enzyme specificity | |
| 67 'XE' : -1, #Enzyme used in the experiment | |
| 68 'XG' : '*', #Peptide type | |
| 69 'XU' : '*', #URI | |
| 70 } | |
| 71 | |
| 72 def cmp_alphanumeric(s1,s2): | |
| 73 if s1 == s2: | |
| 74 return 0 | |
| 75 a1 = re.findall("\d+|[a-zA-Z]+",s1) | |
| 76 a2 = re.findall("\d+|[a-zA-Z]+",s2) | |
| 77 for i in range(min(len(a1),len(a2))): | |
| 78 if a1[i] == a2[i]: | |
| 79 continue | |
| 80 if a1[i].isdigit() and a2[i].isdigit(): | |
| 81 return int(a1[i]) - int(a2[i]) | |
| 82 return 1 if a1[i] > a2[i] else -1 | |
| 83 return len(a1) - len(a2) | |
| 84 | |
| 85 | |
| 86 def sort_chrom_names(names): | |
| 87 rnames = sorted(names,cmp=cmp_alphanumeric) | |
| 88 if 'chrM' in rnames: | |
| 89 rnames.remove('chrM') | |
| 90 rnames.insert(0,'chrM') | |
| 91 if 'MT' in rnames: | |
| 92 rnames.remove('MT') | |
| 93 rnames.append('MT') | |
| 94 return rnames | |
| 95 | |
| 96 | |
| 97 def as_int_list(obj): | |
| 98 if obj is None: | |
| 99 return None | |
| 100 if isinstance(obj, list): | |
| 101 return [int(x) for x in obj] | |
| 102 elif isinstance(obj, str): | |
| 103 return [int(x) for x in obj.split(',')] | |
| 104 else: # python2 unicode? | |
| 105 return [int(x) for x in str(obj).split(',')] | |
| 106 | |
| 107 | |
| 108 class ProBEDEntry (object): | |
| 109 def __init__(self, chrom, chromStart, chromEnd, name, score, strand, | |
| 110 blockCount, blockSizes, blockStarts, | |
| 111 protacc, peptide, uniqueness, genomeReference, | |
| 112 psmScore='.', fdr='.', mods='.', charge='.', | |
| 113 expMassToCharge='.', calcMassToCharge='.', | |
| 114 psmRank='.', datasetID='.', uri='.'): | |
| 115 self.chrom = chrom | |
| 116 self.chromStart = int(chromStart) | |
| 117 self.chromEnd = int(chromEnd) | |
| 118 self.name = name | |
| 119 self.score = int(score) if score is not None else 0 | |
| 120 self.strand = '-' if str(strand).startswith('-') else '+' | |
| 121 self.thickStart = self.chromStart | |
| 122 self.thickEnd = self.chromEnd | |
| 123 self.itemRgb = '0' | |
| 124 self.blockCount = int(blockCount) | |
| 125 self.blockSizes = as_int_list(blockSizes) | |
| 126 self.blockStarts = as_int_list(blockStarts) | |
| 127 self.protacc = protacc | |
| 128 self.peptide = peptide | |
| 129 self.uniqueness = uniqueness | |
| 130 self.genomeReference = genomeReference | |
| 131 self.psmScore = psmScore | |
| 132 self.fdr = fdr | |
| 133 self.mods = mods | |
| 134 self.charge = charge | |
| 135 self.expMassToCharge = expMassToCharge | |
| 136 self.calcMassToCharge = calcMassToCharge | |
| 137 self.psmRank = psmRank | |
| 138 self.datasetID = datasetID | |
| 139 self.uri = uri | |
| 140 | |
| 141 def __str__(self): | |
| 142 return '%s\t%d\t%d\t%s\t%d\t%s\t%d\t%d\t%s\t%d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n' % \ | |
| 143 (self.chrom, self.chromStart, self.chromEnd, | |
| 144 self.name, self.score, self.strand, | |
| 145 self.thickStart, self.thickEnd, self.itemRgb, | |
| 146 self.blockCount, | |
| 147 ','.join([str(x) for x in self.blockSizes]), | |
| 148 ','.join([str(x) for x in self.blockStarts]), | |
| 149 self.protacc, self.peptide, self.uniqueness, | |
| 150 self.genomeReference, | |
| 151 self.psmScore, self.fdr, self.mods, | |
| 152 self.charge, self.expMassToCharge, self.calcMassToCharge, | |
| 153 self.psmRank, self.datasetID, self.uri) | |
| 154 | |
| 155 | |
| 156 class ProBED ( object ): | |
| 157 def __init__(self,species=None,assembly=None,comments=[]): | |
| 158 self.species = species | |
| 159 self.assembly = assembly | |
| 160 self.comments = comments | |
| 161 self.entries = dict() | |
| 162 | |
| 163 def add_entry(self,entry): | |
| 164 if not entry.chrom in self.entries: | |
| 165 self.entries[entry.chrom] = [] | |
| 166 self.entries[entry.chrom].append(entry) | |
| 167 | |
| 168 def write(self,fh): | |
| 169 rnames = sort_chrom_names(self.entries.keys()) | |
| 170 for sn in rnames: | |
| 171 if sn not in self.entries: | |
| 172 continue | |
| 173 for pbe in sorted(self.entries[sn], key=attrgetter('chromStart','chromEnd')): | |
| 174 fh.write(str(pbe)) | |
| 175 | |
| 176 | |
| 177 class ProBAMEntry (object): | |
| 178 def __init__(self, qname='', flag=0, rname='', pos=0, mapq=255, cigar='', rnext='*', pnext='0', tlen='0', seq='*', qual='*', optional=PROBAM_DEFAULTS): | |
| 179 self.qname = qname | |
| 180 self.flag = flag | |
| 181 self.rname = rname | |
| 182 self.pos = pos | |
| 183 self.mapq = mapq | |
| 184 self.cigar = cigar | |
| 185 self.rnext = rnext | |
| 186 self.pnext = pnext | |
| 187 self.tlen = tlen | |
| 188 self.seq = seq | |
| 189 self.qual = qual | |
| 190 self.optional = optional | |
| 191 def __str__(self): | |
| 192 opt_cols = '\t%s' % '\t'.join(['%s:%s:%s' % (t,PROBAM_TYTPES[t],self.optional[t]) for t in PROBAM_TAGS]) if self.optional else '' | |
| 193 return '%s\t%d\t%s\t%d\t%d\t%s\t%s\t%s\t%s\t%s\t%s%s' % ( | |
| 194 self.qname,self.flag,self.rname,self.pos,self.mapq,self.cigar, | |
| 195 str(self.rnext) if self.rnext else '', | |
| 196 str(self.pnext) if self.pnext else '', | |
| 197 str(self.tlen) if self.tlen else '', | |
| 198 self.seq, | |
| 199 self.qual, opt_cols) | |
| 200 def add_optional(self,tag,value): | |
| 201 self.optional[tag] = value | |
| 202 | |
| 203 | |
| 204 class ProBAM ( object ): | |
| 205 def __init__(self,species=None,assembly=None,seqlens={},comments=[]): | |
| 206 self.species = species | |
| 207 self.assembly = assembly | |
| 208 self.seqlens = seqlens | |
| 209 self.comments = comments | |
| 210 self.entries = dict() | |
| 211 self.opt_columns = set() | |
| 212 self.rg = [] | |
| 213 | |
| 214 def add_entry(self,pb_entry): | |
| 215 if not pb_entry.rname in self.entries: | |
| 216 self.entries[pb_entry.rname] = [] | |
| 217 self.entries[pb_entry.rname].append(pb_entry) | |
| 218 if pb_entry.optional: | |
| 219 self.opt_columns | set(pb_entry.optional.keys()) | |
| 220 | |
| 221 def add_entry_from_bed(self,bed_entry,optional=dict()): | |
| 222 if bed_entry.pep: | |
| 223 optional['XP:Z'] = bed_entry.pep | |
| 224 qname=bed_entry.name | |
| 225 flag = 0 if bed_entry.strand == '+' else 16 | |
| 226 rname = bed_entry.chrom | |
| 227 pos = bed_entry.chromStart + 1 | |
| 228 cigar = bed_entry.get_cigar() | |
| 229 seq = bed_entry.get_spliced_seq(strand='+') if bed_entry.seq else '*' | |
| 230 pb_entry = ProBAMEntry(qname=qname, flag=flag, rname=rname, pos=pos,cigar=cigar,seq=seq,optional=optional) | |
| 231 self.add_entry(pb_entry) | |
| 232 | |
| 233 def write(self,fh): | |
| 234 fh.write('@HD VN:1.0 SO:coordinate\n') | |
| 235 rnames = sort_chrom_names(self.seqlens.keys()) | |
| 236 for sn in rnames: | |
| 237 fh.write('@SQ\tSN:%s\tLN:%d\n' % (sn,self.seqlens[sn])) | |
| 238 for rg in self.rg: | |
| 239 fh.write('@RG\tID:%s\n' % (rg)) | |
| 240 fh.write('@PG\tID:SampleSpecificGenerator\n') | |
| 241 for comment in self.comments: | |
| 242 fh.write('@CO\t%s\n' % comment) | |
| 243 for sn in rnames: | |
| 244 if sn not in self.entries: | |
| 245 continue | |
| 246 for pbe in sorted(self.entries[sn], key=attrgetter('pos')): | |
| 247 fh.write('%s\n' % str(pbe)) |
