見出し画像

バイオインフォマティクス武者修行 #5 ~gdown~ (221223 更新)

※以下、国立遺伝学研究所が有するスーパーコンピューターシステムを利用しています。

みなさんは、Linux上でweb上の情報をダウンロードする際にどのコマンドを使っていますか?一般的にはwgetが知られていますよね。

ですが、そうはいかないときがあります。
例えば、ZIPファイルのようなファイルはwgetでダウンロードしづらい場合があり、Windowsでコマンドラインを起動しているといったんローカルでインストールしたあとにまたサーバーに投げなおす(sftpなどで)必要があります。そういうときに役立つのが今から紹介するgdownです。

このツールはもともとGoogle Driveからデータをダウンロードするために開発されたものです。
今回は、RNA-Seq解析で外注していて、データがGoogleDrive経由で届いたので、なんとか直接サーバーにダウンロードしたくて導入した方法を書きます。これ以外にも、上記のようなwgetが使いにくいシーン全般に使えます。

gdownを用いたGoogleDrive等からのデータのダウンロード方法

❶一応gdown用にconda環境を構築する

conda create -n download python=3 #pythonは一応3にした

Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: /home/iceplant4561/anaconda3/envs/download

  added / updated specs:
    - python=3


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    ca-certificates-2022.9.14  |       ha878542_0         152 KB  conda-forge
    libzlib-1.2.12             |       h166bdaf_3          65 KB  conda-forge
    openssl-3.0.5              |       h166bdaf_2         2.8 MB  conda-forge
    ------------------------------------------------------------
                                           Total:         3.0 MB

The following NEW packages will be INSTALLED:

  _libgcc_mutex      conda-forge/linux-64::_libgcc_mutex-0.1-conda_forge
  _openmp_mutex      conda-forge/linux-64::_openmp_mutex-4.5-2_gnu
  bzip2              conda-forge/linux-64::bzip2-1.0.8-h7f98852_4
  ca-certificates    conda-forge/linux-64::ca-certificates-2022.9.14-ha878542_0
  ld_impl_linux-64   conda-forge/linux-64::ld_impl_linux-64-2.36.1-hea4e1c9_2
  libffi             conda-forge/linux-64::libffi-3.4.2-h7f98852_5
  libgcc-ng          conda-forge/linux-64::libgcc-ng-12.1.0-h8d9b700_16
  libgomp            conda-forge/linux-64::libgomp-12.1.0-h8d9b700_16
  libnsl             conda-forge/linux-64::libnsl-2.0.0-h7f98852_0
  libsqlite          conda-forge/linux-64::libsqlite-3.39.3-h753d276_0
  libuuid            conda-forge/linux-64::libuuid-2.32.1-h7f98852_1000
  libzlib            conda-forge/linux-64::libzlib-1.2.12-h166bdaf_3
  ncurses            conda-forge/linux-64::ncurses-6.3-h27087fc_1
  openssl            conda-forge/linux-64::openssl-3.0.5-h166bdaf_2
  pip                conda-forge/noarch::pip-22.2.2-pyhd8ed1ab_0
  python             conda-forge/linux-64::python-3.10.6-ha86cf86_0_cpython
  readline           conda-forge/linux-64::readline-8.1.2-h0f457ee_0
  setuptools         conda-forge/noarch::setuptools-65.3.0-pyhd8ed1ab_1
  tk                 conda-forge/linux-64::tk-8.6.12-h27826a3_0
  tzdata             conda-forge/noarch::tzdata-2022c-h191b570_0
  wheel              conda-forge/noarch::wheel-0.37.1-pyhd8ed1ab_0
  xz                 conda-forge/linux-64::xz-5.2.6-h166bdaf_0


Proceed ([y]/n)? y


Downloading and Extracting Packages
libzlib-1.2.12       | 65 KB     | ################################################################################################################### | 100%
ca-certificates-2022 | 152 KB    | ################################################################################################################### | 100%
openssl-3.0.5        | 2.8 MB    | ################################################################################################################### | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate download
#
# To deactivate an active environment, use
#
#     $ conda deactivate

❷インストール

pip install gdown

