Metadata-Version: 2.1
Name: ncbi-api
Version: 1.0
Summary: NCBI数据下载及解析
Home-page: UNKNOWN
Author: zhangyafei
Author-email: zhangyafeii@foxmail.com
License: UNKNOWN
Platform: UNKNOWN
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.6
Description-Content-Type: text/markdown
Requires-Dist: aiohttp
Requires-Dist: lxml
Requires-Dist: requests
Requires-Dist: zyf (>=0.6)
Requires-Dist: tqdm
Requires-Dist: pandas

## 安装

> pip install ncbi_api
>
> 或者
>
> pip install ncbi_api -i https://pypi.python.org/simple

## 使用

### Geo数据下载

#### Accession列表

- example

  ```python
  from ncbi_api.geo import AccessionDownloader

  downloader = AccessionDownloader()
  downloader.start(page_nums=299, display=500)
  ```

#### series_matrix

- example1：传入accession列表

  ```python
  from ncbi_api.geo import GeoDownloader, GeoDataType

  accessions = ['GSE113138', 'GSE171935', 'GSE164612', 'GSE166066']

  geo = GeoDownloader(accession_list=accessions)
  geo.run(data_type=GeoDataType.SeriesMatrix, workers=4)
  ```

- 运行

  ```
  -*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*-
  正在读取 Accession 列表
  	--> 共有 Accession 数量：4
  -*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*-
  成功读取并完成过滤 -> takes 0.104 seconds
  https://ftp.ncbi.nlm.nih.gov/geo/series/GSE166nnn/GSE166066/matrix/
  https://ftp.ncbi.nlm.nih.gov/geo/series/GSE171nnn/GSE171935/matrix/
  https://ftp.ncbi.nlm.nih.gov/geo/series/GSE113nnn/GSE113138/matrix/
  https://ftp.ncbi.nlm.nih.gov/geo/series/GSE164nnn/GSE164612/matrix/
  GSE113138 200
  GSE164612 200
  GSE171935 200
  GSE166066 200
  -*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*-
  正在读取 Accession 列表
  	--> 共有 Accession 数量：4, 成功下载：4, 下载失败(404)：0,  还剩：0
  -*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*-
  成功读取并完成过滤 -> takes 0.046 seconds
  -*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*-
  正在读取 url 列表
  	--> 共有 url 数量：4
  -*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*-
  成功读取并完成过滤 -> takes 0.004 seconds
  正在下载 -> GSE113138_series_matrix.txt.gz -> 3.18 KB: 4KB [00:00, 1999.91KB/s]                   
  正在下载 -> GSE166066_series_matrix.txt.gz -> 3.01 KB: 4KB [00:00, 1333.64KB/s]                   
  正在下载 -> GSE164612_series_matrix.txt.gz -> 2.21 MB:  21%|█    | 465/2258 [00:31<02:02, 14.60KB/s]
  ```

- example2：传入series_matrix_url列表

  ```python
  from ncbi_api.geo import GeoDownloader, GeoDataType

  series_matrix_urls = {
      'https://ftp.ncbi.nlm.nih.gov/geo/series/GSE93nnn/GSE93247/matrix/GSE93247_series_matrix.txt.gz',
      'https://ftp.ncbi.nlm.nih.gov/geo/series/GSE64nnn/GSE64216/matrix/GSE64216_series_matrix.txt.gz',
      'https://ftp.ncbi.nlm.nih.gov/geo/series/GSE53nnn/GSE53596/matrix/GSE53596-GPL13534_series_matrix.txt.gz'
  }
  geo = GeoDownloader()
  geo.run(data_type=GeoDataType.SeriesMatrix, series_matrix_urls=series_matrix_urls)
  ```

