Coverage for seqbank/main.py: 100.00%

81 statements  

« prev     ^ index     » next       coverage.py v7.17.0, created at 2026-11-27 04:47 +0000

1import typer 

2from typing import Annotated 

3from pathlib import Path 

4from rich.progress import track 

5import plotly.io as pio 

6 

7from .refseq import get_refseq_urls 

8from .seqbank import SeqBank 

9from .storage import Engine 

10from .dfam import download_dfam 

11 

12pio.defaults.mathjax = None 

13 

14 

15app = typer.Typer(pretty_exceptions_enable=False) 

16 

17 

18@app.command() 

19def add( 

20 path: Annotated[ 

21 Path, typer.Argument(help="SeqBank database directory; existing banks detect their engine automatically.") 

22 ], 

23 files: Annotated[ 

24 list[Path], typer.Argument(help="Sequence files to import (FASTA, FASTQ, or another supported format).") 

25 ], 

26 format: Annotated[str, typer.Option(help="Input sequence format; infer from the filename when omitted.")] = "", 

27 filter: Annotated[ 

28 Path | None, typer.Option(help="File listing accessions to include; omit to import all sequences.") 

29 ] = None, 

30 engine: Annotated[ 

31 Engine | None, 

32 typer.Option( 

33 help="Storage engine for a new bank (default: rocksdict if installed, otherwise lmdb); must match an existing bank." 

34 ), 

35 ] = None, 

36) -> None: 

37 """Add sequences from a file or list of files to a SeqBank.""" 

38 print(f"Opening seqbank '{path}'") 

39 seqbank = SeqBank(path=path, write=True, engine=engine) 

40 seqbank.add_files(files, format=format, filter=filter) 

41 

42 

43@app.command() 

44def add_sequence_from_file( 

45 path: Annotated[ 

46 Path, typer.Argument(help="SeqBank database directory; existing banks detect their engine automatically.") 

47 ], 

48 accession: Annotated[str, typer.Argument(help="Accession to assign to the imported sequence.")], 

49 file: Annotated[Path, typer.Argument(help="Input file containing exactly one sequence.")], 

50 format: Annotated[str, typer.Option(help="Input sequence format; infer from the filename when omitted.")] = "", 

51 engine: Annotated[ 

52 Engine | None, 

53 typer.Option( 

54 help="Storage engine for a new bank (default: rocksdict if installed, otherwise lmdb); must match an existing bank." 

55 ), 

56 ] = None, 

57) -> None: 

58 """Add a sequence from a file with a single sequence to a SeqBank.""" 

59 print(f"Opening seqbank '{path}'") 

60 seqbank = SeqBank(path=path, write=True, engine=engine) 

61 seqbank.add_sequence_from_file(accession, file, format=format) 

62 print(f"Added {accession} from {file.name}") 

63 

64 

65@app.command() 

66def url( 

67 path: Annotated[ 

68 Path, typer.Argument(help="SeqBank database directory; existing banks detect their engine automatically.") 

69 ], 

70 urls: Annotated[list[str], typer.Argument(help="URLs of sequence files to download and import.")], 

71 format: Annotated[str, typer.Option(help="Input sequence format; infer from the filename when omitted.")] = "", 

72 max: Annotated[int, typer.Option(help="Maximum number of URLs to import; 0 imports all URLs.")] = 0, 

73 workers: Annotated[ 

74 int, typer.Option(help="Number of download/import threads; -1 uses all available CPU cores.") 

75 ] = -1, 

76 tmp_dir: Annotated[ 

77 Path | None, typer.Option(help="Directory for temporary downloads; omit to use the system temporary directory.") 

78 ] = None, 

79 engine: Annotated[ 

80 Engine | None, 

81 typer.Option( 

82 help="Storage engine for a new bank (default: rocksdict if installed, otherwise lmdb); must match an existing bank." 

83 ), 

84 ] = None, 

85) -> None: 

86 """Add sequences from a list of URLs to a SeqBank.""" 

87 print(f"Opening seqbank '{path}'") 

88 seqbank = SeqBank(path=path, write=True, engine=engine) 

89 

90 seqbank.add_urls(urls, format=format, max=max, workers=workers, tmp_dir=tmp_dir) 

91 

92 

93@app.command() 

94def delete( 

95 path: Annotated[ 

96 Path, typer.Argument(help="SeqBank database directory; existing banks detect their engine automatically.") 

97 ], 

98 accessions: Annotated[list[str], typer.Argument(help="Accessions to delete from the bank.")], 

99) -> None: 

100 """Delete sequences from a SeqBank.""" 

101 print(f"Opening seqbank '{path}'") 

102 seqbank = SeqBank(path=path, write=True) 

103 

104 for accession in track(accessions, "Deleting"): 

105 seqbank.delete(accession) 

106 

107 

108@app.command() 

109def refseq( 

110 path: Annotated[ 

111 Path, typer.Argument(help="SeqBank database directory; existing banks detect their engine automatically.") 

112 ], 

113 max: Annotated[int, typer.Option(help="Maximum number of URLs to import; 0 imports all URLs.")] = 0, 

114 workers: Annotated[ 

115 int, typer.Option(help="Number of download/import threads; -1 uses all available CPU cores.") 

116 ] = -1, 

117 tmp_dir: Annotated[ 

118 Path | None, typer.Option(help="Directory for temporary downloads; omit to use the system temporary directory.") 

119 ] = None, 

120 engine: Annotated[ 

121 Engine | None, 

122 typer.Option( 

123 help="Storage engine for a new bank (default: rocksdict if installed, otherwise lmdb); must match an existing bank." 

124 ), 

125 ] = None, 

126) -> None: 