Collecting gdown
  Downloading gdown-4.5.1.tar.gz (14 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Collecting beautifulsoup4
  Using cached beautifulsoup4-4.11.1-py3-none-any.whl (128 kB)
Collecting requests[socks]
  Downloading requests-2.28.1-py3-none-any.whl (62 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 62.8/62.8 kB 3.2 MB/s eta 0:00:00
Collecting six
  Using cached six-1.16.0-py2.py3-none-any.whl (11 kB)
Collecting filelock
  Downloading filelock-3.8.0-py3-none-any.whl (10 kB)
Collecting tqdm
  Downloading tqdm-4.64.1-py2.py3-none-any.whl (78 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 78.5/78.5 kB 4.2 MB/s eta 0:00:00
Collecting soupsieve>1.2
  Using cached soupsieve-2.3.2.post1-py3-none-any.whl (37 kB)
Collecting idna<4,>=2.5
  Downloading idna-3.4-py3-none-any.whl (61 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 61.5/61.5 kB 5.1 MB/s eta 0:00:00
Collecting certifi>=2017.4.17
  Downloading certifi-2022.9.14-py3-none-any.whl (162 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 162.5/162.5 kB 10.7 MB/s eta 0:00:00
Collecting urllib3<1.27,>=1.21.1
  Downloading urllib3-1.26.12-py2.py3-none-any.whl (140 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 140.4/140.4 kB 8.1 MB/s eta 0:00:00
Collecting charset-normalizer<3,>=2
  Downloading charset_normalizer-2.1.1-py3-none-any.whl (39 kB)
Collecting PySocks!=1.5.7,>=1.5.6
  Downloading PySocks-1.7.1-py3-none-any.whl (16 kB)
Building wheels for collected packages: gdown
  Building wheel for gdown (pyproject.toml) ... done
  Created wheel for gdown: filename=gdown-4.5.1-py3-none-any.whl size=14933 sha256=b4d0159530fcd972001155f469587cee5bb2fadc2fdfd7007b961bc87c07a2ba
  Stored in directory: /lustre7/home/iceplant4561/.cache/pip/wheels/38/72/83/dab5cb8321023eb1549d7a7d7f564b479ed4cf71edb70ddad6
Successfully built gdown
Installing collected packages: urllib3, tqdm, soupsieve, six, PySocks, idna, filelock, charset-normalizer, certifi, requests, beautifulsoup4, gdown
Successfully installed PySocks-1.7.1 beautifulsoup4-4.11.1 certifi-2022.9.14 charset-normalizer-2.1.1 filelock-3.8.0 gdown-4.5.1 idna-3.4 requests-2.28.1 six-1.16.0 soupsieve-2.3.2.post1 tqdm-4.64.1 urllib3-1.26.12

#※一応以下の作業もする
pip install --upgrade gdown

Requirement already satisfied: gdown in /lustre7/home/iceplant4561/anaconda3/envs/download/lib/python3.10/site-packages (4.5.1)
Requirement already satisfied: requests[socks] in /lustre7/home/iceplant4561/anaconda3/envs/download/lib/python3.10/site-packages (from gdown) (2.28.1)
Requirement already satisfied: tqdm in /lustre7/home/iceplant4561/anaconda3/envs/download/lib/python3.10/site-packages (from gdown) (4.64.1)
Requirement already satisfied: six in /lustre7/home/iceplant4561/anaconda3/envs/download/lib/python3.10/site-packages (from gdown) (1.16.0)
Requirement already satisfied: beautifulsoup4 in /lustre7/home/iceplant4561/anaconda3/envs/download/lib/python3.10/site-packages (from gdown) (4.11.1)
Requirement already satisfied: filelock in /lustre7/home/iceplant4561/anaconda3/envs/download/lib/python3.10/site-packages (from gdown) (3.8.0)
Requirement already satisfied: soupsieve>1.2 in /lustre7/home/iceplant4561/anaconda3/envs/download/lib/python3.10/site-packages (from beautifulsoup4->gdown) (2.3.2.post1)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /lustre7/home/iceplant4561/anaconda3/envs/download/lib/python3.10/site-packages (from requests[socks]->gdown) (1.26.12)
Requirement already satisfied: charset-normalizer<3,>=2 in /lustre7/home/iceplant4561/anaconda3/envs/download/lib/python3.10/site-packages (from requests[socks]->gdown) (2.1.1)
Requirement already satisfied: idna<4,>=2.5 in /lustre7/home/iceplant4561/anaconda3/envs/download/lib/python3.10/site-packages (from requests[socks]->gdown) (3.4)
Requirement already satisfied: certifi>=2017.4.17 in /lustre7/home/iceplant4561/anaconda3/envs/download/lib/python3.10/site-packages (from requests[socks]->gdown) (2022.9.14)
Requirement already satisfied: PySocks!=1.5.7,>=1.5.6 in /lustre7/home/iceplant4561/anaconda3/envs/download/lib/python3.10/site-packages (from requests[socks]->gdown) (1.7.1)

❸gdownを起動する

gdown --help
usage: gdown [-h] [-V] [-O OUTPUT] [-q] [--fuzzy] [--id] [--proxy PROXY]
             [--speed SPEED] [--no-cookies] [--no-check-certificate]
             [--continue] [--folder] [--remaining-ok]
             url_or_id
...

$ # a large file (~500MB)
$ gdown https://drive.google.com/uc?id=1l_5RK28JRL19wpT22B-DY9We3TVXnnQQ
$ md5sum fcn8s_from_caffe.npz
256c2a8235c1c65e62e48d3284fbd384

$ # same as the above but with the file ID
$ gdown 1l_5RK28JRL19wpT22B-DY9We3TVXnnQQ

$ # a small file
$ gdown https://drive.google.com/uc?id=0B9P1L--7Wd2vU3VUVlFnbTgtS2c
$ cat spam.txt
spam

$ # download with fuzzy extraction of a file ID
$ gdown --fuzzy 'https://drive.google.com/file/d/0B9P1L--7Wd2vU3VUVlFnbTgtS2c/view?usp=sharing&resourcekey=0-WWs_XOSctfaY_0-sJBKRSQ'
$ cat spam.txt
spam

$ # --fuzzy option also works with Microsoft Powerpoint files
$ gdown --fuzzy "https://docs.google.com/presentation/d/15umvZKlsJ3094HNg5S4vJsIhxcFlyTeK/edit?usp=sharing&ouid=117512221203072002113&rtpof=true&sd=true"

$ # a folder
$ gdown https://drive.google.com/drive/folders/15uNXeRBIhVvZJIhL4yTw4IsStMhUaaxl -O /tmp/folder --folder

$ # as an alternative to curl/wget
$ gdown https://httpbin.org/ip -O ip.json
$ cat ip.json
{
  "origin": "126.169.213.247"
}

$ # write stdout and pipe to extract
$ gdown https://github.com/wkentaro/gdown/archive/refs/tags/v4.0.0.tar.gz -O - --quiet | tar zxvf -
$ ls gdown-4.0.0/
gdown  github2pypi  LICENSE  MANIFEST.in  pyproject.toml  README.md  setup.cfg  setup.py  tests

❹例:gdownを用いたGoogleDriveを介したRNA-Seqデータのダウンロード

ダウンロード手順
①ファイルのIDを調べる
Google Driveのファイルを右クリックし、「リンクを取得」をクリックする
「一般的なアクセス」の下に「リンクを知っている全員。リンクを知っているインターネット上の誰もが編集できます。」と表示されていることを確認してから、「リンクをコピー」をクリックする。


ちなみに,ここで【制限付き】にするとあとでエラーがおこる。
以下のリンクアドレスが入手できる。

https://drive.google.com/file/d/1LrfYAiIp_XuKir6ju_1kIbMCTiPtLKk_/view?usp=share_link
このアドレスを分解すると、
https://drive.google.com/file/d/[FILE_ID]/view?usp=sharing
となり、file/d/と/view?の間にある"1LrfYAiIp_XuKir6ju_1kIbMCTiPtLKk_"がFILE_IDとなる。

あとで使うのでメモしておく。

以下のコマンドを実行する。

conda activate download
(download) gdown "https://drive.google.com/uc?export=download&id=1LrfYAiIp_XuKir6ju_1kIbMCTiPtLKk_"

※うまくいかないとき(以下のようなエラーが出るとき)
参考:https://nakamura196.hatenablog.com/entry/2022/02/22/112851

Access denied with the following error:

        Cannot retrieve the public link of the file. You may need to change
        the permission to 'Anyone with the link', or have had many accesses.

You may still be able to access the file from the browser:

         https://drive.google.com/uc?export=download&id=1LrfYAiIp_XuKir6ju_1kIbMCTiPtLKk_
 #以下のコマンドを実行する 。gdownを更新する。
pip install -U --no-cache-dir gdown --pre

Downloading...
From: https://drive.google.com/uc?export=download&id=1LrfYAiIp_XuKir6ju_1kIbMCTiPtLKk_
To: /lustre7/home/iceplant4561/DATA/Eleocharis_RNA-Seq/dark/RH22115238.zip
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 19.3G/19.3G [03:52<00:00, 82.9MB/s]

❺zipをダウンロードしたら解凍する

(download) ❺zipをダウンロードしたら解凍する
(download) unzip RH22115238.zip
Archive:  RH22115238.zip
   creating: data/share/project/rawdata_manager/fastq_management/result/RH22115238/
  inflating: data/share/project/rawdata_manager/fastq_management/result/RH22115238/sample_6_2.fq.gz
  inflating: data/share/project/rawdata_manager/fastq_management/result/RH22115238/sample_1_2.fq.gz
  inflating: data/share/project/rawdata_manager/fastq_management/result/RH22115238/md5.txt
  inflating: data/share/project/rawdata_manager/fastq_management/result/RH22115238/sample_4_1.fq.gz
  inflating: data/share/project/rawdata_manager/fastq_management/result/RH22115238/sample_6_1.fq.gz
  inflating: data/share/project/rawdata_manager/fastq_management/result/RH22115238/sample_4_2.fq.gz
  inflating: data/share/project/rawdata_manager/fastq_management/result/RH22115238/sample_1_1.fq.gz

(download)ls data/share/project/rawdata_manager/fastq_management/result/RH22115238/
md5.txt  sample_1_1.fq.gz  sample_1_2.fq.gz  sample_4_1.fq.gz  sample_4_2.fq.gz  sample_6_1.fq.gz  sample_6_2.fq.gz

それではまた。

(221223 更新)

この記事が気に入ったらサポートをしてみませんか?