@@ -143,51 +143,49 @@ def intervals_extract(iterable):
143143def go (args ):
144144
145145 # open the reference sequence and collect the sequence header and sequence length of the first record
146- record = list (SeqIO .parse (args .reference , "fasta" ))[0 ]
147- seqID = record .id
148- seqLength = len (record .seq )
146+ records = [x for x in SeqIO .parse (args .reference , "fasta" )]
147+ intervals = []
149148
150- # collect the depths from the pileup, replacing any depth<minDepth with 0
151- try :
149+ for record in records :
150+ seqID = record .id
151+ seqLength = len (record .seq )
152+
153+ # collect the depths from the pileup, replacing any depth<minDepth with 0
152154 depths , rgDepths = collect_depths (
153155 args .bamfile ,
154156 seqID ,
155157 args .depth ,
156158 args .ignore_deletions ,
157159 args .warn_rg_coverage ,
158160 )
159- except Exception as e :
160- print (e )
161- raise SystemExit (1 )
162-
163- # check the number of positions in the reported depths matches the reference sequence
164- if len (depths ) != seqLength :
165- print ("pileup length did not match expected reference sequence length" )
166-
167- # print the readgroup depths to individual files if requested
168- if args .store_rg_depths :
169-
170- # rg is the readgroup and rgd is the depths per position for this readgroup
171- for rg , rgd in rgDepths .items ():
172- fh = open (args .outfile + "." + rg + ".depths" , "w" )
173- for pos , depth in enumerate (rgd ):
174- fh .write ("%s\t %s\t %d\t %d\n " % (seqID , rg , pos , depth ))
175- fh .close ()
176-
177- # create a mask_vector that records reference positions where depth < minDepth
178- mask_vector = []
179- for pos , depth in enumerate (depths ):
180- if depth == 0 :
181- mask_vector .append (pos )
182-
183- # get the intervals from the mask_vector
184- intervals = list (intervals_extract (mask_vector ))
185-
186- # create the mask outfile
187- maskfh = open (args .outfile , "w" )
188- for i in intervals :
189- maskfh .write ("%s\t %s\t %s\n " % (seqID , i [0 ] + 1 , i [1 ] + 1 ))
190- maskfh .close ()
161+
162+ # check the number of positions in the reported depths matches the reference sequence
163+ if len (depths ) != seqLength :
164+ print ("pileup length did not match expected reference sequence length" )
165+
166+ # print the readgroup depths to individual files if requested
167+ if args .store_rg_depths :
168+ # rg is the readgroup and rgd is the depths per position for this readgroup
169+ for rg , rgd in rgDepths .items ():
170+ fh = open (args .outfile + "." + rg + ".depths" , "a" )
171+ for pos , depth in enumerate (rgd ):
172+ fh .write ("%s\t %s\t %d\t %d\n " % (seqID , rg , pos , depth ))
173+ fh .close ()
174+
175+ # create a mask_vector that records reference positions where depth < minDepth
176+ mask_vector = []
177+ for pos , depth in enumerate (depths ):
178+ if depth == 0 :
179+ mask_vector .append (pos )
180+
181+ # get the intervals from the mask_vector
182+ intervals = list (intervals_extract (mask_vector ))
183+
184+ # create the mask outfile
185+ maskfh = open (args .outfile , "a" )
186+ for i in intervals :
187+ maskfh .write ("%s\t %s\t %s\n " % (seqID , i [0 ] + 1 , i [1 ] + 1 ))
188+ maskfh .close ()
191189
192190
193191def main ():
0 commit comments