127 """Download all RefSeq sequences to a SeqBank.""" 

128 print("Getting RefSeq files list") 

129 return url(path, get_refseq_urls(tmp_dir=tmp_dir), max=max, workers=workers, tmp_dir=tmp_dir, engine=engine) 

130 

131 

132@app.command() 

133def dfam( 

134 path: Annotated[ 

135 Path, typer.Argument(help="SeqBank database directory; existing banks detect their engine automatically.") 

136 ], 

137 release: Annotated[str, typer.Option(help="DFam release to download, such as current.")] = "current", 

138 curated: Annotated[ 

139 bool, typer.Option(help="Download only curated DFam families; disable to include all families.") 

140 ] = True, 

141 engine: Annotated[ 

142 Engine | None, 

143 typer.Option( 

144 help="Storage engine for a new bank (default: rocksdict if installed, otherwise lmdb); must match an existing bank." 

145 ), 

146 ] = None, 

147) -> bool: 

148 """Download DFam sequences to a SeqBank.""" 

149 print("Getting DFam") 

150 seqbank = SeqBank(path=path, write=True, engine=engine) 

151 return download_dfam(seqbank, release=release, curated=curated) 

152 

153 

154@app.command() 

155def ls( 

156 path: Annotated[ 

157 Path, typer.Argument(help="SeqBank database directory; existing banks detect their engine automatically.") 

158 ], 

159) -> None: 

160 """List accessions in a SeqBank.""" 

161 seqbank = SeqBank(path=path) 

162 seqbank.ls() 

163 

164 

165@app.command() 

166def count( 

167 path: Annotated[ 

168 Path, typer.Argument(help="SeqBank database directory; existing banks detect their engine automatically.") 

169 ], 

170) -> None: 

171 """Display the number of accessions in a SeqBank.""" 

172 seqbank = SeqBank(path=path) 

173 print(len(seqbank)) 

174 

175 

176@app.command() 

177def merge( 

178 path: Annotated[ 

179 Path, typer.Argument(help="SeqBank database directory; existing banks detect their engine automatically.") 

180 ], 

181 new: Annotated[ 

182 Path, 

183 typer.Argument( 

184 help="Destination bank; new banks use RocksDict if installed (otherwise LMDB), existing banks retain their engine." 

185 ), 

186 ], 

187) -> None: 

188 """Merge every sequence from one SeqBank into another.""" 

189 print(f"Merging seqbank '{path}' into '{new}'") 

190 seqbank = SeqBank(path=path) 

191 destination = SeqBank(path=new, write=True) 

192 seqbank.copy(destination) 

193 

194 

195@app.command() 

196def export( 

197 path: Annotated[ 

198 Path, typer.Argument(help="SeqBank database directory; existing banks detect their engine automatically.") 

199 ], 

200 output: Annotated[Path, typer.Argument(help="Destination file for exported sequences.")], 

201 format: Annotated[str, typer.Option(help="Output sequence format, such as fasta or fastq.")] = "fasta", 

202) -> None: 

203 """Export a SeqBank to a specified format.""" 

204 print(f"Exporting seqbank '{path}' to '{output}' in {format} format") 

205 seqbank = SeqBank(path=path) 

206 return seqbank.export(output, format=format) 

207 

208 

209@app.command() 

210def histogram( 

211 path: Annotated[ 

212 Path, typer.Argument(help="SeqBank database directory; existing banks detect their engine automatically.") 

213 ], 

214 output_path: Annotated[ 

215 Path | None, typer.Option(help="Save the histogram image here (requires Chrome); omit to display it.") 

216 ] = None, 

217 show: Annotated[bool, typer.Option(help="Display the histogram, including when an image is also saved.")] = False, 

218 nbins: Annotated[int, typer.Option(help="Requested number of histogram bins.")] = 30, 

219 min: Annotated[ 

220 int, typer.Option(help="Minimum sequence length to include, in bases; 0 disables the lower bound.") 

221 ] = 0, 

222 max: Annotated[ 

223 int, typer.Option(help="Maximum sequence length to include, in bases; 0 disables the upper bound.") 

224 ] = 0, 

225) -> None: 

226 """Generate a histogram of sequence lengths from a SeqBank.""" 

227 # Load the SeqBank 

228 seqbank = SeqBank(path=path) 

229 

230 # Generate the histogram 

231 fig = seqbank.histogram(nbins=nbins, min=min, max=max) 

232 

233 # Save the histogram to the specified output path 

234 if output_path is None: 

235 show = True 

236 else: 

237 fig.write_image(output_path) 

238 print(f"Histogram saved to {output_path}") 

239 

240 if show: 

241 fig.show() 

242 

243 

244@app.command() 

245def convert( 

246 path: Annotated[ 

247 Path, typer.Argument(help="SeqBank database directory; existing banks detect their engine automatically.") 

248 ], 

249 destination: Annotated[Path, typer.Argument(help="New destination database directory; must not already exist.")], 

250 engine: Annotated[ 

251 Engine, 

252 typer.Option( 

253 help="Storage engine for the destination; RocksDict and Speedict require the rocksdict and speedict extras." 

254 ), 

255 ], 

256) -> None: 

257 """Convert all records into a new directory using the selected engine.""" 

258 if destination.exists(): 

259 raise typer.BadParameter("Destination must not exist") 

260 source = SeqBank(path) 

261 target = SeqBank(destination, write=True, engine=engine) 

262 try: 

263 source.copy(target) 

264 finally: 

265 source.close() 

266 target.close()