- example3: 传入series_matrix_url文件路径

  `history/series_matrix_urls.txt`

  ```
  https://ftp.ncbi.nlm.nih.gov/geo/series/GSE41nnn/GSE41032/matrix/GSE41032_series_matrix.txt.gz
  https://ftp.ncbi.nlm.nih.gov/geo/series/GSE65nnn/GSE65908/matrix/GSE65908_series_matrix.txt.gz
  https://ftp.ncbi.nlm.nih.gov/geo/series/GSE1nnn/GSE1183/matrix/GSE1183_series_matrix.txt.gz
  https://ftp.ncbi.nlm.nih.gov/geo/series/GSE130nnn/GSE130755/matrix/GSE130755_series_matrix.txt.gz
  https://ftp.ncbi.nlm.nih.gov/geo/series/GSE31nnn/GSE31020/matrix/GSE31020_series_matrix.txt.gz
  https://ftp.ncbi.nlm.nih.gov/geo/series/GSE23nnn/GSE23218/matrix/GSE23218_series_matrix.txt.gz
  https://ftp.ncbi.nlm.nih.gov/geo/series/GSE34nnn/GSE34117/matrix/GSE34117_series_matrix.txt.gz
  ...
  ```

  示例代码

  ```python
  from ncbi_api.geo import GeoDownloader, GeoDataType

  geo = GeoDownloader()
  geo.run(data_type=GeoDataType.SeriesMatrix, workers=4, series_matrix_url_filepath='history/series_matrix_urls.txt')
  ```

  运行

  ```
  -*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*-
  正在读取 url 列表
  	--> 共有 url 数量：4, 成功下载：3, 下载失败(404)：0,  还剩：1
  -*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*-
  成功读取并完成过滤 -> takes 0.002 seconds

  正在下载 -> GSE171935_series_matrix.txt.gz -> 1.86 KB: 2KB [00:00, 666.93KB/s]                     
  正在下载 -> GSE113138_series_matrix.txt.gz -> 3.18 KB: 4KB [00:00, 1335.45KB/s]                   
  正在下载 -> GSE166066_series_matrix.txt.gz -> 3.02 KB: 4KB [00:00, 14.50KB/s]                     
  正在下载 -> GSE164612_series_matrix.txt.gz -> 2.21 MB:  49%|█▉  | 1105/2258 [00:46<00:28, 41.07KB/s]
  ```

### Geo数据解压缩

- example1

  ```python
  from zyf.file import scan_directory_contents

  from ncbi_api.compress import Gzip

  file_list = scan_directory_contents('download')

  gzip = Gzip()
  gzip.batch_file_unzip(file_list, unzip_dir='unzip')
  ```

- example2

  ```python
  from ncbi_api.geo import GeoDownloader, GeoDataType
  from zyf.file import scan_directory_contents
  import os
  import re
  from ncbi_api.compress import Gzip


  def start_unzip():
      file_list = set(scan_directory_contents('download'))
      unzip_dir = 'unzip'
      history_dir = 'history'
      success_filepath, error_filepath = f'{history_dir}/unzip_success.txt', f'{history_dir}/unzip_error.txt'
      while True:
          gzip = Gzip(history_dir=history_dir, success_filepath=success_filepath, error_filepath=error_filepath)
          gzip.batch_file_unzip(file_list, unzip_dir=unzip_dir)

          if os.path.exists(error_filepath):
              series_matrix_urls = set()
              with open(error_filepath, mode='r') as f:
                  for line in f:
                      accession = re.search(r'download\\(.*?)_series_matrix.txt.gz', line).group(1)
                      url = f'https://ftp.ncbi.nlm.nih.gov/geo/series/{accession.split("-")[0][:-3]}nnn/{accession.split("-")[0]}/matrix/{accession}_series_matrix.txt.gz'
                      series_matrix_urls.add(url)
              if series_matrix_urls:
                  print('正在对解压失败文件进行重新下载')
                  geo = GeoDownloader(success_filepath='history/download_unzipfailed_success.txt', error_filepath='history/download_unzipfailed_error.txt')
                  geo.run(data_type=GeoDataType.SeriesMatrix, series_matrix_urls=series_matrix_urls)
              else:
                  return


  if __name__ == '__main__':
      start_unzip()
  ```

### Geo数据解析

- example

  ```python
  from zyf.file import scan_directory_contents

  from ncbi_api.geo import GeoParser, GeoDataType

  file_list = scan_directory_contents('unzip')
  parser = GeoParser(file_list=file_list)
  parser.run(GeoDataType.SeriesMatrix, workers=8)
  ```



